0
votes

I can get the metastore table information on my local mysql metastore setup with hive using the below program.

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 MetaStoreTest {

    public static void main(String[] args) throws Exception {

        Connection conn = null;
        try {
            HiveConf conf = new HiveConf();
            conf.addResource(new Path("/home/hadoop/hive-0.12.0/conf/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));
            }
        }
        finally {
            if (conn != null) {
                conn.close();
            }
        }

    }
}

Currently am trying the above program to get the metastore tables from a remote metastore using thrift.In my current hive-site.xml i have these params,

hive.metastore.uris = thrift://host1:9083, thrift://host2:9083
hive.metastore.local = false

There is no params for :

javax.jdo.option.ConnectionDriverName, javax.jdo.option.ConnectionUserName, javax.jdo.option.ConnectionPassword

So, how can i fetch the metatable information over thrift protocol. I am running the above code on host2. Please suggest.

Also when i run the above code, it shows information on :

METASTORE_CONNECTION_DRIVER as derby connection embedded driver, 
METASTORECONNECTURLKEY as jdbc:mysql://<host name>/<database name>?createDatabaseIfNotExist=true, 
METASTORE_CONNECTION_USER_NAME as "APP" and 
METASTOREPWD as "mine"
1
Seems like you've got a bit of a mismatch: your connection URL is a MySQL schema, but you've specified the Derby JDBC driver. Those two probably don't go together well. - Bryan Pendleton

1 Answers

1
votes

replace this section of your code :

conf.addResource(new Path("/home/hadoop/hive-0.12.0/conf/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));

with the below :

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://host2:3306/metastore", "urusername", "urpassword");

[note :Change the metastore dbname, user and password accordingly as per your setup.]

now :

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("show tables");
while (rs.next()) {
   System.out.println("Tables:" +rs.getString(1));
}

See are you getting the metastore table names or not.