美文网首页
hive集成hbase与简单实例

hive集成hbase与简单实例

作者: aiguozu | 来源:发表于2020-03-12 23:01 被阅读0次

        Hive高延迟、结构化和面向分析,hbase低延迟、非结构化和面向编程的。Hive通过集成Hbase获得hbase的一些特性。

        Hive集成HBase可以有效利用HBase数据库的存储特性,如行更新和列索引等。在集成的过程中注意维持HBase jar包的一致性。Hive集成HBase需要在Hive表和HBase表之间建立映射关系,也就是Hive表的列和列类型与HBase表的列族及列限定词建立关联。每一个在Hive表中的域都存在与HBase中,而在Hive表中不需要包含所有HBase中的列。HBase中的rowkey对应到Hive中为选择一个域使用:key来对应,列族(cf:)映射到Hive中的其他所有域,列为(cf:cq)。

    环境:

     hadoop2.6.2

     hive-2.2.0

     hbase1.2.5

    一.jar包拷贝(之所以用这种方式,是因为这种方式最为稳妥,最开始用的软连接的方式,总是却少jar包)到hive的lib目录下删除所有hbase相关的jar

    #cd $HIVE_HOME
    #rm -rf hbase-*.jar
    
    接着从hbase的lib目录下拷贝所有的hbase相关jar
    
    #cp -a  $HBASE_HOME/lib/hbase-*.jar ./
    
    zookeeper相关的jar也要进行替换
    

    二.在hive的hive-site.xml中添加zk的相关配置

    <property>
      <name>hive.zookeeper.quorum</name>
      <value>hadoop</value>
      <description>The list of ZooKeeper servers to talk to. This is </description>
    </property>
    <property>
      <name>hive.zookeeper.client.port</name>
      <value>2181</value>
      <description>The port of ZooKeeper servers tofor read/write locks.</description>
    </property>
    

    重启HIVE
    四.在hive中创建表,执行完建表语句后,会在hbase生成对应的hbase_emp_table表,但是这种表是non-native table类型的表,无法被truncate,也无法使用load加载数据

    CREATE TABLE hive_hbase_emp_table(
    empno int,
    ename string,
    job string,
    mgr int,
    hiredate string,
    sal double,
    comm double,
    deptno int)
    STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
    WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")
    TBLPROPERTIES ("hbase.table.name" = "hbase_emp_table");
    

    五.插入数据

    上面说了无法通过load加载数据,所以借助临时表进行insert,已提前创建了emp表.并插入了数据

    #empno对应hbase_emp_table的行键,不能为null
    
    insert into table hive_hbase_emp_table select * from emp where empno is not null;
    

    六.之后分别在hive和hbase查询数据即可

    七.无法创建新的管理表与hbase_emp_table关联,只能通过创建外部表的方式与hbase_emp_table进行关联

    八、简单实战

    #创建HBase表
    
    hbase(main):011:0>create 'user1',{NAME => 'info',VERSIONS => 1}
    #向user表中插入一些数据
    hbase(main):011:0>put 'user1','1','info:name','zhangsan'
    hbase(main):011:0>put 'user1','1','info:age','25'
    hbase(main):011:0>put 'user1','2','info:name','lisi'
    hbase(main):011:0>put 'user1','2','info:age','22'
    hbase(main):011:0>put 'user1','3','info:name','wangswu'
    hbase(main):011:0>put 'user1','3','info:age','21'
    #使用scan命令来查看下user表中的数据
    hbase(main):011:0>scan 'user';
    
    ROW COLUMN+CELL
    1 column=info:age, timestamp=1476773051604, value=25
    1 column=info:name, timestamp=1476773046344, value=zhangsan
    2 column=info:age, timestamp=1476773051685, value=22
    2 column=info:name, timestamp=1476773051661, value=lisi
    3 column=info:age, timestamp=1476773052632, value=21
    3 column=info:name, timestamp=1476773051709, value=wangswu
    3 row(s) in 0.0610 seconds
    #创建Hive中与HBase中对应的表
    // user1表
    hive>CREATE EXTERNAL TABLE user1 (
    rowkey string,
    info map<STRING,STRING>
    ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
    WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:")
    TBLPROPERTIES ("hbase.table.name" = "user1");
    // user2表
    hive>CREATE EXTERNAL TABLE user2 (
    rowkey string,
    name string,
    age int
    ) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
    WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:age")
    TBLPROPERTIES ("hbase.table.name" = "user1");
    
    #查询user1表数据
    
    hive>select * from user1;
    #结果如下
    
    OK
    1       {"age":"25","name":"zhangsan"}
    2       {"age":"22","name":"lisi"}
    3       {"age":"21","name":"wangswu"}
    Time taken: 0.467 seconds, Fetched: 3 row(s)
    #查询user2表数据
    
    hive>select * from user2
    OK
    1 zhangsan 25
    2 lisi 22
    3 wangswu 21
    Time taken: 0.087 seconds, Fetched: 3 row(s)
    #hive插入数据到hbase
    hive>INSERT INTO TABLE user1
    SELECT '4' AS rowkey,
    map('name','lijin','age','22') AS info
    from dual limit 1;
    

    相关文章

      网友评论

          本文标题:hive集成hbase与简单实例

          本文链接:https://www.haomeiwen.com/subject/vpygjhtx.html