I 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));
}
}
}
}
Regarding Hive-site.xml here is sample from my testing machine. This is for setting up hive metastore with MySql server installed on localhost.
This file you need to place inside
<system_path>/apache-hive-x.xx.x-bin/conf
directoryI do not have much idea about how to use this file in java. But by specifying connection string in java code you can do it as below
Add these lines in your hive-site.xml:
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.