Snowflake Python APIs 教程的常用设置

简介

This topic provides instructions for the common setup required for all Snowflake Python APIs tutorials available in this documentation.

Overview of the Snowflake Python APIs

Before starting your setup, take a look at the Snowflake Python APIs structure. The following table lists some common modules in the API:

ModuleDescription
snowflake.coreDefines an iterator to represent certain resource instances fetched from the Snowflake database.
snowflake.core.databaseManages Snowflake databases.
snowflake.core.schemaManages Snowflake schemas.
snowflake.core.tableManages Snowflake tables.
snowflake.core.taskManages Snowflake tasks.
snowflake.core.task.dagv1A set of APIs at a higher level than the task APIs in snowflake.core.task to more conveniently manage task graphs (DAGs).
snowflake.core.compute_poolManages compute pools in Snowpark Container Services.
snowflake.core.image_repositoryManages image repositories in Snowpark Container Services.
snowflake.core.serviceManages services in Snowpark Container Services.

For a complete list of the APIs currently available, see the API reference documentation.

The snowflake.core module represents the entry point to the core Snowflake Python APIs that manage Snowflake objects. To use the API, you follow a common pattern:

  1. 使用 Snowpark 或 Python Connector 连接建立会话,表示您与 Snowflake 的连接。

  2. Import and instantiate the Root class from snowflake.core, and pass the Snowpark session object as an argument.

    You use the resulting Root object to access the rest of the methods and types in the API.

For more information about the programming model of the API, see Snowflake Python APIs: General concepts.

下面的代码提供了此模式的典型示例:

from snowflake.snowpark import Session
from snowflake.core import Root

session = Session.builder.config("connection_name", "default").create()
root = Root(session)

For more information about various connection options and attributes, see Connect to Snowflake with the Snowflake Python APIs.

Note

The Snowflake Python APIs can establish a connection to Snowflake using either a Snowpark session or a Python Connector connection. The preceding example uses a Snowpark session.

继续执行下一步,开始设置 API 及您的开发环境!

Install the Snowflake Python APIs

Important

The Snowflake Python APIs currently supports the following versions of Python:

Generally available versions:

  • 3.9 (deprecated)
  • 3.10
  • 3.11
  • 3.12
  • 3.13

在安装 API 之前,您需要激活 Python 环境。

在本教程中,您可以使用 conda 或虚拟环境 (venv)。

  1. To create and activate a conda or virtual environment, open a command-line terminal and run the following commands:

    conda create -n <env_name> python==3.10
    conda activate <env_name>
  2. The Snowflake Python APIs package is available in PyPI.

若要在新的 conda 或虚拟环境中安装 API 包,运行以下命令:

pip install snowflake -U
  1. To install the snowflake-snowpark-python package, run the following command:

    pip install 'snowflake-snowpark-python>=1.5.0,<2.0.0'

    In these tutorials, you use the snowflake.snowpark.Session object from the Snowpark API for Python to create a connection to Snowflake.

设置开发环境

This tutorial walks through code examples that you can run in a Jupyter notebook. Each step in the tutorial incrementally showcases the capabilities of the Snowflake Python APIs.

首先设置您的开发环境,以便在笔记本中运行代码示例。

  1. Create a file named $HOME/.snowflake/connections.toml with the following connection parameters, and update it with your real credentials:

    [default]
    account = "<YOUR ACCOUNT NAME>"
    user = "<YOUR ACCOUNT USER>"
    password = "<YOUR ACCOUNT USER PASSWORD>"
    # optional
    # warehouse = "<YOUR COMPUTE WH>"
    # optional
    # database = "<YOUR DATABASE>"
    # optional
    # schema = "<YOUR SCHEMA>"

    Note

    The account parameter does not support account identifiers with underscores. You must specify an account identifier with dashes in place of any underscores. For more information, see Account name in your organization.

    This example specifies these parameters as the default connection to Snowflake in your environment by creating a connection definition named default.

  2. 使用如下方法之一打开笔记本:

    • 在支持 Jupyter 笔记本的代码编辑器(例如 Visual Studio Code)中打开一个新笔记本。

    • To open a notebook in your browser, start a notebook server with the command jupyter notebook.

      To ensure that your environment can run a notebook, run conda install notebook in your terminal before starting the notebook server.

  3. 在笔记本的第一个单元格中,运行以下导入语句:

    from datetime import timedelta
    
    from snowflake.snowpark import Session
    from snowflake.snowpark.functions import col
    from snowflake.core import Root, CreateMode
    from snowflake.core.database import Database
    from snowflake.core.schema import Schema
    from snowflake.core.stage import Stage
    from snowflake.core.table import Table, TableColumn, PrimaryKey
    from snowflake.core.task import StoredProcedureCall, Task
    from snowflake.core.task.dagv1 import DAGOperation, DAG, DAGTask
    from snowflake.core.warehouse import Warehouse

    Note

    After running this cell, you might be prompted to set your Python kernel. If you activated a conda environment, select conda as the Python kernel (for example, something similar to: ~/miniconda3/envs/<your conda env>/bin/python).

在此单元格中,导入 Snowpark 和管理 Snowflake 对象的核心 APIs。

  1. 要与 Snowflake 建立连接,请在下一个单元格中,运行以下代码:

    session = Session.builder.config("connection_name", "default").create()

    In this cell, you create a Snowpark session and set the connection parameters for your session by specifying the connection_name as the default connection definition that you previously configured.

  2. To create a Root object, pass your session object to the Root constructor:

    root = Root(session)

And that’s it! By running the code in these three cells, you’re now ready to use the Snowflake Python APIs.

下一步是什么?

You can now explore Tutorial 1: Create a database, schema, table, and warehouse.