为 Snowpark Python 创建会话¶
要在应用程序中使用 Snowpark,您需要创建一个会话。为了方便编写代码,您还可以导入包和对象的名称。
创建会话
使用该库的第一步是与 Snowflake 数据库建立会话。
导入会话类。
To authenticate, you use the same mechanisms that the Snowflake Connector for Python supports.
Establish a session with a Snowflake database using the same parameters (for example, the account name, user name, etc.) that you use in the connect function in the Snowflake
Connector for Python. For more information, see the parameters for the connect function in the Python Connector API documentation.
Connect by using the connections.toml file¶
要在连接配置文件中添加凭据,请执行以下操作:
-
In a text editor, open the
connections.tomlfile for editing. For example, to open the file in the Linux vi editor: -
添加新的 Snowflake 连接定义。
You can generate the basic settings for the TOML configuration file in Snowsight. For information, see Configuring a client, driver, library, or third-party application to connect to Snowflake.
For example, to add a Snowflake connection called
myconnectionwith the accountmyaccount, userjohndoe, and password credentials, as well as database information, add the following lines to the configuration file:Connection definitions support the same configuration options available in the snowflake.connector.connect method.
-
可选:添加更多连接,如下所示:
-
保存对文件所做的更改。
-
In your Python code, supply connection name to
snowflake.connector.connectand then add it tosession, similar to the following:
For more information, see configuration file.
通过指定连接参数建立连接
Construct a dictionary (dict) containing the names and values of these parameters
(e.g. account, user, role, warehouse, database, schema, etc.).
要创建会话,请执行以下操作:
- Create a Python dictionary (
dict) containing the names and values of the parameters for connecting to Snowflake. - Pass this dictionary to the
Session.builder.configsmethod to return a builder object that has these connection parameters. - Call the
createmethod of thebuilderto establish the session.
The following example uses a dict containing connection parameters to create a new session:
For the account parameter, use your account identifier.
Note that the account identifier does not include the snowflakecomputing.cn suffix.
Note
This example shows you one way to create a session but there are several other ways that you can connect, including: the default authenticator, single sign-on (SSO), multi-factor authentication (MFA), key pair authentication, using a proxy server, and OAuth. For more information, see Connecting to Snowflake with the Python Connector.
通过 Web 浏览器使用单点登录 (SSO)¶
If you have configured Snowflake to use single sign-on (SSO), you can configure your client application to use browser-based SSO for authentication.
Construct a dictionary (dict) containing the names and values of these parameters
(e.g. account, user, role, warehouse, database, authenticator, etc.).
要创建会话,请执行以下操作:
- Create a Python dictionary (
dict) containing the names and values of the parameters for connecting to Snowflake. - Pass this dictionary to the
Session.builder.configsmethod to return a builder object that has these connection parameters. - Call the
createmethod of thebuilderto establish the session.
The following example uses a dict containing connection parameters to create a new session. Set the authenticator option to externalbrowser.
结束会话
如果您不再需要使用会话执行查询并且想要取消当前正在运行的任何查询,请调用该会话对象的 close 方法。例如: