[英]How can I get integers from bigquery nodejs api?
I am fetching data from bigquery which I need to store in MongoDB as integer, so that I can perform operations on that data in Mongo. 我正在从bigquery获取数据,该数据需要以整数形式存储在MongoDB中,以便可以在Mongo中对该数据执行操作。 Even though the data types of columns in bigquery is Integer, its nodejs api is returning string in its Javascript object.
即使bigquery中列的数据类型为Integer,其nodejs api仍在其Javascript对象中返回字符串。 Eg I'm getting results that look like
[{row1:'3',row2:'4',row3:'5'},{row1:'13',row2:'14',row3:'15'}...]
例如,我得到的结果看起来像
[{row1:'3',row2:'4',row3:'5'},{row1:'13',row2:'14',row3:'15'}...]
typeof
gives string on each element of object. typeof
在对象的每个元素上给出字符串。 I can run a loop and convert each element to integer, but that is not scalable on the data set. 我可以运行一个循环并将每个元素转换为整数,但这在数据集上不可扩展。 Also, I don't want all strings to be converted to integers, only ones which are stored as integer in bigquery.
另外,我不希望将所有字符串都转换为整数,而只将其存储为bigquery中的整数。 I'm using gcloud module in nodejs to fetch data.
我在nodejs中使用gcloud模块来获取数据。
assuming you know where the type property is on the response, something like this would work. 假设您知道type属性在响应中的位置,则可以执行类似的操作。
var response = [{type: 'Integer', value: '13'} /* other objects.. */];
var mappedResponse = response.map(function(item) {
// Put your logic here
// This implementation just bails
if (item.type != 'Integer') return item;
// This just converts the value to an integer, but beware
// it returns NaN if the value isn't actually a number
item.value = parseInt(item.value);
// you MUST return the item after modifying it.
return item;
});
This still loops over each item, but immediately bails out if it's not what we're looking for. 这仍然会遍历每个项目,但是如果不是我们想要的内容,则会立即退出。 Could also compose multiple maps and filters to generalize this out.
也可以组成多个地图和过滤器来概括这一点。
The only way to get by this is by first applying a filter, but this basically achieves the same thing as our initial type check 唯一的方法是首先应用过滤器,但这基本上可以实现与我们的初始类型检查相同的功能
var mappedResponse = response
// Now we only deal with integers in the map function
.filter(x => x.type == 'Integer)
.map(function(item) {
// This just converts the value to an integer, but beware
// it returns NaN if the value isn't actually a number
item.value = parseInt(item.value);
// you MUST return the item after modifying it.
return item;
});
BigQuery deliberately encodes integers as strings when returning them via API to avoid loss of precision for large values. 通过API返回整数时,BigQuery会故意将整数编码为字符串,以避免大值的精度下降。 For now, the only option is to parse them on the client side.
目前,唯一的选择是在客户端解析它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.