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()
json scala apache-spark hbase
add a comment |
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
add a comment |
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
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
json scala apache-spark hbase
asked Dec 5 '18 at 18:43
RDataRData
183213
183213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
|
show 1 more comment
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)
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
|
show 1 more comment
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)
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)
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
|
show 1 more comment
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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