java 调用hive_[原创]使用java thrift client调用hive时遇到的问题及解决

hive有几种访问方法,shell的和thrift

client的方法,在安装hive的时候,默认是把metastore保存到deby里的,这种方式就是把metastore保存到本地文件,他的问题是,在同一时间,只能有一个实例访问hive,你开两个shell,就会报错,而且这种方式更多适用于做unit

test,而非工业做法。

那么metastore可以存储到mysql,本地或者非本地都可以。而且只有使用mysql存储metastore的方式,thrift才可以顺利访问到shell下创建的table。

Hive 将元数据存储在 RDBMS 中,有三种模式可以连接到数据库:

1)ingle User Mode: 此模式连接到一个 In-memory 的数据库 Derby,一般用于 Unit

Test。

2)Multi User Mode:通过网络连接到一个数据库中,是最经常使用到的模式。

3)Remote Server Mode:用于非 Java 客户端访问元数据库,在服务器端启动一个

MetaStoreServer,客户端利用 Thrift 协议通过 MetaStoreServer 访问元数据库。

Hive默认是采用Derby来存储其Meta信息的,

现在我们需要修改为mysql

1.在mysql专门为hive添加用户

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

mysql> CREATE USER'hive'@'hive-mysql'IDENTIFIED BY'hivepasswd';

mysql> GRANT ALL PRIVILEGES ON *.* TO'hive'@'hive-mysql'WITH GRANT OPTION;

mysql> CREATE USER 'hive'@'hive-mysql' IDENTIFIED BY 'hivepasswd';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'hive'@'hive-mysql' WITH GRANT OPTION;

2.修改配置文件conf/hive-site.xml

Xml代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

hive.metastore.local

true

controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM

javax.jdo.option.ConnectionURL

jdbc:mysql://hive-mysql:3306/hive?createDatabaseIfNotExist=true

JDBC connect string for a JDBC metastore

javax.jdo.option.ConnectionDriverName

com.mysql.jdbc.Driver

Driver class name for a JDBC metastore

javax.jdo.option.ConnectionUserName

hive

username to use against metastore database

javax.jdo.option.ConnectionPassword

hivepasswd

password to use against metastore database

hive.metastore.local

true

controls whether to connect to remove metastore server or open a new metastore server in Hive Client JVM

javax.jdo.option.ConnectionURL

jdbc:mysql://hive-mysql:3306/hive?createDatabaseIfNotExist=true

JDBC connect string for a JDBC metastore

javax.jdo.option.ConnectionDriverName

com.mysql.jdbc.Driver

Driver class name for a JDBC metastore

javax.jdo.option.ConnectionUserName

hive

username to use against metastore database

javax.jdo.option.ConnectionPassword

hivepasswd

password to use against metastore database

3.添加jdbc的jar包

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.11.tar.gz/from/http://mysql.he.net/

tar -xvzf mysql-connector-java-5.1.11.tar.gz

cp mysql-connector-java-5.1.11/*.jar /data/soft/hive/lib

wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.11.tar.gz/from/http://mysql.he.net/

tar -xvzf mysql-connector-java-5.1.11.tar.gz

cp mysql-connector-java-5.1.11/*.jar /data/soft/hive/lib

4.启动hive

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

bin/hive

hive> show tables;

bin/hive

hive> show tables;

When using MySQL as a metastore I see the error

"com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Specified key

was too long; max key length is 767 bytes".

* This is a

known limitation of MySQL 5.0 and UTF8 databases. One option is to

use another character set, such as 'latin1', which is known to

work.

Hive用MySQL做元数据保存的数据库时,mySQL设置

http://running.iteye.com/blog/910277

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值