如何使用 API 存储在 Google Cloud Storage 中的架构文件在 BigQuery 加载作业上设置架构?

Pab*_*o H 5 google-cloud-storage google-bigquery google-cloud-platform

我创建了一个 python 脚本来从 Google Cloud Storage 存储桶中获取 JSON 文件并将其加载到数据集中。我在尝试指定与文本文件位于同一存储桶中的架构时遇到问题

我的架构文件是一个 txt 文件,格式如下Attribute:DataType,Attribute:DataType

这就是我所拥有的

job_config = bigquery.LoadJobConfig()

schema_uri = 'gs://<bucket-name>/FlattenedProduct_schema.txt'
schema = schema_uri
job_config.schema = schema    
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
uri = 'gs://<bucket-name>/FlattenedProduct_JSON.txt'

load_job = client.load_table_from_uri(
    uri,
    dataset_ref.table('us_states'),
    location='US',  # Location must match that of the destination dataset.
    job_config=job_config)  # API request
Run Code Online (Sandbox Code Playgroud)

Ngu*_*Tín 1

您需要自己读取文本文件并将其转换为schema所需的格式,即目标表的 List[google.cloud.bigquery.schema.SchemaField] \xe2\x80\x93 Schema。

\n\n

所需架构的示例:

\n\n
from google.cloud.bigquery import SchemaField\n\nschem = [\n     SchemaField(\'field1\',\'STRING\'),\n     SchemaField(\'field2\', \'INTEGER\'),\n     SchemaField(\'value\', \'FLOAT\')\n    ]\n
Run Code Online (Sandbox Code Playgroud)\n

  • 如何从 .json 文件构建它? (2认同)