Javascript 如何从BigQueryNodeJSAPI获取整数?
我正在从bigquery获取数据,需要将其作为整数存储在MongoDB中,以便在Mongo中对该数据执行操作。尽管bigquery中列的数据类型是整数,但其NodeJSAPI在其Javascript对象中返回字符串。例如,我得到的结果看起来像Javascript 如何从BigQueryNodeJSAPI获取整数?,javascript,node.js,mongodb,google-bigquery,Javascript,Node.js,Mongodb,Google Bigquery,我正在从bigquery获取数据,需要将其作为整数存储在MongoDB中,以便在Mongo中对该数据执行操作。尽管bigquery中列的数据类型是整数,但其NodeJSAPI在其Javascript对象中返回字符串。例如,我得到的结果看起来像[{row1:'3',row2:'4',row3:'5'},{row1:'13',row2:'14',row3:'15'}…] typeof为对象的每个元素提供字符串。我可以运行一个循环并将每个元素转换为整数,但这在数据集上是不可伸缩的。此外,我不希望所有字
[{row1:'3',row2:'4',row3:'5'},{row1:'13',row2:'14',row3:'15'}…]
typeof
为对象的每个元素提供字符串。我可以运行一个循环并将每个元素转换为整数,但这在数据集上是不可伸缩的。此外,我不希望所有字符串都转换为整数,只希望在bigquery中存储为整数的字符串。我正在nodejs中使用gcloud模块来获取数据。假设您知道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;
});
这仍然会在每个项目上循环,但如果不是我们想要的,则会立即退出。还可以组合多个映射和过滤器来概括这一点
解决这个问题的唯一方法是首先应用过滤器,但这基本上实现了与初始类型检查相同的功能
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;
});
假设您知道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;
});
这仍然会在每个项目上循环,但如果不是我们想要的,则会立即退出。还可以组合多个映射和过滤器来概括这一点
解决这个问题的唯一方法是首先应用过滤器,但这基本上实现了与初始类型检查相同的功能
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;
});
当通过API返回整数时,BigQuery故意将整数编码为字符串,以避免大值的精度损失。目前,唯一的选择是在客户端解析它们。BigQuery在通过API返回整数时故意将整数编码为字符串,以避免丢失大值的精度。目前,唯一的选择是在客户端解析它们。更新gcloud完成了这项任务更新gcloud完成了这项任务