1.安装HIVE
a、复制安装包并解压
b、文件夹改名为hive
c、修改系统配置文件
修改/etc/profile
export JAVA_HOME=/data/jdk1.8
export HADOOP_HOME=/data/hadoop
export HIVE_HOME=/data/hive
export PATH=$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HIVE_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib.tools.jar:$HIVE_HOME/lib/hive-jdbc-1.2.0-standalone.jar:$HIVE_HOME/lib/hadoop-common-2.6.0-cdh5.7.6.jar
注:hadoop-common-2.6.0-cdh5.7.6.jar需要从$HADOOP_HOME/share/hadoop/common/
目录复制到$HIVE_HOME/lib/
d、修改Hive的配置文件
从$HIVE_HOME/conf/
复制并重名配置文件
# cp hive-default.xml.template hive-site.xml //重新命名
# vi hive-site.xml //编辑hive-site.xml
主要修改以下参数:
<property>
<name>javax.jdo.option.ConnectionURL </name>
<value>jdbc:mysql://主机ip:3306/hive?useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName </name>
<value>com.mysql.jdbc.Driver </value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword </name>
<value>hive</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
<description>Username to use against metastore database</description>
</property>
<property>
<name>hive.exec.local.scratchdir</name>
<value>/data/hive/iotmp</value>
<description>Local scratch space for Hive jobs</description>
</property>
<property>
<name>hive.downloaded.resources.dir</name>
<value>/data/hive/iotmp</value>
<description>Temporary local directory for added resources in the remote file system.</description>
</property>
<property>
<name>hive.querylog.location</name>
<value>/data/hive/iotemp</value>
<description>Location of Hive run time structured log file</description>
</property>
e、复制myql的jdbc驱动包
复制mysql-connector-java-5.1.34-bin.jar
到$HIVE_HOME/lib/
2.启动Hive
- 启动Hadoop
start-all.sh
- 后台启动metastore服务
nohup hive --service metastore > metastore.log 2>&1 &
- 启动远程客户端连接服务
nohup hive --service hiveserver2 > hiveserver2.log 2>&1 &
- 启动hive
hive
g、测试结果
show databases
3.Hive命令
删除表
drop table if exists employee;
注:
删除表失败:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask...
解决方法:
检查$HIVE_HOME/lib/
驱动包版本,替换为mysql-connector-java-5.1.34-bin.jar
检查数据库编码方式:latin1 -- cp1252 West European
检查数据库排序规则:latin1_swedish_ci
创建表
create table if not exists
employee(eid int ,name String,salary String, destination String)
comment 'Employee details'
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;
加载本地文件数据(文本文件编码格式需要匹配操作系统,通常为UTF-8)
load data local inpath '/data/sample.txt'
overwrite into table employee;
查询数据
select * from employee;
清空表数据
truncate table employee;
相当于delete from table,truncate速度更快
truncate不能删除外部表,因为外部表里的数据并不是存放在Hive Meta store中