如何在Bigquery中插入一个接收json字符串值的字段?

时间:2017-07-14 05:11:28

标签: google-bigquery

在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中执行该insert语句时,它工作正常。但是,当执行上面的代码时,它会生成以下错误:

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'

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

query是您的查询字符串而不是作业。它看起来应该是:

query_job = self.bq_client.run_sync_query(query)
query_job.timeout_ms = 60000
query_job.use_legacy_sql = False
query_job.run()