JS客户端在BigQuery中存储数字类型以进行流插入

JS客户端在BigQuery中存储数字类型以进行流插入

本文介绍了如何使用Node JS客户端在BigQuery中存储数字类型以进行流插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于BigQuery中的数字类型支持的限制方式超出了Javascript的数字类型,我如何使用节点js客户端 Table.insert(rows,options,callback)方法存储数字类型?对于使用字符串类型处理大数,我正在考虑使用

与此同时,这段代码在NodeJS中工作,可以将庞大的数字插入BigQuery:

  BigQuery = require('@ google-cloud/bigquery');bigquery = BigQuery({projectId:'my-project'});数据集= bigquery.dataset('tt');表= dataset.table('testfun_numeric');行= [{名称:随便什么",numi:-99999999999999999999999999999.999999999"}];table.insert(rows); 

在BigQuery中:

  SELECT名称,numi,numi = numi + 1000 better_comparison来自`fh-bigquery.tt.testfun_numeric` 

我以前在BigQuery中使用

创建了该表

 创建表`tt.testfun_numeric`作为SELECT''名称,CAST(0 AS NUMERIC)numi极限0 

FWIW,我还尝试在NodeJS中使用 BigInt(Number.MAX_SAFE_INTEGER),但是该库无法将其正确发送给BigQuery-除非先将其转换为字符串.

Given that numeric type in BigQuery supports limits way beyond the number type of Javascript, how do I store a numeric type using the node js client Table.insert(rows, options, callback) method? For handling big numbers using string type, I was considering using https://github.com/MikeMcl/big.js. If I send a string value (containing a valid number beyond javascript's number limit) for a numeric field in my BigQuery table, will it work?

解决方案

You need to treat the numbers that are too big for JavaScript as strings.

For example, JS doesn't know how to handle -99999999999999999999999999999.999999999:

Meanwhile this code works in NodeJS to insert that huge number into BigQuery:

BigQuery = require('@google-cloud/bigquery');
bigquery = BigQuery({projectId: 'my-project'});
dataset = bigquery.dataset('tt');
table = dataset.table('testfun_numeric');

rows = [{
  name:"whatever",
  numi: "-99999999999999999999999999999.999999999"
}];
table.insert(rows);

And in BigQuery:

SELECT name, numi
  , numi=numi+1000 better_comparison
FROM `fh-bigquery.tt.testfun_numeric`

I previously created the table in BigQuery with

CREATE TABLE `tt.testfun_numeric`
AS
SELECT '' name, CAST(0 AS NUMERIC) numi
LIMIT 0

FWIW, I also tried BigInt(Number.MAX_SAFE_INTEGER) in NodeJS, but the library wouldn't send it correctly to BigQuery - unless I converted it into a string first.

这篇关于如何使用Node JS客户端在BigQuery中存储数字类型以进行流插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:35