Hive中MySQL的安装及绑定
- Hive 默认使用的元数据库为 derby,开启 Hive 之后就会占用元数据库
- derby数据库只支持单用户访问且不与其他客户端共享数据
- MySQL数据库可支持多用户访问且可与其他客户端共享数据
- 将 Hive 的元数据地址改为 MySQL数据库
[mnlg@XJ202 ~]$ my_hadoop.sh start
[mnlg@XJ202 ~]$ cd /opt/module/hive-3.1.2
[mnlg@XJ202 hive-3.1.2]$ bin/hive
hive> show databases;
hive> show tables;
hive> select * from test;
hive> insert into test values (2);
hive> select * from test;
一、MySQL安装
1、Hive中MySQL的安装
[mnlg@XJ202 hive-3.1.2]$ rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[mnlg@XJ202 hive-3.1.2]$ sudo rpm -e --nodeps mariadb-libs
[mnlg@XJ202 hive-3.1.2]$ rpm -qa|grep mariadb
2、将MySQL两个安装包拷贝到/opt/software目录下
- mysql-connector-java-5.1.27-bin.jar——数据库连接时候的jar包(放在hive里面)
- mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar——安装包
[mnlg@XJ202 software]$ tar -xf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
[mnlg@XJ202 software]$
sudo rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
3、MySQL的使用
[mnlg@XJ202 software]$ vim /etc/my.cnf
[mnlg@XJ202 software]$ cd /var/lib/mysql
[mnlg@XJ202 mysql]$ sudo mysqld --initialize --user=mysql
[mnlg@XJ202 mysql]$ sudo cat /var/log/mysqld.log
[mnlg@XJ202 mysql]$ sudo systemctl start mysqld
[mnlg@XJ202 mysql]$ mysql -uroot -p
Enter password: 输入上方mysqld.log中临时生成的密码,登陆成功
mysql> set password = password('新密码');
mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;
mysql> use my_db;
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
3 rows in set (0.04 sec)
二、Hive元数据配置到MySQL
1、Hive与MySQL进行绑定
[mnlg@XJ202 mysql]$ cd /opt/software/
[mnlg@XJ202 software]$ ll
[mnlg@XJ202 software]$ mv mysql-connector-java-5.1.27-bin.jar /opt/module/hive-3.1.2/lib
[mnlg@XJ202 software]$ cd /opt/module/hive-3.1.2/lib/
2、配置Metastore到MySQL
[mnlg@XJ202 lib]$ ll
[mnlg@XJ202 lib]$ cd ..
[mnlg@XJ202 hive-3.1.2]$ cd conf/
[mnlg@XJ202 conf]$ vim hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- jdbc 连接的 URL -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://XJ202:3306/metastore?useSSL=false</value>
</property>
<!-- jdbc 连接的 Driver-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- jdbc 连接的 username-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<!-- jdbc 连接的 password -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>000000</value>
</property>
<!-- Hive 元数据存储版本的验证 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<!--元数据存储授权-->
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>
<!-- Hive 默认在 HDFS 的工作目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
</configuration>
3、登录MySQL、新建Hive元数据库
[mnlg@XJ202 software]$ mysql -uroot -p000000
mysql> create database metastore;
mysql> quit;
[mnlg@XJ202 software]$ schematool -initSchema -dbType mysql -verbose
beeline> Initialization script completed
schemaTool completed
4、再次启动Hive
[mnlg@XJ202 software]$ cd /opt/module/hive-3.1.2/
[mnlg@XJ202 hive-3.1.2]$ bin/hive
hive> show databases;
hive> create table t1(id int);
hive> insert into t1 values (1);
hive> select * from t1;
hive> quit;
[mnlg@XJ202 hive-3.1.2]$ vim test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long
guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
[mnlg@XJ202 hive-3.1.2]$ hive
hive> show databases;
hive> create table t2 (
> name string,
> friends array<String>,
> children map<String,int>,
> address struct<Street:string,city:string>
> )
> row format delimited fields terminated by ','
> collection items terminated by '_'
> map keys terminated by ':'
> lines terminated by 'n';
hive> load data local inpath '/opt/module/hive-3.1.2/test.txt' into table t2;
hive> select * from t2;
OK
songsong ["bingbing","lili"] {"xiao song":18,"xiaoxiao song":19} {"street":"hui long ","city":null}
guan_beijing NULL NULL NULL
yangyang ["caicai","susu"] {"xiao yang":18,"xiaoxiao yang":19} {"street":"chao yang","city":"beijing"}
Time taken: 2.309 seconds, Fetched: 3 row(s)