hive初体验
Hive搭建学习笔记
一、学习Hive之前,先了解一下什么是Hive;
Hive是Hadoop一个程序接口,Hive让数据分析人员快速上手,Hive使用了类SQL的语法,Hive让JAVA的世界变得简单而轻巧,Hive让Hadoop普及到了程序员以外的人。
二、安装Hive
1.安装前需要保证有mysql,Hadoop的环境
1. 先下载安装包
可以在http://archive.apache.org/dist/hive/ 官网找到对应版本的安装包
2.通过tar命令解压安装包
tar -zvxf apache-hive-2.3.6-bin.tar.gz
3.cd到conf目录
修改defalut配置文件
拷贝一份命名为hive-site.xml
删除里面的配置内容,由于内容比较多,可以通过以下命令
vim删除当前行到倒数第一行的命令
:.,$-1d
然后修改配置
<configuration>
<!-- WARNING!!! This file is auto generated for documentation purposes ONLY! -->
<!-- WARNING!!! Any changes you make to this file will be ignored by Hive. -->
<!-- WARNING!!! You must make your changes in hive-site.xml instead. -->
<!-- Hive Execution Parameters -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://node02:3306/hive_metadata?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>数据库密码</value>
<description>password to use against metastore database</description>
</property>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
</configuration>
保存后修改
hive-log4j2.properties.template 为 hive-log4j2.properties
通过bin初始化一下数据库:
bin/schematool -dbType mysql –initSchema
会生成对应的元数据表
在hdfs上建立对应的目录:
$HADOOP_HOME/bin/hadoop fs -mkidr /tmp
$HADOOP_HOME/bin/hadoop fs -mkidr /user/hive/warehouse
$HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
$HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
在etc/profile配置好HIVE_HOME后可以执行
#启动metastore服务
hive --service metastore &
通过hive即可访问hive
hive和hbase同步
https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration
hive-hbase-handler-3.1.0.jar 拷贝到hbase lib下,把hbase的lib所有jar拷贝到hive的lib
1、在hive的配置文件增加属性:
<property>
<name>hbase.zookeeper.quorum</name>
<value>node1,node2,node3</value>
</property>
2、在hive中创建临时表
CREATE EXTERNAL TABLE tmp_order
(key string, id string, user_id string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,order:order_id,order:user_id")
TBLPROPERTIES ("hbase.table.name" = "t_order");
CREATE TABLE hbasetbl(key int, value string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
TBLPROPERTIES ("hbase.table.name" = "xyz", "hbase.mapred.output.outputtable" = "xyz");
--创建表 create table t_order(id int,name string,phone string) row format delimited fields terminated by '\t' ;
--导入本地数据
load data local inpath '/opt/myhive.data' into table t_order;