简体   繁体   English

BigQuery DataLab的query_params格式

[英]Format of query_params for BigQuery DataLab

Hi there SO community, 大家好,

So I've been trying to get this parameterized query to work on a DataLab notebook. 因此,我一直在尝试使此参数化查询在DataLab笔记本上运行。 Since I plan to execute the query multiple assignments of such parameters I cannot use the %%bq execute syntax. 由于我计划执行查询的此类参数的多个分配,因此无法使用%%bq execute语法。 Instead I've been trying to use .execute(query_params=query_params) but I cannot figure out the appropriate format of the query_params data. 相反,我一直在尝试使用.execute(query_params=query_params)但我无法弄清楚query_params数据的适当格式。

For completeness, this is a simplified version of what I have: 为了完整起见,这是我所拥有内容的简化版本:

%%bq query -n myq
SELECT *
FROM `some_table`
WHERE some_field = @field
query_params = [{'name': 'field', 'type': 'STRING', 'value': 'POTATO'}]
myq.execute(query_params=query_params).result().to_dataframe()

I figured the query_params argument had to be a list by following this and figured that each element should be a dictionary with those keys because of this . 我认为query_params变量必须是遵循此规则的列表,并且由于这个原因,每个元素都应该是具有这些键的字典。

Nonetheless, when I execute this on my DataLab notebook I get an error indicating the parameter type is missing. 但是,当我在DataLab笔记本上执行此操作时,出现错误,指示缺少参数类型。 See part of the error stacktrace below. 请参阅下面的错误堆栈跟踪的一部分。

/usr/local/envs/py3env/lib/python3.5/site-packages/google/datalab/bigquery/_query.py in execute_async(self, output_options, sampling, context, query_params)
    268                                            allow_large_results=output_options.allow_large_results,
    269                                            table_definitions=self.data_sources,
--> 270                                            query_params=query_params)
    271     except Exception as e:
    272       raise e

/usr/local/envs/py3env/lib/python3.5/site-packages/google/datalab/bigquery/_api.py in jobs_insert_query(self, sql, table_name, append, overwrite, dry_run, use_cache, batch, allow_large_results, table_definitions, query_params)
    200       query_config['queryParameters'] = query_params
    201 
--> 202     return google.datalab.utils.Http.request(url, data=data, credentials=self.credentials)
    203 
    204   def jobs_query_results(self, job_id, project_id, page_size, timeout, start_index=0):

/usr/local/envs/py3env/lib/python3.5/site-packages/google/datalab/utils/_http.py in request(url, args, data, headers, method, credentials, raw_response, stats)
    156           return json.loads(str(content, encoding='UTF-8'))
    157       else:
--> 158         raise RequestException(response.status, content)
    159     except ValueError:
    160       raise Exception('Failed to process HTTP response.')

RequestException: HTTP request failed: Missing query parameter type

Any idea what am I missing here? 知道我在这里想念什么吗? I've search everywhere trying to find the appropriate format for this query_params thing and no luck so far. 我到处搜索,试图找到适合此query_params的格式,到目前为止还没有运气。

Thanks for the help. 谢谢您的帮助。

I finally got it to work based on this PR . 我终于可以根据此PR使它起作用。

The expected format is 预期格式为

query_params = [
  {
    'name': 'field',
    'parameterType': {'type': 'STRING'},
    'parameterValue': {'value': field}
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM