Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

hadoop - Insert data into Hbase using Hive (JSON file)

I have already created a table in hbase using hive:

hive> CREATE TABLE hbase_table_emp(id int, name string, role string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:name,cf1:role")
TBLPROPERTIES ("hbase.table.name" = "emp");

and created another table to load data on it :

hive> create table testemp(id int, name string, role string) row format delimited fields terminated by '';
hive> load data local inpath '/home/user/sample.txt' into table testemp;

and finally insert data into the hbase table:

hive> insert overwrite table hbase_table_emp select * from testemp;
hive> select * from hbase_table_emp;
OK
123 Ram     TeamLead
456 Silva   Member
789 Krishna Member
time taken: 0.160 seconds, Fetched: 3 row(s)

the table looks like this in hbase:

hbase(main):002:0> scan 'emp'
ROW                   COLUMN+CELL                                               
 123                  column=cf1:name, timestamp=1422540225254, value=Ram       
 123                  column=cf1:role, timestamp=1422540225254, value=TeamLead  
 456                  column=cf1:name, timestamp=1422540225254, value=Silva     
 456                  column=cf1:role, timestamp=1422540225254, value=Member    
 789                  column=cf1:name, timestamp=1422540225254, value=Krishna      
 789                  column=cf1:role, timestamp=1422540225254, value=Member    
3 row(s) in 2.1230 seconds

Can I do the same for a JSON file :

 {"id": 123, "name": "Ram", "role":"TeamLead"}
 {"id": 456, "name": "Silva", "role":"Member"}
 {"id": 789, "name": "Krishna", "role":"Member"}

and do :

hive> load data local inpath '/home/user/sample.json' into table testemp;

please Help ! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the get_json_object function to parse the data as a JSON object. For instance, if you create a staging table with your JSON data:

DROP TABLE IF EXISTS staging;
CREATE TABLE staging (json STRING);
LOAD DATA LOCAL INPATH '/local/path/to/jsonfile' INTO TABLE staging;

Then use get_json_object to extract the attributes you want to load into the table:

INSERT OVERWRITE TABLE hbase_table_emp SELECT
  get_json_object(json, "$.id") AS id,
  get_json_object(json, "$.name") AS name,
  get_json_object(json, "$.role") AS role
FROM staging;

There is more comprehensive discussion of this function here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...