After you have configured HBase Hive integration, you can create an HBase table from
Hive, and access that table from HBase.
From the Hive shell, create an HBase table:
CREATE EXTERNAL TABLE hbase_hive_table (key int, value string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
TBLPROPERTIES ("hbase.table.name" = "hbase_hive_table", "hbase.mapred.output.outputtable" = "hbase_hive_table");
The hbase.columns.mapping property is mandatory. The
hbase.table.name property is optional. The
hbase.mapred.output.outputtable property is optional;
it is needed, if you plan to insert data to the table.
If hbase.columns.mapping values contain special characters
like '#' or '%', they have to be encoded because the values are used to form
the URI for Ranger based authentication. To enable URL encoding, set the
hive.security.hbase.urlencode.authorization.uri
property to "true" in the Hive Service Advanced Configuration Snippet
(Safety Valve) for hive-site.xml and restart the Hive on Tez
service. Also, update the corresponding Ranger policies for the table so
that they are in URL encoded format.
From the HBase shell, access the hbase_hive_table:
From Hive, query the HBase data to view the data that is inserted in the
hbase_hive_table:
hive> select * from HBASE_HIVE_TABLE;
Total MapReduce jobs = 1
Launching Job 1 out of 1
...
OK
98 val_98
Time taken: 4.582 seconds
Use the following steps to access the existing HBase table through Hive.
You can access the existing HBase table through Hive using the CREATE EXTERNAL
TABLE:
CREATE EXTERNAL TABLE hbase_table_2(key int, value string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES
("hbase.columns.mapping" = ":key,cf1:val")
TBLPROPERTIES("hbase.table.name" = "some_existing_table",
"hbase.mapred.output.outputtable" = "some_existing_table");
You can use different type of column mapping to map the HBase columns to
Hive:
Multiple Columns and Families To define four columns, the first being
the rowkey: “:key,cf:a,cf:b,cf:c”
Hive MAP to HBase Column Family When the Hive data type is a Map, a
column family with no qualifier might be used. This will use the keys of
the Map as the column qualifier in HBase: “cf:”
Hive MAP to HBase Column Prefix When the Hive data type is a Map, a
prefix for the column qualifier can be provided which will be prepended
to the Map keys: “cf:prefix_.*”
You can also define composite row keys. Composite row keys use multiple Hive
columns to generate the HBase row key.
Simple Composite Row Keys. A Hive column with a datatype of Struct will
automatically concatenate all elements in the struct with the
termination character specified in the DDL.
Complex Composite Row Keys and HBaseKeyFactory Custom logic can be
implemented by writing Java code to implement a KeyFactory and provide
it to the DDL using the table property key
“hbase.composite.key.factory”.