【问题标题】:BigQuery Date Functions Not Found in API QueryingAPI 查询中未找到 BigQuery 日期函数
【发布时间】:2016-11-22 22:32:21
【问题描述】:

当我尝试使用 node.js BQ API 运行以下查询时出现 ApiError: Function not found: STRFTIME_UTC_USEC 错误:

SELECT STRFTIME_UTC_USEC(created, "%h %d %Y") as user_created from `tablePath`

根据 BQ 文档 https://cloud.google.com/bigquery/docs/reference/legacy-sql#datetimefunctions ,函数 STRFTIME_UTC_USEC 在 BQ 中可用。事实上,相同的查询在 BQ 控制台网站上的查询生成器中工作(只需将表转义字符从 `` 更改为 [])。

调用查询的代码如下:

const BigQuery = require('@google-cloud/bigquery');
const sqlQuery = SELECT STRFTIME_UTC_USEC(created, "%h %d %Y") as user_created from `tablePath`
const options = {
query: sqlQuery,
useLegacySql: false // Use standard SQL syntax for queries.
};
bigquery.query(options)    
.then(function (results) {
  const rows = results[0];
  console.log(rows);
  callback(rows); 
});

有什么解决方案可以使用用于 BigQuery 的 node.js API 格式化时间戳吗?尝试在查询字符串中使用 YEAR() 函数时,我也遇到了同样的错误。

【问题讨论】:

    标签: node.js google-bigquery


    【解决方案1】:

    Legacy SQL 支持此功能,标准 SQL 不支持,但以下使其使用标准 sql,因此冲突/错误

    useLegacySql: false // Use standard SQL syntax for queries.
    

    只需将其删除或将其更改为 true

    如果您需要/想要使用标准 SQL - 以下面为例

    SELECT 
      FORMAT_DATE("%h %d %Y", CURRENT_DATE()), 
      FORMAT_TIMESTAMP("%h %d %Y", CURRENT_TIMESTAMP())
    

    【讨论】:

      【解决方案2】:

      使用FORMAT_TIMESTAMP 函数,而不是特定于旧版 SQL 的 STRFTIME_UTC_USEC。另请参阅migration guide from legacy to standard SQL

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多