【发布时间】:2017-07-14 05:11:28
【问题描述】:
在 Python 脚本中,我试图在 bigquery 表中插入一条记录。其中一个字段是将 Json 对象的值作为字符串接收。这是我用来执行此操作的代码:
query = "INSERT into config.job_config ( job_name, run_id, task_name, task_step, run_config, version, run_time) VALUES (" + "'" + self.job_name + "', '" + self.run_id + "', '"+self.task_name + "', '"+ task_step + "', '"+ json.dumps(configy) +"', '" + self.config_version+ "', CURRENT_TIMESTAMP() "+")"
print query
query_job = self.bq_client.run_sync_query(query)
query_job.timeout_ms = 60000
query_job.run()
以下是生成的“打印查询”语句:
INSERT into config.job_config ( job_name, run_id, task_name, task_step, run_config, version, run_time) VALUES ('copy:temp.test_lines', 'run-id-123', 'BQLoadGcsFile', '1', '{"gcs": {"landing_bucket": "gs://test-development", "landing_dir": "/lineitems/", "archive_bucket": "gs://test-development", "archive_dir": "/archive/"}, "gcs_to_bq_job_id": "test_lines-run-id-123-2017-07-13"}', '3.0', CURRENT_TIMESTAMP() )
当我在 UI 中执行插入语句时,它工作正常。但是,当上面的代码执行时,会产生如下错误:
File "/home/fereshteh/utils/scheduler_config.py", line 87, in insert_task_instance_config
query_job.run()
File "/home/fereshteh/google-cloud-env/local/lib/python2.7/site-packages/google/cloud/bigquery/query.py", line 364, in run
method='POST', path=path, data=self._build_resource())
File "/home/fereshteh/google-cloud-env/local/lib/python2.7/site-packages/google/cloud/_http.py", line 303, in api_request
error_info=method + ' ' + url)
google.cloud.exceptions.BadRequest: 400 Encountered "" at line 1, column 43.
[Try using standard SQL
(https://cloud.google.com/bigquery/docs/reference/standard-sql/enabling-standard-sql)] (POST https://www.googleapis.com/bigquery/v2/projects/sansar-dev/queries)
当我添加“query.use_legacy_sql = False”(来自https://googlecloudplatform.github.io/google-cloud-python/stable/bigquery-usage.html#querying-data-synchronous)时:
query_job = self.bq_client.run_sync_query(query)
query_job.timeout_ms = 60000
query.use_legacy_sql = False
query_job.run()
它给出以下错误:
query.use_legacy_sql = False
AttributeError: 'str' object has no attribute 'use_legacy_sql'
感谢任何帮助
【问题讨论】:
标签: google-bigquery