在数据流管道中动态设置 bigquery 数据集

Dynamically set bigquery dataset in dataflow pipeline

我可以根据我在之前的数据流步骤中处理的数据将数据插入不同的 bigQuery 数据集吗?


我正在创建数据流管道,它正在从 PubSub 订阅读取数据并写入大查询 table。定义如下:

def run(argv=None, save_main_session=True):
    options: PipelineOptions = PipelineOptions(
        project='project-id',
        runner='DataflowRunner',
        region='region',
        streaming=True,
        setup_file='dataflow/setup.py',
        autoscaling_algorithm='THROUGHPUT_BASED',
        job_name='telemetry-processing'
    )

    with beam.Pipeline(options=options) as p:
        status = (
                p
                 | 'Get Status PubSub' >> beam.io.ReadFromPubSub(
            subscription='projects/project-id/subscriptions/subscription-id',
            with_attributes=True))

        status_records = (status| 'Proto to Dict' >> beam.Map(lambda x: 
convert_proto_to_dict(x, nozzle_status_proto.NozzleStatus)) )

        status_records | 'Write status to BQ' >> beam.io.WriteToBigQuery('project- 
id:dataset-id.table-id')

         bytes_status = (status | 'Get Bytes Result' >> beam.ParDo(GetBytes()))
         bytes_status | 'Write to BQ BackUp' >> beam.io.WriteToBigQuery(
        'project-id:dataset-id.backup-table-id')

对于给定的输入和输出,它完全按照预期工作。
我想要的是,关于我的 PubSubMessage 中的特定属性,定义我的消息应该发送到哪个数据集。 所以我需要更改的部分是这个:

status_records | 'Write status to BQ' >> beam.io.WriteToBigQuery('project-id:dataset-id.table-id')

我已经尝试提取所需的数据并像这样使用它:

status_records | 'Write status to BQ' >> beam.io.WriteToBigQuery('project-id:{data-from-previous-step}.table-id')

但是我们不能直接从 PCollection 中获取数据。

我试图像这样覆盖 WriteToBigQuery post () 但我没有收到任何错误,也没有插入任何内容。

我不知道如何实现。
你知道我应该从哪里开始做这件事吗?
我是否必须为 n 个数据集创建 n 个管道?

WriteToBigQuery 的“table”参数可以是从元素到应该写入的table 的函数。例如:

status_records | 'Write' >> beam.io.WriteToBigQuery(
  lambda e: 'dataset1.invalid_records' if is_invalid(e) else 'dataset2.good_records')