Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将BigQuery模式表转换为json_Python_Google Bigquery - Fatal编程技术网

使用Python将BigQuery模式表转换为json

使用Python将BigQuery模式表转换为json,python,google-bigquery,Python,Google Bigquery,我需要这个BigQueryBQshow-format=prettyjson myproject:mydataset.mytable的Python等价物 有没有办法用Python中的BigQueryAPI实现这一点 我在Python中尝试了以下方法: view_ref = self._client.dataset(dataset.dataset_id).table(table.table_id) table_obj = self._client.get_table(view_ref) dict_

我需要这个BigQueryBQshow-format=prettyjson myproject:mydataset.mytable的Python等价物

有没有办法用Python中的BigQueryAPI实现这一点

我在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
   })
它几乎起作用了;我只是没有嵌套的模式字段/


感谢您的回复,祝您度过愉快的一天。

您可以使用该方法将表模式转换为json。它需要两个属性,分别是schema_list和destination

我用一个带有嵌套数据的公共数据集举例说明了您的案例,并仅用于说明模式将如何

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())
以及输出:

[
  {
    "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"
  }
]

这与使用命令时显示的输出相同!bq show-format=prettyjson bigquery公共数据:samples.wikipedia | jq.schema.fields'

您可以使用该方法将表模式转换为json。它需要两个属性,分别是schema_list和destination

我用一个带有嵌套数据的公共数据集举例说明了您的案例,并仅用于说明模式将如何

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())
以及输出:

[
  {
    "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"
  }
]
这与使用命令时显示的输出相同!bq show-format=prettyjson bigquery公共数据:samples.wikipedia | jq'.schema.fields'