简体   繁体   English

如何在 BigQuery nodejs 客户端库中使用 BigQueryDate class?

[英]How do I use the BigQueryDate class in the BigQuery nodejs client library?

I have a BigQuery table with a column of type "DATE".我有一个 BigQuery 表,其中有一列类型为“DATE”。 When I query the table with the nodejs client library as per below, the date elements in the data are objects of the BigQueryDate class. Thedocumentation for the BigQueryDate class provides no explanation on how the use the class. I would like to convert the date objects to either JavaScript date objects, or date strings, so I can use the date data in my application.当我按照下面的方式使用nodejs 客户端库查询表时,数据中的日期元素是 BigQueryDate class 的对象。BigQueryDate class 的文档没有说明如何使用 class。我想转换日期对象为 JavaScript 日期对象或日期字符串,因此我可以在我的应用程序中使用日期数据。

const { BigQuery } = require("@google-cloud/bigquery");
const bigquery = new BigQuery();
const query = "SELECT * FROM `project.dataset.table`";
bigquery.query(query, function(err, rows) {
  if (!err) {
    console.log(rows);
  }
});

The only parameter that the class has is value, which is the date as a string of the form yyyy-mm-dd. class 的唯一参数是 value,它是格式为 yyyy-mm-dd 的字符串的日期。

const { BigQueryDate } = require("@google-cloud/bigquery");
const date = new BigQueryDate("2019-01-20");
date.value; // returns string "2019-01-20"

I encountered the same situation and found my answer as below:我遇到了同样的情况,并找到了如下答案:
I'm assuming that you've executed query and looping through the rows我假设您已经执行了查询并遍历了行

// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
...
//connect to BigQuery and get the results
...
//loop through the rows and print it on console.
console.log('Rows:');
rows.forEach(
row => console.log(`${row.Col1}:${row.Col2}:${(row.DateCol3).value}:${row.col4}`)
);

Note: Here DateCol3 is date column in BigQuery dataset.注意:这里的 DateCol3 是 BigQuery 数据集中的日期列。
Hope this helps many others.希望这对其他人有帮助。

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

相关问题 如何使用 GCP 的 NodeJS 库部署新的 API 网关 OpenAPI 规范? - How do you use GCP's NodeJS library to deploy new API Gateway OpenAPI Specs? 我如何在 google BigQuery 上输入 pivot? - how do I pivot this on google BigQuery? 如何配置 Google BigQuery 命令行工具以使用服务帐户? - How do I configure Google BigQuery command line tool to use a Service Account? 如何使用 Java 的 SQS 扩展客户端库处理 AWS SQS 中的大型消息? - How do I handle large messages in AWS SQS using the SQS Extended Client Library for Java? 如何将 BigQuery 中的细分导入 Firebase? - How do I import segments from BigQuery into Firebase? 如何获取 BigQuery 中一系列表的最后更新时间? - How do I get the last update time of a sequence of tables in BigQuery? 如何在 BigQuery 中随机匹配元素 - How Do I Randomly Match Elements in BigQuery 如何使用Python的Google Cloud Client Library配置gcloud项目 - How to use Google Cloud Client Library for Python to configure the gcloud project 如何在 BigQuery SQL 中添加 arrays? - How do I add arrays in BigQuery SQL? BigQuery 库中的 [AssertionCredentials] 参数是什么以及如何获取它? - What is [AssertionCredentials] parameter in BigQuery library and how to get it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM