Search code examples
jsonscalaapache-sparkhbase

Insert Json into Hbase as JSON - Scala


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

Solution

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