【问题标题】:Error while reading data, error message: JSON table encountered too many errors, giving up. Rows读取数据时出错,报错信息:JSON表遇到太多错误,放弃。行
【发布时间】:2022-10-21 18:54:58
【问题描述】:

我有两个文件并在 apache-beam 中使用 CoGroupByKey 进行内部连接。 当我向 bigquery 写入行时,我给了我以下错误。

RuntimeError: BigQuery job beam_bq_job_LOAD_AUTOMATIC_JOB_NAME_LOAD_STEP_614_c4a563c648634e9dbbf7be3a56578b6d_2f196decc8984a0d83dee92e19054ffb failed. Error Result: <ErrorProto
 location: 'gs://dataflow4bigquery/temp/bq_load/06bfafaa9dbb47338ad4f3a9914279fe/dotted-transit-351803.test_dataflow.inner_join/f714c1ac-c234-4a37-bf51-c725a969347a'
 message: 'Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details.'
 reason: 'invalid'> [while running 'WriteToBigQuery/BigQueryBatchFileLoads/WaitForDestinationLoadJobs']

- - - - - - - - -代码 - - - - - - - - - - - -

from apache_beam.io.gcp.internal.clients import bigquery
import apache_beam as beam

def retTuple(element):
  
  thisTuple=element.split(',')
  return (thisTuple[0],thisTuple[1:])

def jstr(cstr):
    import datetime

    
    left_dict=cstr[1]['dep_data']
    right_dict=cstr[1]['loc_data']
    for i  in left_dict:
    
        for j in right_dict:
            id,name,rank,dept,dob,loc,city=([cstr[0]]+i+j)
            
            json_str={ "id":id,"name":name,"rank":rank,"dept":dept,"dob":datetime.datetime.strptime(dob, "%d-%m-%Y").strftime("%Y-%m-%d").strip("'"),"loc":loc,"city":city }
    return json_str
            
table_spec = 'dotted-transit-351803:test_dataflow.inner_join'
table_schema = 'id:INTEGER,name:STRING,rank:INTEGER,dept:STRING,dob:STRING,loc:INTEGER,city:STRING'   
gcs='gs://dataflow4bigquery/temp/'
    
p1 = beam.Pipeline()

# Apply a ParDo to the PCollection "words" to compute lengths for each word.
dep_rows = ( 
                p1
                | "Reading File 1" >> beam.io.ReadFromText('dept_data.txt')
                | 'Pair each employee with key' >> beam.Map(retTuple)          # {149633CM : [Marco,10,Accounts,1-01-2019]}
    
               )


loc_rows = ( 
                p1
                | "Reading File 2" >> beam.io.ReadFromText('location.txt') 
                | 'Pair each loc with key' >> beam.Map(retTuple)                # {149633CM : [9876843261,New York]}
               )


results = ({'dep_data': dep_rows, 'loc_data': loc_rows} 
           
           | beam.CoGroupByKey()
           | beam.Map(jstr)
           |  beam.io.WriteToBigQuery(
               custom_gcs_temp_location=gcs,
            table=table_spec,
            schema=table_schema,
            write_disposition=beam.io.BigQueryDisposition.WRITE_TRUNCATE,
            create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
            additional_bq_parameters={'timePartitioning': {'type': 'DAY'}}
               
        )
          )




 
p1.run().wait_until_finish()

我正在使用数据流运行器在 gcp 上运行它。 打印 json_str 字符串时,输出是有效的 json。 例如: {'id':'149633CM','name':'Marco','rank':'10','dept':'Accounts','dob':'2019-01-31','loc':' 9204232778', '城市': '纽约'} {'id':'212539MU','name':'Rebekah','rank':'10','dept':'Accounts','dob':'2019-01-31','loc':' 9995440673','城市':'丹佛'}

我定义的模式也是正确的。 但是,在将其加载到 bigquery 时出现该错误。

【问题讨论】:

    标签: google-cloud-dataflow apache-beam apache-beam-io


    【解决方案1】:

    经过一番研究,我终于解决了。 这是一个架构错误。ID列值就像149633CM我给了数据类型ID作为INTEGER,但是当我尝试使用bq和模式加载json作为--autodetect时,bq标记的数据类型为ID 为 STRING。

    在那之后,我改变了数据类型ID 列为 STRING在我的代码架构中。 而且,它起作用了。该表已创建并已加载。

    但是,我没有得到一件事,如果开始 6 个字符是 Id 列中的数字,为什么 INTEGER 不起作用而 STEERING 起作用?

    【讨论】:

    • 感谢您提供答案,如果您还有其他问题,请创建一个新问题并在那里提问,而不是在您的答案中提问。
    【解决方案2】:

    因为数据类型是在整个字段值上解析的,而不仅仅是前 6 个字符。如果你去掉最后 2 个字符,你可以把 INTEGER

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多