【发布时间】:2020-01-23 15:52:05
【问题描述】:
我正在尝试使用 Cloud Dataflow(Beam Python SDK)将其读取并写入 BigQuery。
读取和写入 2000 万条记录(约 80 MB)大约需要 30 分钟。
查看数据流 DAG,我可以看到将每个 CSV 行转换为 BQ 行花费了大部分时间。
下面是执行相同操作的代码 sn-p:
beam.Map(lambda s: data_ingestion.parse_record_string(s,data_ingestion.stg_schema_dict))
def parse_record_string(self, string_input,schema_dict):
for idx,(x,key) in enumerate(zip(imm_input,schema_dict)):
key = key.strip()
datatype = schema_dict[key].strip()
if key == 'HASH_ID' and datatype != 'STRING':
hash_id = hash(''.join(imm_input[1:idx]))
row_dict[key] = hash_id
else:
if x:
x = x.decode('utf-8').strip()
row_dict[key] = x
else:
row_dict[key] = None
#row_dict[key] = ''
return row_dict
除了 map transform 之外,我还使用了 ParDo 和 Flatmap。它们都产生相同的结果。
请提出任何可能的调整以减少时间。
提前致谢
【问题讨论】:
-
FlatMap 和 Map 均由 ParDo 提供支持。 Beam 在写入 BQ 时会自动从 Python Dicts 转换为 TableRow。
标签: python-2.7 google-cloud-platform profiling google-cloud-dataflow apache-beam