代码之家  ›  专栏  ›  技术社区  ›  zangw

Google云数据流:无法通过“WriteToBigQuery/BigQuerySink”向bigquery插入json数据`BigQueryDisposition.WRITE_截断`

  •  2
  • zangw  · 技术社区  · 7 年前

    给定如下数据集

    {"slot":"reward","result":1,"rank":1,"isLandscape":false,"p_type":"main","level":1276,"type":"ba","seqNum":42544}
    {"slot":"reward_dlg","result":1,"rank":1,"isLandscape":false,"p_type":"main","level":1276,"type":"ba","seqNum":42545}
    ...more type json data here
    

    我尝试过滤这些json数据并使用pythonsdk将它们插入bigquery,如下所示

    ba_schema = 'slot:STRING,result:INTEGER,play_type:STRING,level:INTEGER'
    
    class ParseJsonDoFn(beam.DoFn):
        B_TYPE = 'tag_B'
        def process(self, element):
            text_line = element.trip()
            data = json.loads(text_line)
    
            if data['type'] == 'ba':
                ba = {'slot': data['slot'], 'result': data['result'], 'p_type': data['p_type'], 'level': data['level']}
                yield pvalue.TaggedOutput(self.B_TYPE, ba)
    
    def run():
        parser = argparse.ArgumentParser()
        parser.add_argument('--input',
                          dest='input',
                          default='data/path/data',
                          help='Input file to process.')
        known_args, pipeline_args = parser.parse_known_args(argv)
        pipeline_args.extend([
          '--runner=DirectRunner',
          '--project=project-id',
          '--job_name=data-job',
        ])
        pipeline_options = PipelineOptions(pipeline_args)
        pipeline_options.view_as(SetupOptions).save_main_session = True
        with beam.Pipeline(options=pipeline_options) as p:
            lines = p | ReadFromText(known_args.input)
    
            multiple_lines = (
                lines
                | 'ParseJSON' >> (beam.ParDo(ParseJsonDoFn()).with_outputs(
                                          ParseJsonDoFn.B_TYPE)))
    
            b_line = multiple_lines.tag_B
            (b_line
                | "output_b" >> beam.io.WriteToBigQuery(
                                              'temp.ba',
                                              schema = B_schema,
                                              write_disposition = beam.io.BigQueryDisposition.WRITE_TRUNCATE,
                                              create_disposition = beam.io.BigQueryDisposition.CREATE_IF_NEEDED
                                            ))
    

    INFO:root:finish <DoOperation output_b/WriteToBigQuery output_tags=['out'], receivers=[ConsumerSet[output_b/WriteToBigQuery.out0, coder=WindowedValueCoder[FastPrimitivesCoder], len(consumers)=0]]>
    DEBUG:root:Successfully wrote 2 rows.
    

    看来这两个数据 type:ba 已插入到bigquery表中 temp.ba

    select * from `temp.ba`  limit 100;
    

    此表中没有数据

    我的密码有什么问题吗?或者我遗漏了什么?


    更新

    谢谢@Eric Schmidt回答,我知道初始数据可能会有些滞后。但是,运行上述脚本5分钟后,会出现

    enter image description here

    当我试图移除 write_disposition = beam.io.BigQueryDisposition.WRITE_TRUNCATE BigQuerySink

        | "output_b" >> beam.io.Write(
                          beam.io.BigQuerySink(
                              table = 'ba',
                              dataset = 'temp',
                              project = 'project-id',
                              schema = ba_schema,
                              #write_disposition = beam.io.BigQueryDisposition.WRITE_TRUNCATE,
                              create_disposition = beam.io.BigQueryDisposition.CREATE_IF_NEEDED
                            )
                        ))
    

    表格信息是

    enter image description here

    也许我不明白 然而。有人能告诉我更多信息吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Eric Schmidt    7 年前

    需要考虑两件事:

    1) 直接(本地)运行器使用流式插入。存在初始数据可用性滞后 see this post

    2) 确保您完全符合您正在流式处理的项目。使用BigQuerySink() project=“foo”,dataset=“bar”,table=“biz”。