如何解决通过 python 客户端插入的大查询中的重复字段错误?

how to solve repeated field error in big-query inserted through python client?

我正在使用 google api returns 数据 json 格式如下:

{
            "storageLocations": [
                "us"],      
            
"autoCreated": true,
            
"downloadBytes": "77557"

}

返回的字段之一是 storageLocations,它看起来像一个数组类型,所以我在 bigquery 中将其定义为重复字段。

我需要使用 python-big-query client 将此数据插入到 big-query 中,因此对于该字段,我在 big-query 中创建了以下结构。

为了创建每一行,我添加了一个空字段 row={} 并遍历 json 响应并将其分配为:

row["storageLocations.locations"]=response["storageLocations"]
  row["autoCreated"]= response["autoCreated"]

插入数据时

对于 storageLocations 我收到此错误

 u'insertErrors': [{u'index': 0, u'errors': [{u'debugInfo': u'', u'reason': u'invalid', u'message': u'no
     such field.', u'location': u'storageLocations.locations'}]}

我也试过 row["storageLocations"]["locations"] 但还是不行。

因为我是 python 的新手,我不确定大查询的错误。

感谢您的建议。

通常,当您使用重复记录时,您往往会有多个叶字段,或者您计划在将来添加更多。

您正在使用的 JSON 响应看起来您可能只想使用字符串数组。

例如:

schema=[
    bigquery.SchemaField("storageLocations", "STRING", mode="REPEATED"),
    bigquery.SchemaField("autoCreated", "BOOL"),
    bigquery.SchemaField("downloadBytes", "INT64"),
]

使用更简单的架构,您应该能够在插入行时简单地发送 list/tuple 个字符串值,例如row["storageLocations"] = ['a','b','c']

如果没有这个,您需要构造一个更复杂的行,正如您所发现的那样。