创建 S3 暂存区

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

可以直接从指定 S3 桶中的文件加载数据,无论是否带有文件夹路径(或 S3 术语中的前缀)。如果路径以 / 结尾,则加载相应 S3 文件夹中的所有对象。

备注

上一步 中,如果您按照说明配置具有访问外部 S3 桶所需的策略和权限的 AWS IAM 角色,则您已经创建了 S3 暂存区。您可以跳过此步骤并继续 从 S3 暂存区复制数据

本主题内容:

外部暂存区

除了直接从 S3 桶中的文件加载之外,Snowflake 还支持创建命名的外部暂存区,其中封装了暂存文件所需的所有信息,包括:

  • 暂存文件的 S3 桶。

  • 桶的命名存储集成对象或 S3 凭据(如果受保护)。

  • 加密密钥(如果桶中的文件已加密)。

命名的外部暂存区是可选的,但当您计划定期从同一位置加载数据时,建议 使用。

备注

Snowflake uses multipart uploads when uploading to Amazon S3 and Google Cloud Storage. This process might leave incomplete uploads in the storage location for your external stage.

To prevent incomplete uploads from accumulating, we recommend that you set a lifecycle rule. For instructions, see the Amazon S3 (https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpu-abort-incomplete-mpu-lifecycle-config.html) or Google Cloud Storage (https://cloud.google.com/storage/docs/lifecycle#abort-mpu) documentation.

创建外部暂存区

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

备注

To create a stage, you must use a role that is granted or inherits the necessary privileges. For more information, see 访问控制要求 for CREATE STAGE.

使用 SQL 创建外部暂存区

使用 CREATE STAGE 命令通过 SQL 创建外部暂存区。

以下示例使用 SQL 创建名为 my_s3_stage 的外部暂存区,该暂存区引用名为 mybucket 的私密/受保护的 S3 桶以及名为 encrypted_files/ 的文件夹路径。CREATE 语句包括 选项 1:配置 Snowflake 存储集成以访问 Amazon S3 中创建的 s3_int 存储集成,用于访问 S3 桶。该暂存区引用名为 my_csv_format 的命名文件格式对象,该对象描述了存储在桶路径中的文件中的数据:

CREATE STAGE my_s3_stage
  STORAGE_INTEGRATION = s3_int
  URL = 's3://mybucket/encrypted_files/'
  FILE_FORMAT = my_csv_format;
Copy

备注

通过为暂存区指定命名的文件格式对象(或单个文件格式选项),以后就不必在用于从暂存区加载数据的 COPY 命令中指定相同的文件格式选项。

使用 Python 创建外部暂存区

使用 Snowflake Python APIsStageCollection.create 方法来创建外部暂存区。

与前面的 SQL 示例类似,下面的 Python 示例创建一个名为 my_s3_stage 的外部暂存区,该暂存区引用名为 mybucket 的 S3 桶,以及名为 encrypted_files/ 的文件夹路径:

from snowflake.core.stage import Stage

my_stage = Stage(
  name="my_s3_stage",
  storage_integration="s3_int",
  url="s3://mybucket/encrypted_files/"
)
root.databases["<database>"].schemas["<schema>"].stages.create(my_stage)
Copy

备注

Python API 当前不支持 CREATE STAGE SQL 命令的 FILE_FORMAT 参数。

使用 Snowsight 创建外部暂存区

要使用 Snowsight 创建指定外部暂存区,请执行以下操作:

  1. Sign in to Snowsight.

  2. At the top of the navigation menu, select Add a dashboard tile (Create) » Stage » External Stage.

  3. 选择外部云存储提供商:Amazon S3Microsoft AzureGoogle Cloud Platform

  4. Create Stage 对话框中,输入 Stage Name

  5. 选择要在其中创建暂存区的数据库和架构。

  6. 输入外部云存储位置的 URL

  7. 如果外部存储不是公开的,请启用 Authentication 并输入详细信息。有关更多信息,请参阅 CREATE STAGE

  8. (可选)取消选择 Directory table。目录表允许您在暂存区中查看文件,但需要仓库,因此会产生费用。您可以选择暂时取消选择此选项,稍后再启用目录表。

    If you enable Directory table, optionally select Enable auto-refresh, and then select your event notification or notification integration to automatically refresh the directory table when files are added or removed. For more information, see 自动目录表元数据刷新.

  9. If your files are encrypted, enable Encryption, and then enter your details.

  10. (Optional) To view a generated SQL statement, expand the SQL Preview. To specify additional options for your stage, such as AUTO_REFRESH, you can open this SQL preview in a worksheet.

  11. 选择 Create

使用 Classic Console 创建外部暂存区

Select Databases Databases 选项卡 » <db_name> » Stages.

下一步: 从 S3 暂存区复制数据

语言: 中文