【问题标题】:Bigquery fails to load data from Google Cloud StorageBigquery 无法从 Google Cloud Storage 加载数据
【发布时间】:2017-07-28 21:35:13
【问题描述】:

我有一个 json 记录,看起来像

{"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40",
 "line_item": [{"line":"1","sku":"10","amount":10},
               {"line":"2","sku":"20","amount":20}]}

我正在尝试将上述记录加载到具有架构定义的表中,

"fields": [
  {
    "mode": "NULLABLE",
    "name": "customer_id",
    "type": "STRING"
  },
  {
    "mode": "NULLABLE",
    "name": "order_id",
    "type": "STRING"
  },
  {
    "mode": "REPEATED",
    "name": "line_item",
    "type": "STRING"
  }
]

我收到以下错误“消息”:

文件中从位置 0 开始的行中的 JSON 解析错误: gs://gcs_bucket/file0.为非记录字段指定的 JSON 对象: line_item

我想要line_item json 字符串,它可以有超过 1 行作为表中行项目列中的 json 字符串数组。

有什么建议吗?

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    第一件事是你的输入 JSON 不应该有“\n”字符,所以你应该把它保存为:

    {"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40", "line_item": [{"line":"1","sku":"10","amount":10}, {"line":"2","sku":"20","amount":20}]}
    

    您的 JSON 文件应如下所示的一个示例:

    {"customer_id":"2349uslvn2q3","order_id":"9sufd23rdl40", "line_item": [{"line":"1","sku":"10","amount":10}, {"line":"2","sku":"20","amount":20}]}
    {"customer_id":"2","order_id":"2", "line_item": [{"line":"2","sku":"20","amount":20}, {"line":"2","sku":"20","amount":20}]}
    {"customer_id":"3","order_id":"3", "line_item": [{"line":"3","sku":"30","amount":30}, {"line":"3","sku":"30","amount":30}]}
    

    而且您的架构也不正确。应该是:

    [
      {
        "mode": "NULLABLE",
        "name": "customer_id",
        "type": "STRING"
      },
      {
        "mode": "NULLABLE",
        "name": "order_id",
        "type": "STRING"
      },
      {
        "mode": "REPEATED",
        "name": "line_item",
        "type": "RECORD",
        "fields": [{"name": "line", "type": "STRING"}, {"name": "sku", "type": "STRING"}, {"name": "amount", "type": "INTEGER"}]
      }
    ]
    

    为了更好地理解模式的工作原理,我尝试在 this answer 中编写指南。希望它可以有一些价值。

    如果您的数据内容保存在一个名为 gs://gcs_bucket/file0 的文件中,而您的架构在 schema.json 中,那么此命令应该适合您:

    bq load --source_format=NEWLINE_DELIMITED_JSON dataset.table gs://gcs_bucket/file0 schema.json
    

    (假设您正在使用 CLI 工具,因为您的问题似乎就是这种情况)。

    【讨论】:

    • 感谢您的建议!您在此处拥有的架构定义,我已经尝试过并且工作正常。我以前有过,我希望将整个 line_item 作为表中的 json 值数组而不是字符串数组。是否可以将整个订单项设置为 json?
    • 如果我们存储的记录类型格式不一致怎么办?我正在尝试以 json 格式存储地图。
    • @Shadoninja 我建议在 SO 上问这个。 IIUC 可能最好的方法是将您的数据直接保存为 json 字符串,然后从 BQ 处理它(您可能会在 SO 上得到更好的答案)。
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2017-11-13
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多