Hive HBase集成

来源:互联网 发布:mac怎么调节声音大小 编辑:程序博客网 时间:2024/06/08 16:08

1.版本

    hive0.14

    hbase0.99.2

2.编译

    由于hive0.14版本不支持hbase0.99.2,所以需要对hive源码中的hbase-handler模块中的相关类做修改,重新编译。

2.1 修改 /hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseStorageHandler.java 类470行

TableMapReduceUtil.addDependencyJars(    jobConf, HBaseStorageHandler.class, TableInputFormatBase.class, org.cliffc.high_scale_lib.Counter.class); // this will be removed for HBase 1.0
将org.cliffc.high_scale_lib.Counter.class参数去掉。

2.2 修改/hbase-handler/src/java/org/apache/hadoop/hive/hbase/HiveHBaseInputFormatUtil.java 类

在该类中添加方法

+import org.apache.hadoop.hbase.TableName;
+  public static TableName getTableName(JobConf jobConf) throws IOException {+    String hbaseTableName = jobConf.get(HBaseSerDe.HBASE_TABLE_NAME);+    return TableName.valueOf(Bytes.toBytes(hbaseTableName));+  }

2.3 修改 /hbase-handler/src/java/org/apache/hadoop/hive/hbase/HiveHBaseTableInputFormat.java 类

+import org.apache.hadoop.hbase.client.ConnectionFactory;+import org.apache.hadoop.hbase.client.Connection;

 

93行

-    setHTable(HiveHBaseInputFormatUtil.getTable(jobConf));+    final Connection conn = ConnectionFactory.createConnection(jobConf);++    initializeTable(conn, HiveHBaseInputFormatUtil.getTableName(jobConf));

111行

       public void close() throws IOException {         recordReader.close();+        conn.close();       }

 

362行

 

+    final Connection conn = ConnectionFactory.createConnection(jobConf);

+      InputSplit [] results = null;

+try{

+    initializeTable(conn, HiveHBaseInputFormatUtil.getTableName(jobConf));
-    setHTable(new HTable(HBaseConfiguration.create(jobConf), Bytes.toBytes(hbaseTableName)));

 

+       results = new InputSplit[splits.size()];

-   InputSplit[] results = new InputSplit[splits.size()];

 

         for (int i = 0; i < splits.size(); i++) { 
               results[i] = new HBaseSplit((TableSplit) splits.get(i), tablePaths[0]);
           }
+     } finally{
+           conn.close();
+     }

2.4 maven编译hive

 

3.配置

编辑hive/conf/hive-env.sh

将hive和hbase的lib包加入到hive的环境变量中。

如下

export HIVE_AUX_JARS_PATH=${HIVE_HOME}/lib;${HBASE_HOME}/lib

 

4.demo

4.1hive建表

CREATE TABLE hbase_hive_1(key int, value string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping"=":key,cf:val") TBLPROPERTIES ("hbase.table.name"="hivehbase1");

在hive中建表hbase_hive_1,映射hbase中的表为hivehbase1,执行此命令同时建了hive和hbase的表。


hive外部表

CREATE EXTERNAL TABLE hbase_hive_1(key int, value string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping"=":key,cf:val") TBLPROPERTIES ("hbase.table.name"="hivehbase");

hbase中已经有hivehbase表,执行hive的建外部表命令。

 

4.2插入数据

insert overwrite table hbase_hive_1 values(1,'aaaa');


4.3查询数据

select * from hbase_hive_1


5.性能测试

参考http://lxw1234.com/archives/2015/04/101.htm

0 0
原创粉丝点击