【发布时间】:2022-01-03 04:48:06
【问题描述】:
我目前正在 GCP Dataflow 中构建 PoC Apache Beam 管道。在这种情况下,我想使用来自 PubSub 的主要输入和来自 BigQuery 的侧输入来创建流式管道,并将处理后的数据存储回 BigQuery。
侧流水线代码
side_pipeline = (
p
| "periodic" >> PeriodicImpulse(fire_interval=3600, apply_windowing=True)
| "map to read request" >>
beam.Map(lambda x:beam.io.gcp.bigquery.ReadFromBigQueryRequest(table=side_table))
| beam.io.ReadAllFromBigQuery()
)
带有侧输入代码的功能
def enrich_payload(payload, equipments):
id = payload["id"]
for equipment in equipments:
if id == equipment["id"]:
payload["type"] = equipment["type"]
payload["brand"] = equipment["brand"]
payload["year"] = equipment["year"]
break
return payload
主管道代码
main_pipeline = (
p
| "read" >> beam.io.ReadFromPubSub(topic="projects/my-project/topics/topiq")
| "bytes to dict" >> beam.Map(lambda x: json.loads(x.decode("utf-8")))
| "transform" >> beam.Map(transform_function)
| "timestamping" >> beam.Map(lambda src: window.TimestampedValue(
src,
dt.datetime.fromisoformat(src["timestamp"]).timestamp()
))
| "windowing" >> beam.WindowInto(window.FixedWindows(30))
)
final_pipeline = (
main_pipeline
| "enrich data" >> beam.Map(enrich_payload, equipments=beam.pvalue.AsIter(side_pipeline))
| "store" >> beam.io.WriteToBigQuery(bq_table)
)
result = p.run()
result.wait_until_finish()
将其部署到 Dataflow 后,一切看起来都很好,没有错误。但后来我注意到enrich data 步骤有两个节点而不是一个。
此外,如您所见,侧面输入卡在输入集合中具有 21 个计数的 Elements Added 和输出集合中的 Elements Added 中的 - 值。
您可以找到完整的管道代码 here 和模拟 pubsub 发布者 here
我已经按照这些文档中的所有说明进行操作:
- https://beam.apache.org/documentation/patterns/side-inputs/
- https://beam.apache.org/releases/pydoc/2.35.0/apache_beam.io.gcp.bigquery.html
但仍然发现此错误。请帮我。谢谢!
【问题讨论】:
-
能否提供完整的python管道代码?所以我可以很容易地复制它。
-
@ewertonvsilva 嗨,我已经添加了完整代码的链接,谢谢你的帮助!
-
我花了一些时间在它上面,但它仍然没有在最后一个阶段写,接缝一些与窗口有关的东西。但是,我可能会在其他部分遇到一些错误。 1.将此步骤修复为:
"timestamp" >> beam.Map(lambda src: window.TimestampedValue( # src,src["timestamp"])),2.在阅读主题时使用订阅:"read" >> beam.io.ReadFromPubSub(subscription=INPUT_SUBSCRIPTION)。修复这些并检查它是否可以帮助您前进。 -
@ewertonvsilva 非常感谢兄弟!我会检查的。
标签: python google-cloud-dataflow apache-beam