Insert Json into Hbase as JSON - Scala












0















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









share|improve this question



























    0















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









    share|improve this question

























      0












      0








      0








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









      share|improve this question














      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 scala apache-spark hbase






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 5 '18 at 18:43









      RDataRData

      183213




      183213
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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)





          share|improve this answer


























          • im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

            – RData
            Dec 5 '18 at 19:13








          • 1





            yes, you should add the column family and the qualifier. I edited my answer.

            – Mahmoud Hanafy
            Dec 5 '18 at 19:17











          • yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

            – RData
            Dec 5 '18 at 19:22











          • Bytes,toBytes("columnfamily") this is comma?

            – Mahmoud Hanafy
            Dec 5 '18 at 19:29











          • sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

            – RData
            Dec 5 '18 at 19:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53638809%2finsert-json-into-hbase-as-json-scala%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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)





          share|improve this answer


























          • im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

            – RData
            Dec 5 '18 at 19:13








          • 1





            yes, you should add the column family and the qualifier. I edited my answer.

            – Mahmoud Hanafy
            Dec 5 '18 at 19:17











          • yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

            – RData
            Dec 5 '18 at 19:22











          • Bytes,toBytes("columnfamily") this is comma?

            – Mahmoud Hanafy
            Dec 5 '18 at 19:29











          • sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

            – RData
            Dec 5 '18 at 19:32
















          2














          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)





          share|improve this answer


























          • im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

            – RData
            Dec 5 '18 at 19:13








          • 1





            yes, you should add the column family and the qualifier. I edited my answer.

            – Mahmoud Hanafy
            Dec 5 '18 at 19:17











          • yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

            – RData
            Dec 5 '18 at 19:22











          • Bytes,toBytes("columnfamily") this is comma?

            – Mahmoud Hanafy
            Dec 5 '18 at 19:29











          • sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

            – RData
            Dec 5 '18 at 19:32














          2












          2








          2







          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)





          share|improve this answer















          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)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 5 '18 at 21:05

























          answered Dec 5 '18 at 19:09









          Mahmoud HanafyMahmoud Hanafy

          97011528




          97011528













          • im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

            – RData
            Dec 5 '18 at 19:13








          • 1





            yes, you should add the column family and the qualifier. I edited my answer.

            – Mahmoud Hanafy
            Dec 5 '18 at 19:17











          • yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

            – RData
            Dec 5 '18 at 19:22











          • Bytes,toBytes("columnfamily") this is comma?

            – Mahmoud Hanafy
            Dec 5 '18 at 19:29











          • sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

            – RData
            Dec 5 '18 at 19:32



















          • im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

            – RData
            Dec 5 '18 at 19:13








          • 1





            yes, you should add the column family and the qualifier. I edited my answer.

            – Mahmoud Hanafy
            Dec 5 '18 at 19:17











          • yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

            – RData
            Dec 5 '18 at 19:22











          • Bytes,toBytes("columnfamily") this is comma?

            – Mahmoud Hanafy
            Dec 5 '18 at 19:29











          • sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

            – RData
            Dec 5 '18 at 19:32

















          im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

          – RData
          Dec 5 '18 at 19:13







          im getting a overload method error as cannot be applied to (Array[Byte]), below is the code p.add(Bytes.toBytes("columnfamily"),Bytes.toBytes("qualifier)",Bytes.toBytes("jsonBytes")) hTable.put(m)

          – RData
          Dec 5 '18 at 19:13






          1




          1





          yes, you should add the column family and the qualifier. I edited my answer.

          – Mahmoud Hanafy
          Dec 5 '18 at 19:17





          yes, you should add the column family and the qualifier. I edited my answer.

          – Mahmoud Hanafy
          Dec 5 '18 at 19:17













          yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

          – RData
          Dec 5 '18 at 19:22





          yea tried as below m.add(Bytes,toBytes("columnfamily"),Bytes.toBytes("qualifier"),Bytes.toBytes(jsonBytes)) - same issue

          – RData
          Dec 5 '18 at 19:22













          Bytes,toBytes("columnfamily") this is comma?

          – Mahmoud Hanafy
          Dec 5 '18 at 19:29





          Bytes,toBytes("columnfamily") this is comma?

          – Mahmoud Hanafy
          Dec 5 '18 at 19:29













          sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

          – RData
          Dec 5 '18 at 19:32





          sorry , keyboard issue . this fixed it . im able to now insert but it is not being inserted as a json format ?

          – RData
          Dec 5 '18 at 19:32




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53638809%2finsert-json-into-hbase-as-json-scala%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma