I am writing data from dataflow into bigtable, and need to retrieve data from NodeJS, but realised that the data is in byte array. How do I convert back to integer or float?
The key "\u0000\u0000\u0000\u0000" was originally a 0, but I could never get it to output correctly in my nodeJS code.
I have tried the below methods using Buffer, bin2string, byteArrayToLong, but none of them worked correctly. The following is the code to query data.
async function query(table, start, end) {
return new Promise((resolve, reject) => {
table.createReadStream({
start: start,
end: end
}).on('data', function(row) {
for(var key in row.data.ch){
console.log(JSON.stringify(key)); // Output: "\u0000\u0000\u0000\u0000"
console.log(`bin2string: ${bin2string(key)}`); // Output: bin2string:
let keybuf = Buffer.from(key);
console.log(keybuf); // Output: <Buffer 00 00 00 00>
console.log(keybuf.toString('utf8')); // Output:
const utf16Buffer = Buffer.from(key,'utf16le'); // Output: <Buffer 00 00 00 00 00 00 00 00>
console.log(utf16Buffer);
console.log(utf16Buffer.toString()); // Output:
console.log(byteArrayToLong(key)); // Output: NaN
}
// Nothing to do with data
// We can measure the time needed to get the first row
}).on('end', function(){
resolve();
});
});
}
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}
function byteArrayToLong (byteArray){
var value =0;
for(var i=byteArray.length-1; i>=0; i--){
value = value*256 + byteArray[i];
}
return value;
}