snowflake.snowpark.Session¶
- class snowflake.snowpark.Session(conn: ServerConnection, options: Optional[Dict[str, Any]] = None)[source]¶
Bases:
objectEstablishes a connection with a Snowflake database and provides methods for creating DataFrames and accessing objects for working with files in stages.
When you create a
Sessionobject, you provide connection parameters to establish a connection with a Snowflake database (e.g. an account, a user name, etc.). You can specify these settings in a dict that associates connection parameters names with values. The Snowpark library uses the Snowflake Connector for Python to connect to Snowflake. Refer to Connecting to Snowflake using the Python Connector for the details of Connection Parameters.To create a
Sessionobject from adictof connection parameters:>>> connection_parameters = { ... "user": "<user_name>", ... "password": "<password>", ... "account": "<account_name>", ... "role": "<role_name>", ... "warehouse": "<warehouse_name>", ... "database": "<database_name>", ... "schema": "<schema_name>", ... } >>> session = Session.builder.configs(connection_parameters).create()
To create a
Sessionobject from an existing Python Connector connection:>>> session = Session.builder.configs({"connection": <your python connector connection>}).create()
Sessioncontains functions to construct aDataFrameliketable(),sql()andread, etc.A
Sessionobject is not thread-safe.Methods
add_import(path[, import_path])Registers a remote file in stage or a local file as an import of a user-defined function (UDF).
add_packages(*packages)Adds third-party packages as dependencies of a user-defined function (UDF).
add_requirements(file_path)Adds a requirement file that contains a list of packages as dependencies of a user-defined function (UDF).
call(sproc_name, *args[, statement_params])Calls a stored procedure by name.
Cancel all action methods that are running currently.
Clears all files in a stage or local files from the imports of a user-defined function (UDF).
Clears all third-party packages of a user-defined function (UDF).
close()Close this session.
createDataFrame(data[, schema])Creates a new DataFrame containing the specified values from the local data.
create_async_job(query_id)Creates an
AsyncJobfrom a query ID.create_dataframe(data[, schema])Creates a new DataFrame containing the specified values from the local data.
flatten(input[, path, outer, recursive, mode])Creates a new
DataFrameby flattening compound values into multiple rows.generator(*columns[, rowcount, timelimit])Creates a new DataFrame using the Generator table function.
Returns the name of the current account for the Python connector session attached to this session.
Returns the name of the current database for the Python connector session attached to this session.
Returns the name of the primary role in use for the current session.
Returns the name of the current schema for the Python connector session attached to this session.
Returns the name of the warehouse in use for the current session.
Returns the fully qualified name of the current schema for the session.
Returns a list of imports added for user defined functions (UDFs).
Returns a
dictof packages added for user-defined functions (UDFs).Returns the name of the temporary stage created by the Snowpark library for uploading and storing temporary artifacts for this session.
Create an instance of
QueryHistoryas a context manager to record queries that are pushed down to the Snowflake database.range(start[, end, step])Creates a new DataFrame from a range of numbers.
remove_import(path)Removes a file in stage or local file from the imports of a user-defined function (UDF).
remove_package(package)Removes a third-party package from the dependency list of a user-defined function (UDF).
replicate_local_environment([...])Adds all third-party packages in your local environment as dependencies of a user-defined function (UDF).
sql(query[, params])Returns a new DataFrame representing the results of a SQL query.
table(name)Returns a Table that points the specified table.
table_function(func_name, *func_arguments, ...)Creates a new DataFrame from the given snowflake SQL table function.
use_database(database)Specifies the active/current database for the session.
use_role(role)Specifies the active/current primary role for the session.
use_schema(schema)Specifies the active/current schema for the session.
use_secondary_roles(roles)Specifies the active/current secondary roles for the session.
use_warehouse(warehouse)Specifies the active/current warehouse for the session.
write_pandas(df, table_name, *[, database, ...])Writes a pandas DataFrame to a table in Snowflake and returns a Snowpark
DataFrameobject referring to the table where the pandas DataFrame was written to.Attributes
Returns a builder you can use to set configuration properties and create a
Sessionobject.confGet or set configuration parameters related to usage of custom Python packages in Snowflake.
Returns a
FileOperationobject that you can use to perform file operations on stages.The query tag for this session.
Returns a
DataFrameReaderthat you can use to read data from various supported sources (e.g.Returns a
stored_procedure.StoredProcedureRegistrationobject that you can use to register stored procedures.Set to
Trueto use the SQL simplifier (defaults toTrue).Controls whether or not the Snowpark client sends usage telemetry to Snowflake.
udafReturns a
udaf.UDAFRegistrationobject that you can use to register UDAFs.Returns a
udf.UDFRegistrationobject that you can use to register UDFs.Returns a
udtf.UDTFRegistrationobject that you can use to register UDTFs.