【问题标题】:How can I insert non-repeated records into BigQuery using the bigrquery library?如何使用 bigrquery 库将非重复记录插入 BigQuery?
【发布时间】:2019-10-02 21:33:55
【问题描述】:

我正在尝试将非重复记录插入 BigQuery,但一直收到错误 Array specified for non-repeated field: record.

我的问题是:如何使用 bigrquery 库将非重复记录插入 BigQuery?

如果我有以下架构:

bqSchema <- bq_fields(list(
  bq_field(name = "record", type = "RECORD", fields = list(
    bq_field(name = "a", type = "INTEGER"),
    bq_field(name = "b", type = "STRING")
  ))
))

还有这个数据框:

df <- tibble(
  record = list(
    a = 1,
    b = "B"
  )
)

如下插入数据会导致 BigQuery 出现错误:

bq_perform_upload(bqTableObj, df, fields = bqSchema)
# Array specified for non-repeated field: record

我认为这部分是因为 bigrquery converts the dataframe to JSONjsonlite::stream_out(),但不使用参数 auto_unbox = TRUE,导致数组,而不是对象。这会导致以下以换行符分隔的 JSON 被发送到 BigQuery:

{"record": [1]}
{"record": ["B"]}

我认为应该发送到 BigQuery 的正确 NDJSON 应该是:

{"record": {"a": 1, "b", "B"}}

以前有没有人遇到过这个问题,或者有什么想法可以解决这个问题?

【问题讨论】:

    标签: r bigrquery


    【解决方案1】:

    您应该在设置 mode = "REPEATED" 的地方尝试以下操作:

    bqSchema <- bq_fields(list(
      bq_field(name = "record", type = "RECORD", mode = "REPEATED",
               fields = list(bq_field(name = "a", type = "INTEGER"),
                             bq_field(name = "b", type = "STRING")
                             )
               )
     ))
    

    【讨论】:

    • 感谢您的建议。但是,我要加载的数据格式不是重复的记录字段。感觉这个答案隐藏了错误而不是解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2015-04-14
    • 2021-02-02
    相关资源
    最近更新 更多