为本地文件选择内部暂存区

暂存区指定数据文件的存储位置(即“暂存”),以便可以将文件中的数据加载到表中。

内部暂存区的类型

Snowflake 支持以下类型的内部暂存区:

  • 用户
  • 命名

默认情况下,Snowflake 中的每个用户和表都会自动分配一个内部暂存区,用于暂存要加载的数据文件。此外,还可以创建命名的内部暂存区。

数据加载过程的两个步骤中都需要文件暂存信息:

  1. You must specify an internal stage in the PUT command when uploading files to Snowflake.
  2. You must specify the same stage in the COPY INTO <table> command when loading data into a table from the staged files.

考虑特定数据文件的最佳暂存区类型。每个选项都有优点和潜在的缺点。

用户暂存区

默认情况下,每个用户都分配了一个用于存储文件的 Snowflake 暂存区。如果您的文件仅由单个用户访问,但需要复制到多个表中,则此暂存区是一个方便的选项。

用户暂存区具有以下特征和局限性:

  • User stages are referenced using @~; e.g. use LIST @~ to list the files in a user stage.
  • 与命名暂存区不同,不能更改或删除用户暂存区。
  • User stages do not support setting file format options. Instead, you must specify file format and copy options as part of the COPY INTO <table> command.

如果出现以下情况,此选项不适用:

  • 多个用户需要访问文件。
  • 当前用户对数据将被加载到的表没有 INSERT 权限。

表暂存区

Note

Apache Iceberg™ tables in Snowflake don’t support table stages.

默认情况下,每个表都分配了一个用于存储文件的 Snowflake 暂存区。该暂存区被称为 暂存区。

如果您只需要将文件复制到单个表中,但希望多个用户可以访问这些文件,则可以使用表暂存区。

表暂存区具有以下特征和局限性:

  • A table stage has the same name as the table. For example, a table named mytable has a stage referenced as @%mytable.
  • A table stage is an implicit stage tied to a table object. It’s not a separate database object. As a result, a table stage has no grantable privileges of its own. A table stage is also not appropriate if you need to copy file data into multiple tables.
  • To stage files on a table stage, list the files, query the files, or drop them, you must be the table owner (have the role with the OWNERSHIP privilege on the table).
  • 与命名暂存区不同,您无法更改或删除表暂存区。
  • 表暂存区不支持在加载数据时转换数据(使用查询作为 COPY 命令的源)。

命名暂存区

命名暂存区是数据库对象,可为数据加载提供最大程度的灵活性:

  • 在暂存区中具有适当权限的用户可以将数据加载到任何表中。
  • Because the stage is a database object, the security/access rules that apply to all objects apply. The privileges to use a stage can be granted or revoked from roles. In addition, ownership of the stage can be transferred to another role.

如果您计划暂存仅由您自己加载的数据文件,或者仅将数据加载到单个表中,则您可能更愿意仅使用您的用户暂存区或要加载数据的表的暂存区。

Named stages are optional but recommended when you plan regular data loads that could involve multiple users and/or tables. For instructions on creating a named stage, see Creating a Named Stage below.

创建命名暂存区

您可以使用 SQL 或 Web 界面创建命名的内部暂存区。

Note

To create a stage, you must use a role that is granted or inherits the necessary privileges. For more information, see Access control requirements for CREATE STAGE.

使用 SQL 创建命名暂存区

Use the CREATE STAGE command to create a named stage using SQL.

以下示例创建一个使用服务器端加密的内部暂存区:

CREATE STAGE my_int_stage
  ENCRYPTION = (TYPE = 'SNOWFLAKE_SSE');

使用 Python 创建命名暂存区

Use the StageCollection.create method of the Snowflake Python APIs to create a named stage. For more information, see Creating a stage.

以下示例创建一个使用服务器端加密的内部暂存区:

from snowflake.core.stage import Stage, StageEncryption

my_stage = Stage(
  name="my_int_stage",
  encryption=StageEncryption(type="SNOWFLAKE_SSE")
)
root.databases["<database>"].schemas["<schema>"].stages.create(my_stage)

Create a named stage using Snowsight

To use Snowsight to create a named internal stage, do the following:

  1. sign in to Snowsight.

  2. at the top of the navigation menu, select Add a dashboard tile (Create) » Stage » Snowflake Managed.

  3. In the Create Stage dialog, enter a Stage Name.

  4. Select the database and schema where you want to create the stage.

  5. Optionally deselect Directory table. Directory tables let you see files on the stage, but require a warehouse and thus incur a cost. You can choose to deselect this option for now and enable a directory table later.

  6. Select the type of Encryption supported for all files on your stage. For details, see encryption for internal stages. You can’t change the encryption type after you create the stage.

    Note

    To enable data access, use server-side encryption. Otherwise, staged files are client-side encrypted by default and unreadable when downloaded. For more information, see Server-side encryption for unstructured data access.

  7. Complete the fields to describe your stage. For more information, see CREATE STAGE.

  8. Select Create.

Next: Staging data files from a local file system