Hive 安装和配置
1.mysql 安装(在 root 下进行)
1)上传 mysql 安装包文件,就是 3 个 rpm 文件
2)执行下面的安装
rpm -ivh --replacefiles MySQL-server*.rpm
rpm -ivh --replacefiles MySQL-devel*.rpm
rpm -ivh --replacefiles MySQL-client*.rpm
3)启动 mysql
service mysql start
*设置为开机启动: chkconfig mysql on
4) 登录 mysql
直接 mysql 就可以进入
[hadoop@master apps]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.54 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
- 修改密码:
mysql>use mysql;
mysql> update user set password=password(‘123456’) where user=‘root’;
mysql> flush privileges;
5)修改 mysql 远程登录的权限
mysql> grant all privileges on . to ‘root’@’%’ identified by ‘123456’ with
grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
成功
2.安装 hive
1)上传 hive 安装包
2)解压
tar -zxvf apache-hive-2.3.2-bin.tar.gz
mv apache-hive-2.3.2-bin hive
3)修改环境变量
vim /etc/profile
export HIVE_HOME=/opt/apps/hive
export PATH= P A T H : PATH: PATH:HIVE_HOME/bin
[hadoop @master apps]$ source /etc/profile
4)修改 Hive 的配置文件
到 hive 配置文件目录中
cd /opt/apps/hive/conf
把初始化的文件 复制一份出来 并且改名
cp hive-env.sh.template hive-env.sh
cp hive-default.xml.template hive-site.xml
cp hive-log4j2.properties.template hive-log4j2.properties
cp hive-exec-log4j2.properties.template hive-exec-log4j2.properties
5)修改 hive-env.sh
最后加上
export JAVA_HOME=/opt/apps/java ##Java 路径
export HADOOP_HOME=/opt/apps/hadoop ##Hadoop 安装路径
export HIVE_HOME=/opt/apps/hive ##Hive 安装路径
export HIVE_CONF_DIR=/opt/apps/hive/conf ##Hive 配置文件路径
启动 hadoop,在 hdfs 中创建下面的目录 ,并且授权
hadoop fs -mkdir -p /user/hive/warehouse
hadoop fs -mkdir -p /user/hive/tmp
hadoop fs-mkdir -p /user/hive/log
hadoop fs -chmod -R 777 /user/hive/warehouse
hadoop fs -chmod -R 777 /user/hive/tmp
hadoop fs -chmod -R 777 /user/hive/log
6)修改 hive-site.xml
将 hive-site.xml 文件中以下几个配置项的值设置成上一步中创建的几个路径。
在最后的 之前加上
hive.exec.scratchdir
/user/hive/tmp
hive.metastore.warehouse.dir
/user/hive/warehouse
hive.querylog.location
/user/hive/log
需要在 hive-site.xml 文件中配置 MySQL 数据库连接信息。
在最后的 之前加上
javax.jdo.option.ConnectionURL
jdbc:mysql://master:3306/hive?createDatabaseIfNotExist=true
javax.jdo.option.ConnectionDriverName
com.mysql.jdbc.Driver
javax.jdo.option.ConnectionUserName
root
javax.jdo.option.ConnectionPassword
123456
7)创建 tmp 文件夹
mkdir /opt/apps/hive/tmp
8)替换下面的元素
在配置文件 hive-site.xml 里面 总共有 4 个地方
把“${system:java.io.tmpdir}" 改成 “/opt/apps/hive/tmp”
把 “{system:user.name}" 改成 “{user.name}”
9)下载完成后把 mysql-connector-java-5.1.27.jar 然后放入 KaTeX parse error: Expected 'EOF', got '#' at position 554: ….org/codes.html#̲multiple_bindin… hive
which: no hbase in
(/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/hd/apps/java/bin:/home
/hd/apps/hadoop/bin:/home/hd/apps/hadoop/sbin:/root/bin:/home/hd/apps/java/bin:/ho
me/hd/apps/hadoop/bin:/home/hd/apps/hadoop/sbin:/home/hd/apps/hive/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/home/hd/apps/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder
.class]
SLF4J: Found binding in
[jar:file:/home/hd/apps/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf
4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Logging initialized using configuration in
file:/home/hd/apps/hive/conf/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions.
Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive> show databases;
OK
default
Time taken: 8.295 seconds, Fetched: 1 row(s)
hive>
已经成功安装 hive。。。。
Hive 的数据操作
hive 数据操作
95001,李勇,男,20,CS
95002,刘晨,女,19,IS
95003,王敏,女,22,MA
95004,张立,男,19,IS
95005,刘刚,男,18,MA
95006,孙庆,男,23,CS
95007,易思玲,女,19,MA
95008,李娜,女,18,CS
95009,梦圆圆,女,18,MA
95010,孔小涛,男,19,CS
95011,包小柏,男,18,MA
95012,孙花,女,20,CS
95013,冯伟,男,21,CS
95014,王小丽,女,19,CS
95015,王君,男,18,MA
95016,钱国,男,21,MA
95017,王风娟,女,18,IS
95018,王一,女,19,IS
95019,邢小丽,女,19,IS
95020,赵钱,男,21,IS
95021,周二,男,17,MA
95022,郑明,男,20,MA
1)建立表对象
hive>create table student(Sno int,Sname string,Sex string,Sage int,Sdept string)row
format delimited fields terminated by ‘,’ stored as textfile;
2)上传结构数据到 hdfs
把 students.txt 上传到 hdfs
[hadoop@master apps]$ hadoop fs -mkdir /hive_data
[hadoop@master apps]$ hadoop fs -put /opt/apps/students.txt /hive_data
3)加载数据
load data inpath ‘/hive_data/students.txt’ overwrite into table student;
hive> load data inpath ‘/hive_data/students.txt’ overwrite into table student;
Loading data to table default.student
OK
Time taken: 1.665 seconds
hive> select * from student;
OK
95001 李勇 男 20 CS
95002 刘晨 女 19 IS
95003 王敏 女 22 MA
95004 张立 男 19 IS
95005 刘刚 男 18 MA
95006 孙庆 男 23 CS
95007 易思玲 女 19 MA
95008 李娜 女 18 CS
95009 梦圆圆 女 18 MA
95010 孔小涛 男 19 CS
95011 包小柏 男 18 MA
95012 孙花 女 20 CS
95013 冯伟 男 21 CS
95014 王小丽 女 19 CS
95015 王君 男 18 MA
95016 钱国 男 21 MA
95017 王风娟 女 18 IS
95018 王一 女 19 IS
95019 邢小丽 女 19 IS
95020 赵钱 男 21 IS
95021 周二 男 17 MA
95022 郑明 男 20 MA
Time taken: 0.363 seconds, Fetched: 22 row(s)
4)写 SQL 加载数据
- 查询按性别分组各多少人;
- 查询男生和女生的平均年龄;
- 查询每个专业各多少人
- 查询按性别和专业分组的平均年龄;