【问题标题】:Having trouble submitting a query that has > and < signs to Bigquery API向 Bigquery API 提交具有 > 和 < 符号的查询时遇到问题
【发布时间】:2018-07-31 18:08:09
【问题描述】:

我们的环境使用 Python 2.7 和 BigQuery 库 0.27.0。

要提交的查询是由

加载的 JSON 字符串的一部分
json.loads(json_blob)

然后从键中提取查询的值:

query_str = json_blob["sql_command"]

打印query_str 会得到以下值:

('query_str: ', '"   select distinct id from my_table where step_count > 3  and lower(name) = \'test\')  "')

当脚本提交查询执行如下:

job = self.bq_client.run_async_query(job_id, query_str, udf_resources=udf_obj, query_parameters=query_params)

BigQuery 作业返回错误,当我使用 https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/get 上的 job_id 查找作业信息时,我看到实际执行的查询如下:

select distinct id from my_table where step_count \u003e 3  and lower(name) = 'test')

我已经阅读了编码/解码并尝试了它们,并且没有区别。

有没有办法可以将从 json_blob (query_str = json_blob["sql_command"]) 检索到的 query_str 转换为真正的字符串?我们知道,当我们将此类查询定义为字符串(在脚本中硬编码而不是从 JSON blob 中的键检索)时,查询会成功执行,例如。

query_str = """select distinct  id from my_table where step_count > 3  and lower(name) = 'test')"""

非常感谢任何建议。

【问题讨论】:

    标签: python python-2.7 google-bigquery


    【解决方案1】:

    我无法使用 Python 2.7 重现这一点:

    $ python
    >>> from google.cloud import bigquery
    >>> import json
    >>> client = bigquery.Client('<project name redacted>')
    >>> json_blob = json.loads('{"sql_command":"select distinct id from my_table where step_count > 3  and lower(name) = \'test\'"}')
    >>> query_str = json_blob["sql_command"]
    >>> query_job = client.query(query_str)
    >>> rows = query_job.result()
    

    我收到一个错误,my_table 无法解析(如预期的那样),但没有语法错误。语法验证发生在表解析之前。查看职位信息,我看到:

    $ bq --format=prettyjson show -j <job id>
    {
      "configuration": {
        "query": {
          "priority": "INTERACTIVE", 
          "query": "select distinct id from my_table where step_count > 3  and lower(name) = 'test'", 
          "useLegacySql": false
        }
      },
    

    因此将查询传递给 BigQuery 没有问题。一些建议:

    • 检查字符串中的代码点;也许它没有您认为的内容:

      print [ord(c) for c in query_str]
      

    例如,大于号的代码点是 62,因此您应该在输出中看到它。

    • 选择不同的序列化格式而不是 JSON,看看这是否会影响结果。也许你的过程中的其他东西在你没有意识到的情况下执行转义,你可以通过使用例如确定问题的根源。改为protocol buffers,看看这是否会有所不同。

    【讨论】:

    • 非常感谢您的回复,您是对的,这不是真正的问题。
    猜你喜欢
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多