You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Snowflake中超大规模(20亿+)数据表下载至本地备份?

解决Snowflake大表直接导出CSV到本地的方案

你的问题核心在于:Worksheet中运行的Python代码是在Snowflake的服务器端执行的,生成的CSV文件会保存在Snowflake的临时计算节点上,无法直接传输到你的本地系统。下面是几种可行的解决方案:

方案1:在本地运行Python脚本直接导出

直接在本地机器上编写并运行Python脚本,通过Snowflake驱动连接数据库,查询数据后直接写入本地CSV文件。这种方式不需要依赖Snowflake的Worksheet,数据全程在本地处理,还能避免大内存占用问题。

修改后的本地运行代码示例

import snowflake.snowpark as snowpark
import pandas as pd
from datetime import datetime, timedelta

# 替换为你的Snowflake连接信息
connection_params = {
    "account": "你的账户标识",
    "user": "你的用户名",
    "password": "你的密码",
    "warehouse": "使用的计算仓库名",
    "database": "目标数据库名",
    "schema": "目标schema名"
}

def main():
    # 建立本地到Snowflake的会话连接
    session = snowpark.Session.builder.configs(connection_params).create()

    end_date = datetime(2024, 3, 1)
    current_date = datetime(2022, 2, 1)

    while current_date < end_date:
        start_month = current_date.strftime('%Y-%m-%d')
        # 计算下个月第一天
        next_month = (current_date + timedelta(days=32)).replace(day=1).strftime('%Y-%m-%d')

        query = f"""
        SELECT * 
        FROM my_table 
        WHERE email_created_at >= '{start_month}' AND email_created_at < '{next_month}'
        """
    
        filename = f"myfile_{current_date.strftime('%Y_%m')}.csv"
        # 迭代写入避免内存溢出(适合单月数据量较大的情况)
        with open(filename, 'w', newline='', encoding='utf-8') as csv_file:
            # 获取表头
            columns = [col.name for col in session.sql(query).columns]
            # 写入第一行表头
            pd.DataFrame([columns]).to_csv(csv_file, index=False, header=False)
            
            # 分批读取并写入数据
            chunk_size = 10000
            chunk = []
            for row in session.sql(query).iter_rows():
                chunk.append(row)
                if len(chunk) == chunk_size:
                    pd.DataFrame(chunk, columns=columns).to_csv(csv_file, index=False, header=False)
                    chunk = []
            # 写入剩余的最后一批数据
            if chunk:
                pd.DataFrame(chunk, columns=columns).to_csv(csv_file, index=False, header=False)
        
        print(f"{current_date.strftime('%Y-%m')}数据已保存到本地文件:{filename}")
        
        # 切换到下一个月
        current_date = (current_date + timedelta(days=32)).replace(day=1)
    
    session.close()

if __name__ == "__main__":
    main()

方案2:使用SnowSQL命令行工具分块导出

SnowSQL是Snowflake官方命令行客户端,支持直接将查询结果导出到本地文件,无需编写复杂脚本,适合大规模数据的批量导出。

操作步骤

  1. 安装并配置SnowSQL(完成账户连接配置)
  2. 运行以下Shell脚本分月导出(替换占位符为你的实际信息):
#!/bin/bash
# 定义时间范围
start_year=2022
start_month=2
end_year=2024
end_month=2

current_year=$start_year
current_month=$start_month

while [ $current_year -lt $end_year ] || ([ $current_year -eq $end_year ] && [ $current_month -le $end_month ]); do
    # 格式化日期参数
    start_date=$(printf "%04d-%02d-01" $current_year $current_month)
    # 计算下一个月
    if [ $current_month -eq 12 ]; then
        next_year=$((current_year + 1))
        next_month=1
    else
        next_year=$current_year
        next_month=$((current_month + 1))
    fi
    next_date=$(printf "%04d-%02d-01" $next_year $next_month)
    
    # 执行导出命令
    snowsql -c 你的连接名 -d 目标数据库 -s 目标schema -w 计算仓库 -q "SELECT * FROM my_table WHERE email_created_at >= '$start_date' AND email_created_at < '$next_date';" -o output_file="myfile_${current_year}_${current_month}.csv" -o header=true -o field_optionally_enclosed_by='"' -o field_delimiter=','
    
    echo "导出完成:myfile_${current_year}_${current_month}.csv"
    
    # 更新月份变量
    current_year=$next_year
    current_month=$next_month
done

方案3:通过COPY INTO导出到云存储再同步到本地

针对20亿条的超大规模数据,直接导出到本地效率较低时,可以先将数据导出到云存储(如AWS S3、Azure Blob),再通过云同步工具下载到本地。

示例(导出到AWS S3)

  1. 提前完成Snowflake与S3的存储集成配置(配置权限与外部阶段)
  2. 运行SQL分月导出:
-- 设置当月时间范围
SET start_date = '2022-02-01';
SET next_date = '2022-03-01';

-- 导出到S3外部阶段
COPY INTO @my_s3_stage/myfile_2022_02.csv
FROM (SELECT * FROM my_table WHERE email_created_at >= $start_date AND email_created_at < $next_date)
FILE_FORMAT = (TYPE = CSV FIELD_OPTIONALLY_ENCLOSED_BY = '"');
  1. 使用AWS CLI将S3文件同步到本地:
aws s3 sync s3://你的存储桶路径/ 本地目标文件夹路径/

内容的提问来源于stack exchange,提问作者Aurangzeb Rathore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 08:04:55