Как получить схему таблицы из файла json: parse_table_schema_from_json? - PullRequest
0 голосов
/ 14 декабря 2018

Я пытаюсь получить схему таблицы, используя 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"
  }
]

Чего мне не хватает?

1 Ответ

0 голосов
/ 17 декабря 2018

Вам нужен dict вместо списка.Измените вашу функцию и файл схемы, как указано ниже, и попробуйте снова

  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"
  }
  ]
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...