参考官网:https://cwiki.apache.org/confluence/display/Hive/GettingStarted
Java1.7或以上,建议java1.8
Hadoop2.x
官网地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/ 可以选择合适的版本下载
下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3.2/apache-hive-2.3.2-bin.tar.gz
可以直接使用wget命令直接下载到linux服务器上。
wget https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3.2/apache-hive-2.3.2-bin.tar.gz
tar xvf apache-hive-2.3.2-bin.tar.gz
$HADOOP_HOME/sbin/start-dfs.sh
$HADOOP_HOME/bin/hadoop fs -mkdir tmp
$HADOOP_HOME/bin/hadoop fs -mkdir /user/hive/warehouse
$HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
$HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
通过hive存储的数据实际存在hdfs上。
hive的元数据(如表定义)存储在derby数据库或mysql数据库里。
5.1 使用derby数据库
$HIVE_HOME/bin/schematool -dbType "derby" -initSchema
好处:不需要额外安装
缺点:derby只允许单连接,无法两个客户端连接到hive;
初始化的时候会在当前路径生成一个metastore文件,当切换路径去启动hive时,会找不到该文件,报错。
不建议使用derby数据库。
5.2 使用mysql数据库
(1)配置hive-site.xml文件
vim $HIVE_HOME/conf/hive-site.xml
hive.metastore.warehouse.dir
/user/hive/warehouse
hive.metastore.local
true
javax.jdo.option.ConnectionURL
jdbc:mysql://127.0.0.1:3306/hive?createDatabaseIfNotExist=true&useSSL=false
javax.jdo.option.ConnectionDriverName
com.mysql.jdbc.Driver
javax.jdo.option.ConnectionUserName
root
javax.jdo.option.ConnectionPassword
rootpassword
(2)需要将mysql jdbc驱动包拷至$HIVE_HOME/lib/下
官网地址:https://dev.mysql.com/downloads/connector/j/ 选择JDBC
下载地址:https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.46.tar.gz
可以使用wget下载,并使用tar xvf命令解压,拷贝其中的mysql-connector-java-5.1.46.jar
(3)初始化mysql数据库
$HIVE_HOME/bin/schematool -dbType mysql -initSchema
$HIVE_HOME/bin/hive
7. 简单的表操作,数据操作
show databases;
show tables;
create table test (id int ,name string ) row format delimited fields terminated by ' ';
load data local inpath'/root/lbl/file/hivetest' overwrite into table test;
select * from test;
第三行是创建表,表数据会存在hdfs的文件中,表的一行数据在文件中也是一行,列与列之间以‘ ’间隔。
第四行是将本地文件读取到表中,文件内容如下:
1 a
2 b
3 c