【问题标题】:Load JSON data into BigQuery table将 JSON 数据加载到 BigQuery 表中
【发布时间】:2017-08-28 20:38:37
【问题描述】:

我正在尝试通过以下方式将简单的 JSON 数据加载到 BigQuery 表中:

$ bq load \
    --apilog \
    --source_format=NEWLINE_DELIMITED_JSON \
    my_dataset.my_table \
    ./input.json ./schema.json

但收到以下错误消息:

Upload complete.
Waiting on bqjob_xxxx_xxx ... (3s) Current status: DONE
BigQuery error in load operation: Error processing job 'my_project_id:bqjob_xxxx_xxx': CSV table encountered too many errors, giving up. Rows: 1; errors: 1.
Failure details:
- file-00000000: Error detected while parsing row starting at
position: 0. Error: Data between close double quote (") and field
separator.

它抱怨一些 CSV 错误,但我正在尝试加载 JSON (--source_format=NEWLINE_DELIMITED_JSON)

我的input.json 包含以下数据:

{"domain":"stackoverflow.com","key":"hello","value":"world"}

我的schema.json 如下:

[
    {
        "name": "domain",
        "type": "string",
        "mode": "nullable"
    },
    {
        "name": "key",
        "type": "string",
        "mode": "nullable"
    },
    {
        "name": "value",
        "type": "string",
        "mode": "nullable"
    }
]

bq 2.0.25 版:

$ gcloud version | grep ^bq
bq 2.0.25

【问题讨论】:

    标签: json google-bigquery


    【解决方案1】:

    这里的问题是标志apilog 需要一个字符串作为输入。这个命令应该对你有用:

    bq load \
        --apilog '' \
        --source_format=NEWLINE_DELIMITED_JSON \
        my_dataset.my_table \
        ./input.json ./schema.json
    

    空字符串将输出发送到stdout。如果您想将日志保存到本地文件,则只需发送一个非空字符串,例如--apilog 'localfile_name'

    【讨论】:

      【解决方案2】:

      BQ 命令说:

      USAGE: bq.py [--global_flags] <command> [--command_flags] [args]
      

      如您所见,有 global_flagscommand_flags

      对于具有值的 global_flags,您需要使用等号:

      --flag=value
      

      command_flags 是布尔值:

      --[no]replace
      

      或者他们接受必须跟随标志的参数:

      --source_format NEWLINE_DELIMITED_JSON
      

      也不要混合全局和命令标志:apilog 是一个全局标志。 我会把你的命令改写成:

      $ bq --apilog load \
          --source_format NEWLINE_DELIMITED_JSON \
          my_dataset.my_table \
          ./input.json ./schema.json
      

      【讨论】:

      • 最好使用bq --apilog - load …bq --apilog '' load …,因为没有参数--apilog使用load作为其参数。
      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      相关资源
      最近更新 更多