前提要安装好hadoop环境和mysql。
1、下载并解压
https://archive.apache.org/dist/hive/hive-3.1.3/apache-hive-3.1.3-bin.tar.gz
下载bin包到/app/src中。
cd /app/src/
tar zxvf apache-hive-3.1.3-bin.tar.gz
mv apache-hive-3.1.3-bin /app/hive
2、配置path
nano /etc/profile
export HIVE_HOME=/app/hive
export PATH=$HIVE_HOME/bin:$PATH
3、下载并配置connector-java驱动
下载mysql-connector-java驱动:
https://cdn.mysql.com/archives/mysql-connector-java-8.0/mysql-connector-j-8.0.33.tar.gz
cd zxvf mysql-connector-java-8.0.33.tar.gz
cd mysql-connector-java-8.0.33
cp mysql-connector-java-8.0.33-bin.jar /app/hive/lib
4、配置
cd /opt/hive/conf
cp hive-env.sh.template hive-env.sh
nano hive-env.sh
增加:
HADOOP_HOME=/usr/local/hadoop
nano hive-site.xml 内容:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration><property><name>hive.metastore.warehouse.dir</name><value>/opt/hive/warehouse</value><!-- 注意这里写刚刚新创文件的路径 --></property><property><name>hive.metastore.local</name><value>true</value></property><property><name>javax.jdo.option.ConnectionURL</name><value>jdbc:mysql://127.0.0.1:3306/hiveDB?createDatabaseIfNotExist=true</value><!-- 这里自己搓自己的mysql的ip地址 --></property><property><name>javax.jdo.option.ConnectionDriverName</name><value>com.mysql.jdbc.Driver</value></property><property><name>javax.jdo.option.ConnectionUserName</name><value>root</value></property><property><name>javax.jdo.option.ConnectionPassword</name><value>123123</value><!-- 这里搓自己的mysql账号和密码 --></property>
</configuration>
5、执行命令:
schematool -dbType mysql -initSchema
6、启动hdfs
start-dfs.sh
通过 hive进入到命令行模式。
7、例子:
create database if not exists db1;
use db1;create table if not exists table1(
eduLevel_name string comment '学历',
company_name string comment '公司名',
jobName string comment '职位名称',
salary int comment '薪资',
city_code int comment '城市编码',
responsibility string comment '岗位职责',
workingExp string comment '工作经验'
)
row format delimited fields terminated by ','
lines terminated by '\n'
stored as textfile;
truncate table table1;
load data local inpath '/root/t1.txt' into table table1;select avg(salary),workingExp from table1 group by workingExp