管理常规数据加载

本主题提供管理常规数据加载的最佳实践、一般准则和重要注意事项。

对暂存数据文件进行分区

在规划定期数据加载(例如 ETL [提取、转换、加载])进程或定期导入计算机生成的数据时,请务必使用逻辑粒度路径在内部(即 Snowflake)暂存区或外部位置(S3 桶或 Azure 容器)中对数据进行分区。创建一个分区结构,其中包括标识详细信息(如应用程序或位置)以及写入数据的日期。然后,您可以使用单个命令将分区数据的任何部分复制到 Snowflake 中。您可以按最初填充表时的小时、日、月甚至年将数据复制到 Snowflake 中。

使用路径的分区 S3 桶的一些示例:

s3china://bucket_name/application_one/2016/07/01/11/s3china://bucket_name/application_two/location_one/2016/07/01/14/

其中:

application_one , application_two , location_one , etc.

标识路径中所有数据源的详细信息。数据可按编写日期整理。可选的 24 小时目录减少了每个目录中的数据量。

Note

S3 传输 Snowflake 使用的每个 COPY 语句的目录列表,因此减少每个目录中的文件数量可以提高 COPY 语句的表现。您甚至可以考虑每小时在文件夹中创建 10-15 分钟增量的子文件夹。

同样,也可以在内部暂存区暂存文件时添加路径。例如:

PUT file:///tmp/file_20160701.11*.csv @my_stage/<application_one>/<location_one>/2016/07/01/11/;

加载暂存数据

Load organized data files into Snowflake tables by specifying the precise path to the staged files. For more information, see Organizing data by path.

移除加载的数据文件

成功加载暂存文件中的数据后,请考虑移除暂存文件,以确保不会再次无意中加载数据(重复)。

Note

Do not remove the staged files until the data has been loaded successfully. To check if the data has been loaded successfully, use the COPY_HISTORY command. Check the STATUS column to determine if the data from the file has been loaded. Note that if the status is Load in progress, removing the staged file can result in partial loads and data loss.

Staged files can be deleted from a Snowflake stage (user stage, table stage, or named stage) using the following methods:

  • Files that were loaded successfully can be deleted from the stage during a load by specifying the PURGE copy option in the COPY INTO <table> command.
  • After the load completes, use the REMOVE command to remove the files in the stage.

移除文件可确保不会无意中再次加载这些文件。这还能提高加载性能,因为它减少了 COPY 命令为验证暂存区中的现有文件是否已加载而必须扫描的文件数量。