Connect to Snowflake with the Snowflake Python APIs¶
Before you can perform actions with the Snowflake Python APIs, you must define a connection to Snowflake. With the connection, you can
create a Root object for access to resources modeled by the API.
指定连接属性
您可以使用以下机制之一定义与 Snowflake 的连接:
使用 Python 字典进行连接¶
您可以使用 Python 字典指定连接到 Snowflake 所需的值。连接时,将此字典作为实参传递给您用于连接的函数或方法:
使用配置文件进行连接
You can specify connection definitions in a TOML configuration file. This eliminates the need to explicitly define a connection to Snowflake in your code.
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.
You can also configure the connection settings manually. For example, create a configuration file located at
~/.snowflake/connections.toml, and add connection settings similar to the following:
In this example, you define a Snowflake connection named myconnection with the account test-account, user test_user,
password credentials, and database information.
Note
Underscores are not supported in the account setting. If the account identifier
includes underscores, replace them with dashes. For more information, see Account name in your organization.
Connection definitions support the same configuration options available in the Snowflake Python Connector.
Connect and create a Root object¶
Using the connection properties you’ve specified, you can create a connection to Snowflake. With the connection, you can create a
Snowflake Python APIs Root object with which to begin using the API.
您可以使用以下对象之一进行连接:
Connect with a Snowpark Session¶
If you’re using the Snowpark API for Python, you can create a connection to Snowflake
by using its snowflake.snowpark.Session object.
The Snowpark Python library is not automatically installed as a dependency of snowflake.core. To connect to Snowflake using the
Snowpark Session object, follow these steps:
-
To install the
snowflake-snowpark-pythonpackage, run the following command: -
要创建到 Snowflake 的连接,请运行类似于以下示例的代码:
In this example, the code creates a
Sessionobject using a connection definition namedmyconnection, which is specified in a configuration file. Using the resultingSessionobject, the code creates aRootobject from which to use the API.
For more information about creating a Session, see Creating a Session for Snowpark Python.
Connect with a Python Connector Connection¶
If you’re using the Snowflake Connector for Python, you can create a connection
to Snowflake by using its snowflake.connector.connect function. The function returns a Connection object.
You don’t need to install the Python Connector library separately. The snowflake-connector-python package is installed automatically as
a dependency when you install the snowflake parent package.
Code in the following example creates a Connection object using a connection definition named myconnection, which is specified
in a configuration file. Using the resulting Connection object, the code creates a Root object from which to use the API:
For more information about the Snowflake Connector for Python API, see Python Connector API.
Use the Root object¶
With a Root object created from your connection to Snowflake, you can access
objects and methods of the Snowflake Python APIs. The Root object is the root of the resource tree modeled by the API.
You use the Root object to interact with Snowflake objects represented by the API.
Code in the following example uses the Root object to access Snowflake objects in order to resume the task named mytask.
The task is in the schema named myschema, which is in the database named mydb. The code uses the databases,
schemas, and tasks methods to get an object that represents this task: