[英]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.