简体   繁体   English

BigQuery 架构表到 json 和 Python

[英]BigQuery schema table to json with Python

I need to have a Python equivalence of this BigQuery bq show --format=prettyjson myproject:mydataset.mytable .我需要这个 BigQuery bq show --format=prettyjson myproject:mydataset.mytable的 Python 等价物。

Is there a way to do it with the BigQuery API in Python?有没有办法使用 Python 中的 BigQuery API 来做到这一点?

I tried this in Python:我在 Python 中试过这个:

view_ref = self._client.dataset(dataset.dataset_id).table(table.table_id)
table_obj = self._client.get_table(view_ref)

dict_schema = []
for schema_field in table_obj.schema:
    dict_schema.append({
        'name': schema_field.name,
        'mode': schema_field.mode,
        'type': schema_field.field_type
   })

It almost works;它几乎可以工作; I just don't have the nested schema field/我只是没有嵌套模式字段/

Thanks for replies and have a nice day.感谢您的回复,祝您有美好的一天。

You can get convert your table schema to json simply using the schema_to_json() method.只需使用schema_to_json()方法,您就可以将表模式转换为 json。 It needs two attributes, schema_list and destination , respectively.它分别需要两个属性schema_listdestination

I exemplified your case using a public dataset with nested data and used StringIO() just to show how the schema will be.我使用带有嵌套数据的公共数据集来举例说明您的案例,并使用StringIO()来展示架构的样子。

from google.cloud import bigquery
import io

client = bigquery.Client()

project = 'bigquery-public-data'
dataset_id = 'samples'
table_id = 'shakespeare'

dataset_ref = client.dataset(dataset_id, project=project)
table_ref = dataset_ref.table(table_id)
table = client.get_table(table_ref)


f = io.StringIO("")
client.schema_to_json(table.schema, f)
print(f.getvalue())

And the output:和 output:

[
  {
    "description": "A single unique word (where whitespace is the delimiter) extracted from a corpus.",
    "mode": "REQUIRED",
    "name": "word",
    "type": "STRING"
  },
  {
    "description": "The number of times this word appears in this corpus.",
    "mode": "REQUIRED",
    "name": "word_count",
    "type": "INTEGER"
  },
  {
    "description": "The work from which this word was extracted.",
    "mode": "REQUIRED",
    "name": "corpus",
    "type": "STRING"
  },
  {
    "description": "The year in which this corpus was published.",
    "mode": "REQUIRED",
    "name": "corpus_date",
    "type": "INTEGER"
  }
]

Which is the same as the output displayed when using the command :bq show --format=prettyjson bigquery-public-data.samples.wikipedia | jq '.schema.fields'这与使用命令时显示的 output 相同:bq show --format=prettyjson bigquery-public-data.samples.wikipedia | jq '.schema.fields' :bq show --format=prettyjson bigquery-public-data.samples.wikipedia | jq '.schema.fields'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM