HBase和Hive集成

一、准备

  • HBase和Hive不兼容,所以要重新编译hive-HBase-handler-1.2.2.jar
  • 将操作HBase的jar包拷到Hive下,或者使用软连接
ln -s $HBASE_HOME/lib/HBase-common-1.3.1.jar  $HIVE_HOME/lib/HBase-common-1.3.1.jar
ln -s $HBASE_HOME/lib/HBase-server-1.3.1.jar $HIVE_HOME/lib/HBase-server-1.3.1.jar
ln -s $HBASE_HOME/lib/HBase-client-1.3.1.jar $HIVE_HOME/lib/HBase-client-1.3.1.jar
ln -s $HBASE_HOME/lib/HBase-protocol-1.3.1.jar $HIVE_HOME/lib/HBase-protocol-1.3.1.jar
ln -s $HBASE_HOME/lib/HBase-it-1.3.1.jar $HIVE_HOME/lib/HBase-it-1.3.1.jar
ln -s $HBASE_HOME/lib/htrace-core-3.1.0-incubating.jar $HIVE_HOME/lib/htrace-core-3.1.0-incubating.jar
ln -s $HBASE_HOME/lib/HBase-hadoop2-compat-1.3.1.jar $HIVE_HOME/lib/HBase-hadoop2-compat-1.3.1.jar
ln -s $HBASE_HOME/lib/HBase-hadoop-compat-1.3.1.jar $HIVE_HOME/lib/HBase-hadoop-compat-1.3.1.ja
  • 修改hive-site.xml,添加下面配置
<property>
  <name>hive.zookeeper.quorum</name>
  <value>启动zookeeper的主机</value>
  <description>The list of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>
<property>
  <name>hive.zookeeper.client.port</name>
  <value>2181</value>
  <description>The port of ZooKeeper servers to talk to. This is only needed for read/write locks.</description>
</property>

二、操作

2.1 情形一

  • 数据已经在hbase中,只需要在hive建表,在hive中建表,这个表需要和hbase中的数据进行映射; 只能创建external non-native table,查询即可
  • 例如:
create external table hbase_t3(
   id int,
   age int,
   gender string,
   name string
)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:age,info:gender,info:name")
TBLPROPERTIES ("hbase.table.name" = "t3");

2.2 情形二

  • 数据还尚未插入到hbase,可以在hive中建表,建表后,在hive中执行数据的导入将数据导入到hbase,再分析。 表必须是managed non-native table。
CREATE  TABLE `hbase_emp`(
  `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" = "emp");
  • 注意
    • 替换hive-hbase-handler.jar
    • 使用insert向表中导入数据

三、注意事项

  • 在建表时,hive中的表字段的类型要和hbase中表列的类型一致,以避免类型转换失败造成数据丢失
  • row format 的作用是指定表在读取数据时,使用什么分隔符来切割数据,只有正确的分隔符,才能正确切分字段

四、理论

  • Storage Handlers
    • Storage Handlers是一个扩展模块,帮助hive分析不在hdfs存储的数据,例如数据存储在hbase上,可以使用hive提供的对hbase的Storage Handlers,来读写hbase中的数据。
    • native table: 本地表,hive无需通过Storage Handlers就能访问的表。
    • non-native table : hive必须通过Storage Handlers才能访问的表。
  • 创建native表:
    • file_format: ORC|TEXTFILE|SEQUNCEFILE|PARQUET,都是hive中支持的文件格式,由hive负责数据的读写
[ROW FORMAT row_format] [STORED AS file_format]
  • 创建non-native
    • 数据在外部存储,hive通过Storage Handlers来读写数据
STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]
  • SerDe
    • 序列化器和反序列化器,普通的文件数据,以及在建表时,如果不指定serde,默认使用LazySimpleSerDe
    • 表中的数据是什么样的格式,就必须使用什么样的SerDe
      • 纯文本:row format delimited ,默认使用LazySimpleSerDe
      • JSON格式:使用JsonSerde
      • ORC:使用读取ORC的SerDe
      • Paquet:  使用读取PaquetSerDe
    • 例如: 数据中全部是JSON格式
{"name":"songsong","friends":["bingbing","lili"]}
{"name":"songsong1","friends": ["bingbing1" , "lili1"]}
create table testSerde2(
name string,
friends array<string>
)
ROW FORMAT SERDE 
'org.apache.hive.hcatalog.data.JsonSerDe' 
STORED AS TEXTFILE;

猜你喜欢

转载自blog.csdn.net/qq_38689352/article/details/113758977