将Json作为Json-Scala插入Hbase中



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

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

您可以将json对象编码为字符串。然后将该字符串编码为字节数组。然后将这个字节数组放入Hbase中。伪代码将是这样的:

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

当从hbase加载值时,必须颠倒这个顺序。伪代码将是这样的:

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

最新更新