为 Snowpark Scala 设置 SBT REPL

本主题介绍如何为 Snowpark 设置 SBT REPL。

在 sbt 中创建新的 Scala 项目

接下来,为 Snowpark 创建一个新的 Scala 项目。

  1. 为项目创建一个新目录,然后切换到该目录。

    mkdir snowpark_project
    cd snowpark_project
  2. Run the sbt new command, and specify the template (https://www.scala-sbt.org/1.x/docs/sbt-new-and-Templates.html) that you want to use to create the new project. For example:

    sbt new scala/hello-world.g8

输入项目的名称。这将创建一个具有该名称的项目目录。

为 Snowpark 配置 sbt 项目

接下来,为 Snowpark 配置项目。

In the build.sbt file for your project, make the following changes:

  1. If the scalaVersion setting does not match the version that you plan to use, update the setting. For example:

    scalaVersion := "2.12.20"

    Note that you must use a Scala version that is supported for use with the Snowpark library.

  2. Add the Snowpark library to the list of dependencies. For example:

    libraryDependencies += "com.snowflake" % "snowpark_2.12" % "1.18.0"
  1. 添加以下行以配置 REPL:
    Compile/console/scalacOptions += "-Yrepl-class-based"
    Compile/console/scalacOptions += "-Yrepl-outdir"
    Compile/console/scalacOptions += "repl_classes"

验证 sbt 项目配置

要验证您是否已将项目配置为使用 Snowpark,请运行简单的 Snowpark 代码示例。

  1. In the src/main/scala/Main.scala file, replace the contents with the code below:

    import com.snowflake.snowpark._
    import com.snowflake.snowpark.functions._
    
    object Main {
      def main(args: Array[String]): Unit = {
        // Replace the <placeholders> below.
        val configs = Map (
          "URL" -> "https://<account_identifier>.snowflakecomputing.cn:443",
          "USER" -> "<user name>",
          "PASSWORD" -> "<password>",
          "ROLE" -> "<role name>",
          "WAREHOUSE" -> "<warehouse name>",
          "DB" -> "<database name>",
          "SCHEMA" -> "<schema name>"
        )
        val session = Session.builder.configs(configs).create
        session.sql("show tables").show()
      }
    }

    Note the following:

    • Replace the placeholders with values that you use to connect to Snowflake.

    • For account_identifier, specify your account identifier.

    • If you prefer to use key pair authentication:

      • Replace PASSWORD with PRIVATE_KEY_FILE, and set it to the path to your private key file.
      • If the private key is encrypted, you must set PRIVATE_KEY_FILE_PWD to the passphrase for decrypting the private key.

      As an alternative to setting PRIVATE_KEY_FILE and PRIVATE_KEY_FILE_PWD, you can set the PRIVATEKEY property to the string value of the unencrypted private key from the private key file.

      • For example, if your private key file is unencrypted, set this to the value of the key in the file (without the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- header and footer and without the line endings).
      • Note that if the private key is encrypted, you must decrypt the key before setting it as the value of the PRIVATEKEY property.
  2. 切换到项目目录,执行以下命令以运行示例代码:

    sbt "runMain Main"