为 Snowpark Scala 设置 IntelliJ IDEA CE

本主题介绍如何为 Snowpark 设置 IntelliJ IDEA CE。

为 Scala 开发设置 IntelliJ IDEA CE

To use Scala in IntelliJ IDEA CE, you need to install the Scala plugin. See the Installation section (https://docs.scala-lang.org/getting-started/intellij-track/getting-started-with-scala-in-intellij.html#installation) of the tutorial Getting Started with Scala in IntelliJ IDEA (https://docs.scala-lang.org/getting-started/intellij-track/getting-started-with-scala-in-intellij.html).

在 IntelliJ IDEA 中创建新的 Scala 项目

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

  1. Choose File » New » Project.

    1. 在左侧列表中,选择 Scala。

    2. 在右侧列表中,选择 sbt。

      Window for selecting the type of project to create
    3. Click Next.

  2. 填写新项目的详细信息。

    For the JDK and Scala SDK, select the JDK and Scala versions supported for use with Snowpark.

  3. Click Finish to create the new project.

为 Snowpark 配置 IntelliJ IDEA 项目

接下来,为 Snowpark 配置项目。

  1. In the Project tool window on the left (https://www.jetbrains.com/help/idea/2020.3/guided-tour-around-the-user-interface.html), double-click on the build.sbt file for your project.

    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"
  2. Save the changes to the build.sbt file.

  3. 更新 Maven 存储库。

    See Update Maven repositories (https://www.jetbrains.com/help/idea/troubleshooting-common-maven-issues.html#5e1bf655).

  4. 重新加载 SBT 项目:

    1. Choose View » Tool Windows » sbt to display the sbt Tool window.
    2. Right-click on the project name, and choose Reload sbt Project.

这会导致 IntelliJ IDEA CE 下载 Snowpark 库,并使 API 可在您的代码中使用。

验证 IntelliJ IDEA 项目配置

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

  1. In the Project tool window on the left (https://www.jetbrains.com/help/idea/2020.3/guided-tour-around-the-user-interface.html), expand your project, expand the src/main folders, and select the scala folder.

  2. Right-click on the folder, and choose New » Scala class.

  3. In the Create New Scala Class dialog box, enter the name “Main”, select Object, and press the Enter key.

  4. In the 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.
  5. Click the green arrow next to the Object line, and choose Run Main to run the example.

    Arrow icon for running the sample program