Updated on 2025-11-13 GMT+08:00

Configuring Hive to Access HBase Data

Scenario

Hive on HBase allows users to query and operate data stored in HBase through the Hive SQL APIs. It combines HBase's efficient storage and real-time read/write capabilities and Hive's SQL query capabilities, providing a flexible and efficient data processing mode.

In MRS, Hive can access and process data stored in HBase through internal and external tables. This section describes how to use Hive on MRS to process MRS HBase data.

Prerequisites

  • A cluster client has been installed. For how to install the client, see Installing a Client. In the following operations, the client is installed in /opt/hadoopclient directory. You can change it as required.
  • If Kerberos authentication is enabled for the cluster (in security mode), a user for creating Hive on HBase tables has been created. The user has been added to the hive user group and configured with the HBase permissions:
    • If Ranger authentication is enabled for HBase, configure the permissions to create tables, and write data into and read data from the tables for the user. For details, see Adding a Ranger Access Permission Policy for HBase.
    • If Ranger authentication is disabled for HBase, configure the permissions to create tables, and write data into and read data from the tables for the user. For details, see Creating an HBase Permission Role.

Hive Accessing HBase Through Internal Tables

If no table is created in HBase, you can create a table in Hive. Hive automatically writes the table structure and data to HBase. The following example describes how to create a table in Hive to access HBase.

  1. Log in to the node where the client is installed as the client installation user and run the following commands to configure environment variables and authenticate the user:

    Go to the client installation directory.

    cd Client installation directory

    Load the environment variables.

    source bigdata_env

    Authenticate the user. Skip this step for clusters with Kerberos authentication disabled.

    kinit Hive service user

  2. Log in to the Hive client.

    beeline

  3. Create an HBase table in Hive, insert data into the table, and view the table data.

    1. Create an HBase table, for example, hive_hbase_table, in Hive.
      create table hive_hbase_table(id int, name string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties ("hbase.columns.mapping" = ":key,cf1:name") tblproperties ("hbase.table.name" = "hive_hbase_table");
    2. Insert data into the table.
      insert into table hive_hbase_table values(12,'abab');
    3. View the table data.
      select * from hive_hbase_table;

      The table data is as follows:

      Figure 1 Viewing Hive table data

  4. Exit the Hive client.

    !q

  5. Log in to the HBase client.

    hbase shell

  6. Check whether the table has been created in HBase.

    describe 'hive_hbase_table'

    If the command output shown in Figure 2 is displayed, the hive_hbase_table table has been created in HBase using Hive.

    Figure 2 Viewing the HBase table

  7. Check whether there is data written by Hive in HBase.

    scan 'hive_hbase_table'

    If the data is the same as that inserted in 3.b, the HBase table has been created in Hive.

    Figure 3 Viewing data in the HBase table

Hive Accessing HBase Through Foreign Tables

If a table has been created in HBase and you want to access the table using Hive, you can create a foreign table in Hive to map the table in HBase. In this way, you can access the table in HBase through Hive.

  1. Log in to the node where the client is installed as the client installation user and run the following commands to configure environment variables and authenticate the user:

    Go to the client installation directory.

    cd Client installation directory

    Load the environment variables.

    source bigdata_env

    Authenticate the user. Skip this step for clusters with Kerberos authentication disabled.

    kinit Hive service user

  2. Log in to the HBase client.

    hbase shell

  3. Create a table in HBase and query table data.

    1. Create a table in HBase.
      create 'hbase_table','f'
    2. Insert data into the table.
      put 'hbase_table','12','f:col1','hello'
    3. View the table data.
      scan 'hbase_table'
      Figure 4 Viewing data in the HBase table

  4. Press Ctrl + C to exit the HBase client.
  5. Log in to the Hive client.

    beeline

  6. Create a foreign table in Hive and map it to the table in HBase.

    create external table hbase_table(key int,col1 string,col2 string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping" = "f:col1,f:col2") tblproperties("hbase.table.name" = "hbase_table", "hbase.mapred.output.outputtable" = "hbase_table");

  7. View the hbase_table data in Hive.

    select * from hbase_table;

    If the data is the same as that inserted in 3.b, the HBase foreign table has been created in Hive.

    Figure 5 Viewing Hive table data

Helpful Links