【发布时间】:2019-05-16 05:29:05
【问题描述】:
我正在尝试使用 EMR 对按日期分区的 EXTERNAL 表运行查询,其中dt 分区的格式为YYYYmmdd i.e: 20190121。
CREATE EXTERNAL TABLE `my_schema`.`tracking_table`(
`id` string,
`active_bitmap` string)
PARTITIONED BY (
`dt` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
'quoteChar'='\"',
'separatorChar'='\t')
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
's3://bucket/trackingtable'
我创建了一个简单的脚本,将结果(以制表符分隔并以 gzip 压缩)插入到我的 S3 存储桶中。
set hive.cli.print.header=true;
set mapred.output.compress=true;
set hive.exec.compress.output=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
INSERT OVERWRITE DIRECTORY '${OUTPUT}/dt={:start_date}/'
select if(b.id is null,a.id,b.id) as id
,if(b.days_active is null, 1, (shiftleft(CAST(b.days_active AS BIGINT),1))|if(a.is_active is null,0,1) ) as active_bitmap
,'{:start_date}' as dt_partition
from(
select id,
if(count(1) > 0, 1, NULL) as is_active
from my_schema.activity_table where dt='{:start_date}' group by id
)a
full outer join(
select * from my_schema.tracking_table where dt='{:start_date-1}'
)b on a.id=b.id;
我在 HIVE 控制台上测试了我的脚本,将 ${OUTPUT}, {:start_date} 和 {:start_date-1} 参数替换为值,它工作正常,我可以在我的 S3 输出存储桶中看到结果被压缩和分隔。
现在,我想以编程方式为去年的数据运行此脚本。如何将日期参数传递给我的 EMR 步骤?我看到 EMR 上有一个参数部分,但我猜那是用于 EMR 集群的配置参数。
另外,{:start_date-1} 是否适用于我的日期格式,还是我需要将字符串日期解析为日期,减去一天并再次将其解析为字符串?
我正计划创建一个 python 脚本,该脚本需要一系列日期并将每个步骤提交到一个长时间运行的 EMR 集群,但我不知道如何将日期作为参数传递,而且我找不到任何教程如何轻松做到这一点。
【问题讨论】:
标签: amazon-web-services hive hiveql amazon-emr