Мне нужно передать даты моему запросу в Python API BigQuery следующим образом.Он работает безопасно;однако таблица назначения не заполняется, поскольку даты не были успешно переданы в запрос.Я не уверен, что является причиной проблемы.
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_date
и self.end_date
инициализируются после создания объекта:
start_date, end_date = '2018-06-01 00:00:00', '2018-06-30 23:59:59'
I 'Я знаю этот вопрос тоже.