将 Json 作为 JSON 插入 Hbase - Scala

Insert Json into Hbase as JSON - Scala

我想使用 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)