【问题标题】:How to insert a python dictionary data to google cloud bigquery如何将 python 字典数据插入谷歌云 bigquery
【发布时间】:2021-10-08 14:50:47
【问题描述】:

我正在尝试将 python 字典数据插入 bigbuery。

以下是我使用的数据

data = {
    'columnID':'123156',
    'deviceID':'156',
    'describle':{
        'name':'car',
        'freq':'10',
        'period':'3',
    }
}

我还在下面定义了 bigquery 表架构

table_schema = {
    'fields':[
        {'name':'columnID', 'type':'STRING', 'mode':'REQUIRED'},
        {'name':'deviceID', 'type':'STRING', 'mode':'REQUIRED'},
        {'name':'describle', 'type':'RECORD', 'mode':'NULLABLE', 'fields':[
            {'name':'name', 'type':'STRING', 'mode':'NULLABLE'},
            {'name':'freq', 'type':'STRING', 'mode':'NULLABLE'},
            {'name':'period', 'type':'STRING', 'mode':'NULLABLE'}]
        },
    ]
}

似乎无法将数据插入到 bigquery 表中,有人知道吗?

【问题讨论】:

    标签: python-3.x google-cloud-platform google-bigquery


    【解决方案1】:

    我已经在我的机器上进行了测试,它工作正常。请尝试以下脚本。

    from google.cloud import bigquery
    
    PROJECT_ID = "your-project"
    DATASET_ID = "your_dataset"
    TABLE_ID = "your_table"
    
    client = bigquery.Client()
    
    # 1) create table
    schema = [
        bigquery.SchemaField("columnID", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("deviceID", "INTEGER", mode="REQUIRED"),
        bigquery.SchemaField(
            "describle",
            "RECORD",
            mode="NULLABLE",
            fields=[
                bigquery.SchemaField("name", "STRING", mode="NULLABLE"),
                bigquery.SchemaField("freq", "STRING", mode="NULLABLE"),
                bigquery.SchemaField("period", "STRING", mode="NULLABLE"),
            ],
        ),
    ]
    
    table = bigquery.Table(f"{PROJECT_ID}.{DATASET_ID}.{TABLE_ID}", schema=schema)
    table = client.create_table(table)
    print(
        "Created table {}.{}.{}".format(
            table.project, table.dataset_id, table.table_id
        )
    )
    
    # 2) insert data
    rows_to_insert = [
        {
            "columnID": "123156",
            "deviceID": "156",
            "describle": {
                "name": "car",
                "freq": "10",
                "period": "3",
            },
        }
    ]
    
    errors = client.insert_rows_json(
        f"{PROJECT_ID}.{DATASET_ID}.{TABLE_ID}", rows_to_insert
    )
        
    if errors == []:
        print("New rows have been added.")
    else:
        print("Encountered errors while inserting rows: {}".format(errors))
    

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 2016-06-07
      • 2019-12-08
      • 2017-09-27
      • 2017-02-10
      • 1970-01-01
      • 2018-05-25
      • 2023-01-30
      • 2018-03-01
      相关资源
      最近更新 更多