【问题标题】:Bigquery API: How to provide schema to load_table_from_storage callBigquery API:如何为 load_table_from_storage 调用提供架构
【发布时间】:2017-06-05 15:23:33
【问题描述】:

使用load_table_from_storage 将托管在 Google Cloud Storage 上的 json 上传到 Bigquery 时,指定架构的最佳方式是什么?

字段列表非常复杂且复杂,我已经拥有如下格式: https://cloud.google.com/bigquery/docs/personsDataSchema.json

有什么方法可以在 Python 中以这种格式提供模式?如果是的话,我应该使用什么语法?我尝试了各种选项,到目前为止都没有奏效。

【问题讨论】:

    标签: python google-bigquery google-cloud-storage


    【解决方案1】:

    注意load_table_from_storagedestination 输入中接收到一个google.cloud.bigquery.table.Table 类的对象。这是您应该指定架构的地方。

    例如,如果“bqc”是您的 BigQuery 客户端对象,则会创建一个 Table 对象:

    ds = bqc.dataset('dataset_name')
    table = ds.table('table_name')
    

    现在假设您的 json 文件中有这些数据可供使用:

    {"user_id": "1", "visitid": 1, "hits": [{"hitNumber": 1, "type": "PAGE"}, {"hitNumber": 2, "type": "PAGE"}]}
    {"user_id": "2", "visitid": 1, "hits": [{"hitNumber": 1, "type": "EVENT"}, {"hitNumber": 2, "type": "PAGE"}]}
    

    然后定义其架构如下:

    from google.cloud.bigquery.schema import SchemaField
    f1 = SchemaField('user_id', 'STRING')
    f2 = SchemaField('visitid', 'INTEGER')
    f3 = SchemaField('hits', 'RECORD', mode='REPEATED', fields=[SchemaField('hitNumber', 'INTEGER'), SchemaField('type', 'STRING')])
    
    table.schema = [f1, f2, f3]
    table.create()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2017-03-17
      相关资源
      最近更新 更多