# Creating a Session for Snowpark¶

To use Snowpark in your application, you need to create a session. For convenience in writing code, you can also import the names of packages and objects.

In this Topic:

## Importing Names from Packages and Objects for Snowpark¶

The Snowpark API provides a number of classes, objects, and functions that are available in different packages. For convenience, you can import the class, object, and function names from packages and objects to avoid having to use qualified names.

For example:

• The com.snowflake.snowpark package contains the main classes for the Snowpark API. To import the names in this package, use:

import com.snowflake.snowpark._

• The com.snowflake.snowpark.functions object defines utility functions (including system-defined functions). To import the function names from this object, use:

import com.snowflake.snowpark.functions._

• The com.snowflake.snowpark.types defines classes and objects that you can use to define schemas for semi-structured data.

import com.snowflake.snowpark.types._


Note

If you used the run.sh script to start the Scala REPL, the script already imports names com.snowflake.snowpark and com.snowflake.snowpark.functions.

## Creating a Session for Snowpark¶

The first step in using the library is establishing a session with the Snowflake database. To create a session, use the SessionBuilder object. You can access the SessionBuilder object through the builder field in the Session companion object:

import com.snowflake.snowpark._

...
// Get a SessionBuilder object.
val builder = Session.builder


To provide the details to establish a session with a Snowflake database (for example, the account identifier, user name, etc.), either create a properties file (a text file) or programmatically build a Map containing the properties.

In the properties file or Map, set the following properties:

• URL: Set this to the URL for your account in the form https://account_identifier.snowflakecomputing.com.

If the account identifier contains underscores (_), replace those underscores with hyphens (-).

• Any additional JDBC parameters (see JDBC Driver Connection Parameter Reference in the JDBC driver documentation) needed to connect to Snowflake (e.g. USER, ROLE, WAREHOUSE, DB, SCHEMA, etc.).

Note

To change the logging level (e.g. from INFO to DEBUG), see Changing the Logging Settings.

To authenticate, you can use the same mechanisms that the JDBC Driver supports. For example, you can use:

For key-pair authentication, you can either:

• Set the PRIVATEKEY property to the private key (the contents of the private key .p8 file without any carriage returns, newlines, the -----BEGIN ... PRIVATE KEY----- header, and the -----END ... PRIVATE KEY----- footer).

• Set the PRIVATE_KEY_FILE property to the path to the private key file.

If the private key is encrypted, set the PRIVATE_KEY_FILE_PWD property to the passphrase for decrypting the key.

To create the session:

1. Set the properties in the Session.builder object.

• If you created a properties file, pass the path to the properties file to the Session.builder.configFile method.

• If you programmatically built a Map of the properties, pass the Map to the Session.builder.configs method.

Both methods return a builder object that has these properties.

2. Call the create method of the builder object to establish the session.

The following is an example of a properties file that sets the basic parameters for connecting to a Snowflake database. The example is set up to use key-pair authentication.

# profile.properties file (a text file)
URL = https://<account_identifier>.snowflakecomputing.com
PRIVATEKEY = <private_key_copied_from_the_private_key_file>
ROLE = <role_name>
WAREHOUSE = <warehouse_name>
DB = <database_name>
SCHEMA = <schema_name>


Note that the PRIVATEKEY property should be set to the value of the private key from the private key file. If you prefer to point to the private key file, set PRIVATE_KEY_FILE instead. In addition, if the private key is encrypted, you must set PRIVATE_KEY_FILE_PWD to the passphrase for decrypting the private key:

# profile.properties file (a text file)
URL = https://<account_identifier>.snowflakecomputing.com
PRIVATE_KEY_FILE = </path/to/private_key_file.p8>
PRIVATE_KEY_FILE_PWD = <if the private key is encrypted, set this to the passphrase for decrypting the key>
ROLE = <role_name>
WAREHOUSE = <warehouse_name>
DB = <database_name>
SCHEMA = <schema_name>


The following example uses this properties file to create a new session:

// Create a new session, using the connection properties
// specified in a file.
val session = Session.builder.configFile("/path/to/properties/file").create


The following example uses a Map to set the properties:

// Create a new session, using the connection properties
// specified in a Map.
val session = Session.builder.configs(Map(
"URL" -> "https://<account_identifier>.snowflakecomputing.com",
"PRIVATEKEY" -> "<private_key_copied_from_the_private_key_file>",
"ROLE" -> "<role_name>",
"WAREHOUSE" -> "<warehouse_name>",
"DB" -> "<database_name>",
"SCHEMA" -> "<schema_name>"
)).create


As is the case with the properties file example, the PRIVATEKEY property should be set to the value of the private key from the private key file. If you prefer to point to the private key file, set PRIVATE_KEY_FILE instead. In addition, if the private key is encrypted, you must set PRIVATE_KEY_FILE_PWD to the passphrase for decrypting the private key:

// Create a new session, using the connection properties
// specified in a Map.
val session = Session.builder.configs(Map(
"URL" -> "https://<account_identifier>.snowflakecomputing.com",