[英]Strategies for unloading massive amounts of data from one Redshift table to another?
我的公司每个月(大约100亿行左右)收集有关服务器使用情况的大量数据。 我的任务是将数据从此初始表中卸载到S3
,然后在其中将其复制到另一个群集中的表中。 然后,此数据将用于Tableau
仪表板报告。
我遇到的问题是,卸载(在某种程度上来说是复制)步骤间歇性地失败,并出现诸如Unexpected error: The server is already closed.
这使我认为这实际上是超时的。 还有一种奇怪的行为,它在卸载步骤中搅动并挂起,失败后,我可以看到它已将所有数据和清单文件卸载到了存储桶中。
由于存在所有这些不确定因素,我不得不寻找其他可能稍微分散任务的策略。 我对Spark
非常感兴趣,目前正在使用pyspark
了解它,并且想知道我是否可以通过某种方式缓解分布式处理的问题。 是否可以仅将数据存储在ec2
并让Tableau从那里拉出? 是否有某种方式可以分配卸载过程?
我将在下面的过程中添加代码,以便在遇到瓶颈时纠正该问题:
from datetime import datetime
import logging
import boto3
import psycopg2 as ppg2
from inst_utils import aws, misc_utils
from inst_config import config3
if __name__ == '__main__':
logger = misc_utils.initialize_logger(config3.REQUESTS_USAGE_LOGFILE)
# Unload step
timestamp = datetime.now()
month = timestamp.month
year = timestamp.year
s3_sesh = boto3.session.Session(**config3.S3_INFO)
s3 = s3_sesh.resource('s3')
fname = 'load_{}_{:02d}'.format(year, month)
bucket_url = ('canvas_logs/agg_canvas_logs_user_agent_types/'
'{}/'.format(fname))
unload_url = ('s3://{}/{}'.format(config3.S3_BUCKET, bucket_url))
s3.Bucket(config3.S3_BUCKET).put_object(Key=bucket_url)
table_name = 'requests_{}_{:02d}'.format(year, month - 1)
logger.info('Starting unload.')
try:
with ppg2.connect(**config3.REQUESTS_POSTGRES_INFO) as conn:
cur = conn.cursor()
# TODO add sql the sql folder to clean up this program.
unload = r'''
unload ('select
user_id
,course_id
,request_month
,user_agent_type
,count(session_id)
,\'DEV\' etl_requests_usage
,CONVERT_TIMEZONE(\'MST\', getdate()) etl_datetime_local
,\'agg_canvas_logs_user_agent_types\' etl_transformation_name
,\'N/A\' etl_pdi_version
,\'N/A\' etl_pdi_build_version
,null etl_pdi_hostname
,null etl_pdi_ipaddress
,null etl_checksum_md5
from
(select distinct
user_id
,context_id as course_id
,date_trunc(\'month\', request_timestamp) request_month
,session_id
,case
when user_agent like \'%CanvasAPI%\' then \'api\'
when user_agent like \'%candroid%\' then \'mobile_app_android\'
when user_agent like \'%iCanvas%\' then \'mobile_app_ios\'
when user_agent like \'%CanvasKit%\' then \'mobile_app_ios\'
when user_agent like \'%Windows NT%\' then \'desktop\'
when user_agent like \'%MacBook%\' then \'desktop\'
when user_agent like \'%iPhone%\' then \'mobile\'
when user_agent like \'%iPod Touch%\' then \'mobile\'
when user_agent like \'%iPad%\' then \'mobile\'
when user_agent like \'%iOS%\' then \'mobile\'
when user_agent like \'%CrOS%\' then \'desktop\'
when user_agent like \'%Android%\' then \'mobile\'
when user_agent like \'%Linux%\' then \'desktop\'
when user_agent like \'%Mac OS%\' then \'desktop\'
when user_agent like \'%Macintosh%\' then \'desktop\'
else \'other_unknown\'
end as user_agent_type
from {}
where context_type = \'Course\')
group by
user_id
,course_id
,request_month
,user_agent_type')
to '{}'
credentials 'aws_access_key_id={};aws_secret_access_key={}'
manifest
gzip
delimiter '|'
'''.format(
table_name, unload_url, config3.S3_ACCESS, config3.S3_SECRET)
cur.execute(unload)
conn.commit()
except ppg2.Error as e:
logger.critical('Error occurred during transaction: {}'.format(e))
raise Exception('{}'.format(e))
logger.info('Starting copy process.')
schema_name = 'ods_canvas_logs'
table_name = 'agg_canvas_logs_user_agent_types'
manifest_url = unload_url + 'manifest'
logger.info('Manifest url: {}'.format(manifest_url))
load = aws.RedshiftLoad(schema_name,
table_name,
manifest_url,
config3.S3_INFO,
config3.REDSHIFT_POSTGRES_INFO_PROD,
config3.REDSHIFT_POSTGRES_INFO,
safe_load=True,
truncate=True
)
load.execute()
FWIW,我想您可能应该发布另一个问题,其中包含您尝试过的UNLOAD
详细信息。
我发现在卸载整个表(例如不使用查询)时, UNLOAD
的工作要好得多 。
尝试使用要卸载的数据子集创建一个临时表,然后UNLOAD
整个表,然后删除该临时表。
CREATE TEMP TABLE a AS SELECT b FROM c WHERE d = e;
UNLOAD (SELECT * FROM a) TO 's3://bucket' CREDENTIALS … ;
DROP TABLE a;
关于您上面的实际问题 ,我认为您使用这种方法不会取得太大的成功。 瓶颈将不是Spark或Python,而是简单地说Redshift根本不是为返回大量行而设计的。
我同意@Jim Nasby-DISTINCT的GROUP BY是多余的,并且也很可能是造成麻烦的原因,因为它们迫使Redshift在复制前在单个Leader节点上执行整个数据集的整理。
Redshift的COPY命令的巨大好处是,如果查询允许,每个节点都可以与其他节点并行卸载自己的数据。 因此,如果您有10个节点,则所有10个节点都可以创建S3连接(多个),并开始抽取数据。
在您的情况下,通过具有此DISTINCT,您实际上将其禁用了,因为首先需要重新计算所有数据。
因此,我会与其他人一起说,最好是按原样转储整个表(这将更快并且对集群产生更少的负担),或者根据日期范围进行简单的增量式上载,可能有一些其他简单的条件(例如您有context_type = \\'Course\\')
。 只要没有GROUP BY / DISTINCT / ORDER BY都可以并行运行且非常快。
使用Spark并没有什么不同,因为它只会先通过SQL连接泵送数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.