Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to access Hive metadata tables using Jdbc program

Need to access Hive metadata tables using Jdbc program.What exactly does the Metastore actually store and how can I access it?

I tried doing this:

    sql="show tables";
Statement stmt = con.createStatement();

System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}

So I get the list of tables, but I want to know which table is this information stored in so that I can Directly Select from that table instead of firing a Hive command.

Also my Metastore is configured in PostGreSQL.(if that helps!) Thanks Hitz

like image 361
user2176576 Avatar asked Sep 04 '26 18:09

user2176576


1 Answers

You can query the metastore DB through JDBC.
E.g: list table names and their location on HDFS:

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));
            }
        }
        finally {
            if (conn != null) {
                conn.close();
            }
        }

    }
}

There exists an ER diagram about the metastore, but it might not be up-to-date, therefore I'd suggest you to run the metastore DDL script (Hive 0.12), in a test schema, and create the new ER diagram from these tables. (E.g with PowerArchitect)

like image 179
Lorand Bendig Avatar answered Sep 07 '26 08:09

Lorand Bendig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!