Insert Json into Hbase as JSON - Scala

三世轮回 提交于 2019-12-20 06:01:06

问题


I would like to insert a json object into a Hbase cellusing scala, presently i'm able to insert values using the below code but would like to know how i may be able to insert the entire Json object into a Hbase cell.

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()

回答1:


You can encode your json object as a string. then encode this string as byte array. then put this byte array in Hbase. pseudo code will be like this:

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

and when loading the value from hbase you have to reverse this order. Pseudo code will be like this:

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


来源:https://stackoverflow.com/questions/53638809/insert-json-into-hbase-as-json-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!