首页 > 解决方案 > 将 SELECT 语句结果导出到 BigQuery 时,仅创建空表

问题描述

我正在尝试将 select 语句结果导出到另一个表作为永久存储。但是,无论何时创建新表,它都是无模式的。当我尝试查询该结果表时,会显示错误:

表 project-id.dataset_name.temp_table 没有架构。

这是我将结果从 SELECT 语句导出到临时选项卡的代码

def query_to_table():
    service_account_info = {}  # account info

    credentials = Credentials.from_service_account_info(
        service_account_info)
    client = bigquery.Client(
        project=service_account_info.get("project_id"),
        credentials=credentials)

    query = """
            SELECT
                a,
                b

            FROM `project.dataset.table`

            WHERE a NOT IN ('error', 'warning')
        """

    destination_dataset = client.dataset("abc_123") #this is another dataset
    destination_table = destination_dataset.table("temp_table") # destination table

    try:
        client.get_table(destination_table)
        client.delete_table(destination_table)
    except Exception as e:
        # Some logging
        pass

    client.create_table(Table(destination_table))

    # Execute the job and save to table
    job_config = bigquery.QueryJobConfig()
    job_config.allow_large_results = True
    job_config.use_legacy_sql = False
    job_config.destination = destination_table
    job_config.dry_run = True

    query_job = client.query(query, job_config=job_config)

    # Wait till the job done
    while not query_job.done():
        time.sleep(1)

    logging.info(f"Processed {query_job.total_bytes_processed} bytes.")

    return destination_table

错误在哪里?Google Cloud 方面是否有任何 API 更改?因为这个脚本在一个月前工作。

请帮忙。

标签: python-3.xgoogle-cloud-platformgoogle-bigquerypython-bigquery

解决方案


该死!我只是想通了,那是因为我dry_runTrue.

根据这个:https : //stackoverflow.com/a/28355802/4494547,如果dry_run设置为True,它只是评估查询而不实际运行作业。

花了我 5 个小时搞定我的脑袋。:(


推荐阅读