Setting Up the SBT REPL for Snowpark Scala¶
This topic explains how to set up the SBT REPL for Snowpark.
Creating a New Scala Project in sbt¶
Next, create a new Scala project for Snowpark.
Create a new directory for your project, and change to that directory.
mkdir snowpark_project cd snowpark_project
Run the
sbt new
command, and specify the template that you want to use to create the new project. For example:sbt new scala/hello-world.g8
Enter a name for your project. This creates a project directory with that name.
Configuring the sbt Project for Snowpark¶
Next, configure the project for Snowpark.
In the build.sbt
file for your project, make the following changes:
If the
scalaVersion
setting does not match the version that you plan to use, update the setting. For example:scalaVersion := "2.12.13"
Note that you must use a Scala version that is supported for use with the Snowpark library.
Add the Snowpark library to the list of dependencies. For example:
libraryDependencies += "com.snowflake" % "snowpark" % "1.14.0"
Add the following lines to configure the REPL:
Compile/console/scalacOptions += "-Yrepl-class-based" Compile/console/scalacOptions += "-Yrepl-outdir" Compile/console/scalacOptions += "repl_classes"
Verifying Your sbt Project Configuration¶
To verify that you have configured your project to use Snowpark, run a simple example of Snowpark code.
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.com: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
withPRIVATE_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
andPRIVATE_KEY_FILE_PWD
, you can set thePRIVATEKEY
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.
If you plan to create UDFs:
Don’t set up your
object
to extend theApp
trait. For details, see Caveat About Creating UDFs in an Object With the App Trait.Don’t set up your
object
to extend a class or trait that is not serializable.
Change to your project directory, and run the following command to run the sample code:
sbt "runMain Main"