簡體   English   中英

如何從json文件中獲取表架構:parse_table_schema_from_json?

[英]How to get table schema from json file: parse_table_schema_from_json?

我正在嘗試from apache_beam.io.gcp.bigquery import parse_table_schema_from_json使用parse_table_schema_from_json獲取表架構,從此處 from apache_beam.io.gcp.bigquery import parse_table_schema_from_json

這是我的代碼:

 def getSchema(pathToJSON):
    with open(pathToJSON) as data_file:
        schema_data = json.dumps(json.load(data_file))
    table_schema = parse_table_schema_from_json(schema_data)
    # print(table_schema)
    return table_schema

這是我得到的錯誤:

    Traceback (most recent call last):
  File "test_get_schema.py", line 16, in <module>
    getSchema("vauto_table_schema.json")
  File "test_get_schema.py", line 13, in getSchema
    table_schema = parse_table_schema_from_json(schema_data)
  File "/home/usr/.local/lib/python2.7/site-packages/apache_beam/io/gcp/bigquery.py", line 269, in parse_table_schema_from_json
    fields = [_parse_schema_field(f) for f in json_schema['fields']]
TypeError: list indices must be integers, not str

我的json文件如下所示:

[
  {
    "name": "StockNumber",
    "type": "INTEGER",
    "mode": "NULLABLE"
  },
  {
    "name": "Product",
    "type": "STRING",
    "mode": "NULLABLE"
  }
]

我想念什么?

您需要字典而不是列表。 修改您的函數和架構文件,如下所述,然后重試

  def getSchema(pathToJSON):
    schema_data = json.dumps(json.load(open("mapping.json")))
    table_schema = parse_table_schema_from_json(schema_data)
    # print(table_schema)
    return table_schema

您的架構文件應為

{
  "fields": [
   {
    "type": "INTEGER",
    "name": "StockNumber",
    "mode": "NULLABLE"
  },
  {
    "type": "STRING",
    "name": "Product",
    "mode": "NULLABLE"
  }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM