gpt4 book ai didi

node.js - 将大对象转换为原始数据类型?

转载 作者:行者123 更新时间:2023-12-05 06:51:26 26 4
gpt4 key购买 nike

我刚开始使用 BigQuery 和 NodeJS。我使用此命令安装了客户端库 npm install --save @google-cloud/bigquery 。然后我设置我的凭据和 API key 。然后我制作了一个包含以下内容的 app.js:

// Import the Google Cloud client library using default credentials
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
async function query() {
// Queries the U.S. given names dataset for the state of Texas.

const query = `SELECT AVG(vs)
FROM \`vertical-idea-303617.somedb.testdata\`
WHERE d >= '2021-01-11 18:14:00' AND d < '2021-01-11 21:14:00'
GROUP BY bId, cId, EXTRACT(YEAR FROM d), EXTRACT(MONTH FROM d), EXTRACT(DAY FROM d), EXTRACT(HOUR FROM d), EXTRACT(MINUTE FROM d)`;

// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
const options = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
};

// Run the query as a job
const [job] = await bigquery.createQueryJob(options);
console.log(`Job ${job.id} started.`);

// Wait for the query to finish
const [rows] = await job.getQueryResults();

// Print the results
console.log('Rows:');
rows.forEach(row => {
console.log(row);
});
}
query();

当我运行 node app.js 时,我得到如下结果:

Job ae0c6e3d-a41f-4ffe-b51e-087ab25cda2d started.
Rows:
{ f0_:
Big {
s: 1,
e: 2,
c: [ 5, 2, 4, 8 ],
constructor:
{ [Function: Big]
DP: 20,
RM: 1,
NE: -7,
PE: 21,
strict: false,
Big: [Circular],
default: [Circular] } } }
{ f0_:
Big {
s: 1,
e: 2,
c: [ 1, 1, 3, 5 ],
constructor:
{ [Function: Big]
DP: 20,
RM: 1,
NE: -7,
PE: 21,
strict: false,
Big: [Circular],
default: [Circular] } } }
etc...

我不明白Big 对象是什么。我期待 decimalfloat 值。这个 Big 对象是什么?如何将 Big 对象转换为 decimalfloat

我仍在尝试浏览 BigQuery 文档,但我迷路了。任何指导/方向/反馈都会有所帮助。谢谢

--

我发现我可以通过 parseFloat(row.f0_.toString())Big 转换为 float。但这是最佳实践吗?

最佳答案

您可以在查询中将结果转换为 float64:

const query = `SELECT CAST(AVG(vs) as FLOAT64)
FROM \`vertical-idea-303617.somedb.testdata\`
WHERE d >= '2021-01-11 18:14:00' AND d < '2021-01-11 21:14:00'
GROUP BY bId, cId, EXTRACT(YEAR FROM d), EXTRACT(MONTH FROM d),
EXTRACT(DAY FROM d), EXTRACT(HOUR FROM d), EXTRACT(MINUTE FROM d)`;

https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_functions

关于node.js - 将大对象转换为原始数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66157339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com