卸载到 Google Cloud Storage¶
如果您已经拥有 Google Cloud Storage 账户并使用 Cloud Storage 桶来存储和管理数据文件,您可以在从 Snowflake 表卸载数据时使用现有桶和文件夹路径。本主题介绍如何使用 COPY 命令来将数据从表卸载到 Cloud Storage 桶的命令。然后,您可以将卸载的数据文件下载到本地文件系统。
如下图所示,要将数据卸载到 Cloud Storage 桶,分两步执行:
- 第 1 步:
使用 COPY INTO <location> 命令将 Snowflake 数据库表中的数据复制到 Cloud Storage 桶中的一个或多个文件。在命令中,您指定引用 Cloud Storage 桶的命名外部暂存对象(推荐),或者您可以通过指定桶的 URI 和存储集成(如果需要)来选择直接卸载到桶。
无论您使用哪种方法,如果您手动或在脚本中执行命令,此步骤都需要运行一个当前虚拟仓库的会话。仓库提供计算资源来从表中写入行。
- 第 2 步:
使用 Google 提供的接口/工具从 Cloud Storage 桶下载文件。

本主题内容:
配置 Cloud Storage 以卸载数据¶
要让 Snowflake 写入 Cloud Storage 桶,您必须配置存储集成对象,将外部云存储的身份验证责任委托给 Snowflake Identity and Access Management (IAM) 实体。
有关配置说明,请参阅 配置 Google Cloud Storage 集成。
将数据卸载到外部暂存区¶
外部暂存区是指定的数据库对象,为数据卸载提供最大程度的灵活性。因为它们是数据库对象,所以可以将指定暂存区的权限授予任何角色。
您可以使用 Web 界面或 SQL 来创建指定的外部暂存区:
- Classic Console:
- SQL:
创建命名暂存区¶
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.
以下示例使用名为 unload
的文件夹路径来创建名为 my_ext_unload_stage
的外部暂存区。该暂存区引用以下对象:
A named storage integration called
gcs_int
. For instructions, see 配置 Google Cloud Storage 集成.A named file format called
my_csv_unload_format
. For instructions, see 准备卸载数据.CREATE OR REPLACE STAGE my_ext_unload_stage URL='gcs://mybucket/unload' STORAGE_INTEGRATION = gcs_int FILE_FORMAT = my_csv_unload_format;
将数据卸载到命名暂存区¶
利用外部暂存,用 COPY INTO <location> 命令将表上数据卸载到 Cloud Storage 桶。
以下示例使用
my_ext_unload_stage
暂存区将mytable
表中的所有行卸载到 Cloud Storage 桶中的一个或多个文件。将d1
文件名前缀应用于文件:COPY INTO @my_ext_unload_stage/d1 FROM mytable;
使用 Cloud Storage 提供的工具从桶中检索对象(即命令生成的文件)。
将数据直接卸载到 Cloud Storage 桶中¶
使用 COPY INTO <location> 命令将表中数据直接卸载到指定的 Cloud Storage 桶中。当您不计划使用相同的表和桶参数定期卸载数据时,此选项非常适合临时卸载。
您必须指定 Cloud Storage 桶的 URI,以及访问桶所需的存储集成。
以下示例将
mytable
表中的所有行卸载到 Cloud Storage 桶中文件夹路径前缀为unload/
的一个或多个文件:COPY INTO 'gcs://mybucket/unload/' FROM mytable STORAGE_INTEGRATION = gcs_int;
使用 Cloud Storage 控制台(或同等客户端应用程序)从桶中检索对象(即命令生成的文件)。