scala插入hbase

kmbjn2e3  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(493)

我想使用scala将json对象插入hbase单元,目前我可以使用下面的代码插入值,但我想知道如何将整个json对象插入hbase单元。

  1. import org.apache.hadoop.hbase.util.Bytes.toBytes
  2. val hTable:HTable = new HTable(configuration, "tablename")
  3. val p = new Put(Bytes.toBytes("row1"))
  4. p.add(Bytes.toBytes("info"),Bytes.toBytes("firstname)",Bytes.toBytes("Jim"))
  5. hTable.put(p)
  6. hTable.close()
2guxujil

2guxujil1#

可以将json对象编码为字符串。然后将这个字符串编码为字节数组。然后把这个字节数组放到hbase中。伪代码如下:

  1. json = createYourJson()
  2. jsonString = json.toString
  3. jsonBytyes = Bytes.toBytes(jsonString)
  4. put.add(yourColumnFamily, yourQualifier, jsonBytes)

当从hbase加载值时,必须反转此顺序。伪代码如下:

  1. jsonBytes = hbase.get(table, columnFamily, qualifier)
  2. jsonString = Bytes.toString(jsonBytes)
  3. json = Json.parse(jsonString)

相关问题