【问题标题】:How to convert Bigtable HBase API returning byte array to integer or float in nodeJS?如何将 Bigtable HBase API 返回字节数组转换为 nodeJS 中的整数或浮点数?
【发布时间】:2019-03-17 22:04:33
【问题描述】:

我正在将数据流中的数据写入bigtable,需要从NodeJS检索数据,但意识到数据是字节数组。如何转换回整数或浮点数?

键“\u0000\u0000\u0000\u0000”最初是 0,但我无法让它在我的 nodeJS 代码中正确输出。

我已经使用 Buffer、bin2string、byteArrayToLong 尝试了以下方法,但它们都不能正常工作。以下是查询数据的代码。

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;
}

【问题讨论】:

    标签: javascript arrays node.js hbase google-cloud-bigtable


    【解决方案1】:

    我对你的问题没有完整的答案,但我有一些建议。

    Cloud Bigtable 数字都是“编码为 8 字节 big-endian 值的 64 位整数”(请参阅​​ here)。 Node.js long 的“endian-ness”是系统特定的(参见here)。 PHP 与 Cloud Bigtable 有类似的问题(请参阅here)。

    在 Java 中,所有数值都是大端的。 HBase Bytes 类完成了数字和字节 (source code) 之间的所有转换,并可能提供一些线索。

    https://github.com/googleapis/nodejs-bigtable/issues 上发布问题以获得更好的解决方案可能会很生气。

    【讨论】:

      【解决方案2】:

      对于整数,下面的函数可以工作。

      function byteToInt(x){
          let val=0;
          for (let i=0; i<x.length; ++i) {        
              val+=x[i];        
              if(i<x.length-1) val = val << 8;
          }
          return val;
      }
      

      对于float,NodeJS已经提供了从Buffer中读取的方法:

      let buf = Buffer.from(value, 'binary');
      let num = buf.readFloatBE(0);
      

      根据字节顺序和位数,也可以使用以下内容:

      buf.readInt8(偏移量)

      buf.readInt16BE(偏移量)

      buf.readInt16LE(偏移量)

      buf.readInt32BE(偏移量)

      buf.readInt32LE(偏移量)

      【讨论】:

        猜你喜欢
        • 2013-01-09
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多