1、MySQL引入

当打开一个客户端窗口启动Hive后再打开一个客户端窗口启动Hive,会产生java.sql.SQLException异常。原因是,Metastore 默认存储在自带的derby数据库中,推荐使用MySQL存储Metastore;

Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException:
 Unable to instantiate
 org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
        at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:522)
        at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677)
        at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
        at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1523)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:86)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:132)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:104)
        at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:3005)
        at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:3024)
        at 
        org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:503)
... 8 more

2、MySQL安装

安装包准备
MySQL5.6.24百度云资源
链接:https://pan.baidu.com/s/12NqHvB3r6bRvzo6aX2BaVg 提取码:itza

通过XFTP工具将压缩包传到Linux的software目录下

Java 如何获取hive表的分隔符 java获取hive表元数据_mysql

1.查看mysql是否安装,如果安装了,卸载mysql

(1)查看

[root@hadoop102 桌面]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64

(2)卸载

[root@hadoop102 桌面]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64

2.解压mysql-libs.zip文件到当前目录

[root@hadoop102 software]# unzip mysql-libs.zip
[root@hadoop102 software]# ls
mysql-libs.zip
mysql-libs

3.进入到mysql-libs文件夹下

[root@hadoop102 mysql-libs]# ll
总用量 76048
-rw-r--r--. 1 root root 18509960 3月  26 2015 MySQL-client-5.6.24-1.el6.x86_64.rpm
-rw-r--r--. 1 root root  3575135 12月  1 2013 mysql-connector-java-5.1.27.tar.gz
-rw-r--r--. 1 root root 55782196 3月  26 2015 MySQL-server-5.6.24-1.el6.x86_64.rpm

4、安装MySql服务器

1.安装mysql服务端

[root@hadoop102 mysql-libs]# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm

2.查看产生的随机密码

[root@hadoop102 mysql-libs]# cat /root/.mysql_secret
OEXaQuS8IWkG19Xs	YLxnB8x8fC7txIm6

3.查看mysql状态

[root@hadoop102 mysql-libs]# service mysql status

4.启动mysql

[root@hadoop102 mysql-libs]# service mysql start

5、安装MySql客户端

1.安装mysql客户端

[root@hadoop102 mysql-libs]# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm

2.链接mysql

[root@hadoop102 mysql-libs]# mysql -uroot -pOEXaQuS8IWkG19Xs

3.修改密码

mysql>SET PASSWORD=PASSWORD('123456');

4.退出mysql

mysql>exit

6、 MySql中user表中主机配置

配置只要是root用户+密码,在任何主机上都能登录MySQL数据库。
1.进入mysql

[root@hadoop102 mysql-libs]# mysql -uroot -p123456

2.显示数据库

mysql>show databases;

3.使用mysql数据库

mysql>use mysql;

4.展示mysql数据库中的所有表

mysql>show tables;

5.展示user表的结构

mysql>desc user;

6.查询user表

mysql>select User, Host, Password from user;

7.修改user表,把Host表内容修改为%

mysql>update user set host=’%’ where host=‘localhost’;

Java 如何获取hive表的分隔符 java获取hive表元数据_HiveJDBC_02


8.删除root用户的其他host

mysql>delete from user where Host='hadoop102';
mysql>delete from user where Host='127.0.0.1';
mysql>delete from user where Host='::1';

Java 如何获取hive表的分隔符 java获取hive表元数据_mysql_03


9.刷新

mysql>flush privileges;

10.退出

mysql>quit;

在Windows系统下用Navicat数据库连接工具连接数据库

(注意:在Windows下配置了主机映射才可以写主机名)

Java 如何获取hive表的分隔符 java获取hive表元数据_mysql_04

3、 配置Metastore到MySql

1.将mysql-connector-java-5.1.27里的mysql-connector-java-5.1.27-bin.jar包复制到Hive的lib文件下

Java 如何获取hive表的分隔符 java获取hive表元数据_大数据_05

2.在/opt/module/hive/conf目录下创建一个hive-site.xml

[zhulkun@hadoop102 conf]$ touch hive-site.xml
[zhukun@hadoop102 conf]$ vi hive-site.xml

3.根据官方文档配置参数,拷贝数据到hive-site.xml文件中

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
	  <name>javax.jdo.option.ConnectionURL</name>
	  <value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true</value>
	  <description>JDBC connect string for a JDBC metastore</description>
	</property>

	<property>
	  <name>javax.jdo.option.ConnectionDriverName</name>
	  <value>com.mysql.jdbc.Driver</value>
	  <description>Driver class name for a JDBC metastore</description>
	</property>

	<property>
	  <name>javax.jdo.option.ConnectionUserName</name>
	  <value>root</value>
	  <description>username to use against metastore database</description>
	</property>

	<property>
	  <name>javax.jdo.option.ConnectionPassword</name>
	  <value>123456</value>
	  <description>password to use against metastore database</description>
	</property>
</configuration>

配置完毕后,如果启动hive异常,可以重新启动虚拟机。(重启后,别忘了启动hadoop集群)

4、多窗口启动Hive测试

1.先启动MySQL

[zhukun@hadoop102 mysql-libs]$ mysql -uroot -p123456

查看有几个数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| test               |
+--------------------+

2.再次打开多个窗口,分别启动hive

[zhukun@hadoop102 hive-1.2.1]$ bin/hive

启动hive后,回到MySQL窗口查看数据库,显示增加了metastore数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| metastore          |
| mysql             |
| performance_schema |
| test               |
+--------------------+

Java 如何获取hive表的分隔符 java获取hive表元数据_Hive_06

5、HiveJDBC访问

1、准备测试数据

在/opt/data 目录下新建aa.txt文件

Java 如何获取hive表的分隔符 java获取hive表元数据_mysql_07


编辑aa.txt

Java 如何获取hive表的分隔符 java获取hive表元数据_mysql_08


将aa.txt上传到table aa上

Java 如何获取hive表的分隔符 java获取hive表元数据_大数据_09

2. 启动hiveserver2服务

[zhuakun@hadoop102 hive]$ bin/hiveserver2

(这是一个阻塞进程,直接挂在这里另起一个窗口)

3.启动beeline

另起一个窗口启动beeline

[zhukun@hadoop102 hive]$ bin/beeline
Beeline version 1.2.1 by Apache Hive
beeline>
4.连接hiveserver2
beeline> !connect jdbc:hive2://hadoop102:10000(回车)
Connecting to jdbc:hive2://hadoop102:10000
Enter username for jdbc:hive2://hadoop102:10000: zhukun(回车)
Enter password for jdbc:hive2://hadoop102:10000: (没设密码直接回车)
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop102:10000> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
| hive_db2       |
+----------------+--+

Java 如何获取hive表的分隔符 java获取hive表元数据_大数据_10


当每正确运行一次命令后hiveserver2中会显示ok

Java 如何获取hive表的分隔符 java获取hive表元数据_Hive_11