【问题标题】:How to pass query statement to bigquery in node.js environment如何在 node.js 环境中将查询语句传递给 bigquery
【发布时间】:2019-03-12 06:26:00
【问题描述】:

大查询时,SQL语句中函数的参数 我想通过将 sql 语句作为 @ 变量名插入来更新它的结果。 但是,没有方法支持 node.js。

对于python,有类似下面例子的方法。 可以使用函数的参数作为@变量名。

query = "" "
SELECT word, word_count
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @ corpus
AND word_count> = @min_word_count
ORDER BY word_count DESC;"" "
query_params = [
bigquery.ScalarQueryParameter ('corpus', 'STRING', 'romeoandjuliet'),
bigquery.ScalarQueryParameter ('min_word_count', 'INT64', 250)]
job_config = bigquery.QueryJobConfig ()
job_config.query_parameters = query_params

相关文件: https://cloud.google.com/bigquery/docs/parameterized-queries#bigquery-query-params-python

我想征求意见。

【问题讨论】:

  • 提供的链接中没有 javascript 示例的事实并不意味着通过 node.js 中的 javascript 客户端库不支持 @。 @ 是 bigquery 端的 sql 进程的属性。请使用 javascript sql 客户端尝试此操作,如果它不起作用,请发布您收到的代码和错误消息...祝您好运。
  • 使用这个 [链接] (cloud.google.com/bigquery/docs/reference/rest/v2/…) 解释如何使用参数对象设置 job.query
  • 一种选择是使用API​​通过节点jscloud.google.com/bigquery/docs/…进行连接和查询(你可以在API explorer中尝试)

标签: node.js google-bigquery


【解决方案1】:

当您使用选项中的 params 键传递参数化查询时,BigQuery node.js 客户端支持参数化查询。刚刚更新了the docs 以显示这一点。希望这会有所帮助!

例子:

const sqlQuery = `SELECT word, word_count
      FROM \`bigquery-public-data.samples.shakespeare\`
      WHERE corpus = @corpus
      AND word_count >= @min_word_count
      ORDER BY word_count DESC`;

const options = {
  query: sqlQuery,
  // Location must match that of the dataset(s) referenced in the query.
  location: 'US',
  params: {corpus: 'romeoandjuliet', min_word_count: 250},
};

// Run the query
const [rows] = await bigquery.query(options);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2021-11-11
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多