【问题标题】:Python Apache Beam Datapiple to read multiple BQ tablesPython Apache Beam Datapiple 读取多个 BQ 表
【发布时间】:2019-03-13 16:31:51
【问题描述】:

我需要读取 BQ 表并应用一些转换并将其加载到另一个 BQ 表。转换对所有表都是通用的。

我只是想知道我们是否可以一次读取多个表并应用转换并加载到不同的目标表。源表和目标表的结构将相同。例如

table_x --transformation(abc) -- table_x1
table_y --transformation(abc) -- table_y1

下面是我用一个表测试的示例代码:

import apache_beam as beam
        from apache_beam.options.pipeline_options import PipelineOptions
        from apache_beam.io.gcp.bigquery import parse_table_schema_from_json

        def get_schema(table_in): 
            """
            Function to pull the json schema for the table being copied. The schema should be in the schema/ folder, and be of the format:
            'schema_"input_table_name".json'
            """
            with open('schema/schema_'+table_in+'.json') as f:
                        data = f.read()
                        # Wrapping the schema in fields is required for the BigQuery API.
                        table_schema = '{"fields": ' + data + '}'        
            return parse_table_schema_from_json(table_schema) # # This code reads from a biq query table and loads in to another table on BQ, transformation still need to looked at

        def run(argv=None):
            """
            Function that will instantiate the pipeline options, define the pipeline, and then run it
            """
            pipeline_options = PipelineOptions()
            p = beam.Pipeline(options=pipeline_options)    
            (p 
             | 'ReadTable' >> beam.io.Read(beam.io.BigQuerySource('projecttest:datasetx.table_x'))
             | 'Write to BigQuery' >> beam.io.Write(
                                         beam.io.BigQuerySink('projecttest:datasetx.table_x1',

    schema=get_schema('table_x'),
                                         write_disposition=beam.io.BigQueryDisposition.WRITE_TRUNCATE,
                                         create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED))
                                     )
            p.run().wait_until_finish()    

        if __name__ == '__main__':
            run()

【问题讨论】:

    标签: python google-bigquery apache-beam


    【解决方案1】:

    你有(总结)行:

     p | beam.io.Read(beam.io.BigQuerySource("x")
       | beam.io.Write(beam.io.BigQuerySink("x1")
    

    为什么不直接添加以下(摘要)行?

     p | beam.io.Read(beam.io.BigQuerySource("y")
       | beam.io.Write(beam.io.BigQuerySink("y1")
    

    【讨论】:

    • 嗨.. 感谢您的快速回复。我在一个模式中有大约 50 个表,还有更多的模式和表。我正在尝试通用数据管道。
    • 为什么这个答案不起作用?还有更多细节吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2018-08-11
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多