如何在Hive-Site.xml文件中为配置单元Metastore连接设置配置? [英] How to set configuration in Hive-Site.xml file for hive metastore connection?
问题描述
我想使用java代码连接MetaStore。我不知道如何在Hive-Site.xml文件中设置配置设置,以及在何处发布Hive-Site.xml文件。请帮助。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
public class HiveMetastoreJDBCTest {
public static void main(String [] args)throws Exception {
Connection conn = null;
尝试{
HiveConf conf = new HiveConf();
conf.addResource(new Path(file:///path/to/hive-site.xml));
Class.forName(conf.getVar(ConfVars.METASTORE_CONNECTION_DRIVER));
conn = DriverManager.getConnection(
conf.getVar(ConfVars.METASTORECONNECTURLKEY),
conf.getVar(ConfVars.METASTORE_CONNECTION_USER_NAME),
conf.getVar(ConfVars.METASTOREPWD));
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
select t.tbl_name,s.location from tbls t+
join sds s on t.sd_id = s.sd_id); (rs.next()){
System.out.println(rs.getString(1)+:+ rs.getString(2));
}
}
}
}
> hive-site.xml :
< property>
< name> hive.metastore.local< / name>
<值> true< /值>
< / property>
<属性>
< name> javax.jdo.option.ConnectionURL< / name>
< value> jdbc:mysql:// localhost:3306 / hive< / value>
< / property>
<属性>
< name> javax.jdo.option.ConnectionUserName< / name>
< value> hiveuser< / value>
< / property>
<属性>
< name> javax.jdo.option.ConnectionPassword< / name>
< value> hivepass< / value>
< / property>
在 jdbc:mysql:// localhost:3306 / hive
, 3306
是你的默认mysql端口; hive
是我们的hive Metastore的mysql数据库名称。
将 hiveuser
更改为您的mysql配置单元用户名,并将 hivepass
更改为您的mysql配置单元密码。
$ b
在终端中执行此步骤,如果您尚未在mysql中为配置单元存储区创建数据库:
mysql -u root -p
输入您的mysql root密码。
的MySQL>创建数据库配置单元;
mysql>创建用户'hiveuser'@'%'IDENTIFIED BY'hivepass';
mysql> GRANT all on *。* to'hiveuser'@ localhost''hivepass';
mysql> flush privileges;
这里, hiveuser
和 hivepass
分别代表您给予配置单元Metastore的用户名和密码。
注意:您需要 mysql $ HIVE_HOME / lib 和
$ HADOOP_HOME / lib 中的-jdbc-connector.jarI want to connect MetaStore using the java code. I have no idea how to set configuration setting in Hive-Site.xml file and where I'll post the Hive-Site.xml file. Please help.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; public class HiveMetastoreJDBCTest { public static void main(String[] args) throws Exception { Connection conn = null; try { HiveConf conf = new HiveConf(); conf.addResource(new Path("file:///path/to/hive-site.xml")); Class.forName(conf.getVar(ConfVars.METASTORE_CONNECTION_DRIVER)); conn = DriverManager.getConnection( conf.getVar(ConfVars.METASTORECONNECTURLKEY), conf.getVar(ConfVars.METASTORE_CONNECTION_USER_NAME), conf.getVar(ConfVars.METASTOREPWD)); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery( "select t.tbl_name, s.location from tbls t " + "join sds s on t.sd_id = s.sd_id"); while (rs.next()) { System.out.println(rs.getString(1) + " : " + rs.getString(2)); } } } }
解决方案Add these lines in your hive-site.xml:
<property> <name>hive.metastore.local</name> <value>true</value> </property> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost:3306/hive</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>hiveuser</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>hivepass</value> </property>
In
jdbc:mysql://localhost:3306/hive
,3306
is your default mysql port;hive
is our mysql database name for hive metastore. Changehiveuser
to your mysql hive username andhivepass
to your mysql hive password.Do this step in terminal, if you haven't created a database for hive metastore in mysql:
mysql -u root -p
Enter your mysql root password.
mysql> create database hive;
mysql> create user 'hiveuser'@'%' IDENTIFIED BY 'hivepass';
mysql> GRANT all on *.* to 'hiveuser'@localhost identified by 'hivepass';
mysql> flush privileges;
Here,
hiveuser
andhivepass
are whatever username and password you give for hive metastore respectively.NOTE: You need to have mysql-jdbc-connector.jar in $HIVE_HOME/lib and $HADOOP_HOME/lib
这篇关于如何在Hive-Site.xml文件中为配置单元Metastore连接设置配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!