简体   繁体   English

如何从bigquery nodejs api获取整数?

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

相关问题 如何从承诺对象中获取内容? NodeJs - How can i get the content from a promise object?? NodeJs 如何从 Nodejs 中的 setTimeout 获取整数? - How can I get an integer from setTimeout in Nodejs? 如何从 JWT header 中获取值到 nodejs 中的变量中? - How can I get a value from the JWT header into a variable in nodejs? 如何从指纹扫描仪获取数据到NodeJS应用程序? - How can I get data from a fingerprint scanner in to a NodeJS application? 如何从 OAuth facebook nodejs 获取生日? (我可以在终端中登录但无法访问它) - How can I Get birthday from OAuth facebook nodejs? (I can log it in the terminal but can't access it) 我如何在谷歌脚本中获取 bigquery 表数据? - How i can get bigquery table data in google script? 我如何在nodejs中获取postrequest的正文? - How can I get the body of the postrequest in nodejs? 我如何让sharejs与nodejs一起运行? - How can I get sharejs running with nodejs? 如何获取此 JSON 的名称或从 NodeJS Express 路由器获取路径? - How can i get the name of this JSON or get the path from NodeJS Express Router? 如何为 Kubernetes nodejs API 客户端从 Z6EEDC273A68A62D7C733 文件生成“V1Job” object? - How can I generate a `V1Job` object for the Kubernetes nodejs API client from a yaml file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM