本文介绍了将Json作为JSON插入Hbase-Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用scala将json对象插入Hbase单元中,目前我可以使用以下代码插入值,但想知道我如何能够将整个Json对象插入Hbase单元中.

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

推荐答案

您可以将json对象编码为字符串.然后将此字符串编码为字节数组.然后将此字节数组放入Hbase.伪代码将如下所示:

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)

,从hbase加载值时,您必须颠倒此顺序.伪代码将如下所示:

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)

这篇关于将Json作为JSON插入Hbase-Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:20