[英]How can I get integers from bigquery nodejs api?
我正在從bigquery獲取數據,該數據需要以整數形式存儲在MongoDB中,以便可以在Mongo中對該數據執行操作。 即使bigquery中列的數據類型為Integer,其nodejs api仍在其Javascript對象中返回字符串。 例如,我得到的結果看起來像[{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;
});
通過API返回整數時,BigQuery會故意將整數編碼為字符串,以避免大值的精度下降。 目前,唯一的選擇是在客戶端解析它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.