运行参数化查询-标准SQL

时间:2018-10-26 18:33:13

标签: python google-bigquery

我需要按照以下说明将日期传递给BigQuery python API中的查询。它运行安全;但是,由于日期未成功传递给查询,因此未填充目标表。我不确定是什么原因引起的。

    client = bigquery.Client()
    job_config = bigquery.QueryJobConfig()

    query = """
            select distinct 
            ga.fullVisitorId
            from `843777.ga_sessions_*` ga, UNNEST(ga.hits) as hits
            where totals.timeOnSite > 0
            and (ga._TABLE_SUFFIX >= @start_date and ga._TABLE_SUFFIX <= @end_date)
    """

    query_params = [
        bigquery.ScalarQueryParameter('start_date', 'STRING', self.start_date),
        bigquery.ScalarQueryParameter('end_date', 'STRING', self.end_date)
    ]

    # Set the destination table
    table_ref = client.dataset("segmentation_project").table('myTable')
    job_config.destination = table_ref
    job_config.allow_large_results = True
    job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
    job_config.query_parameters = query_params

    try:
        query_job = client.query(query, location="US", job_config=job_config) # API request - starts the query
        query_job.result()  # Waits for job to complete.
        print('Query results loaded to table {}'.format(table_ref.path))
    except ValueError:
        print("Unable to load dataset")

在上面的代码段中,self.start_dateself.end_date在创建对象后即被初始化:

start_date, end_date = '2018-06-01 00:00:00', '2018-06-30 23:59:59'

我也知道this question

1 个答案:

答案 0 :(得分:2)

如果您查看_TABLE_SUFFIX的格式,则其格式为YYYYMMDD,因此尝试将其与YYYY-MM-DD 00:00:00进行比较将无法获得所需的结果。您应该声明:

start_date, end_date = '20180601', '20180630'