需要使用Jdbc程序访问Hive元数据表

时间:2014-01-15 07:30:56

标签: java eclipse jdbc hadoop hive

需要使用Jdbc程序访问Hive元数据表.Metastore究竟存储了什么以及如何访问它?

我试过这样做:

    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));
}

所以我得到了表的列表,但我想知道这个信息存储在哪个表中,以便我可以从该表中直接选择而不是激活Hive命令。

我的Metastore也是在PostGreSQL中配置的。(如果有帮助!) 谢谢 希茨

2 个答案:

答案 0 :(得分:3)

您可以通过JDBC查询Metastore数据库。
例如:列出表名称及其在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();
            }
        }

    }
}

exists关于Metastore的ER图,但它可能不是最新的, 因此,我建议你在测试中运行metastore DDL script(Hive 0.12) 架构,并从这些表创建新的ER图。 (例如PowerArchitect

答案 1 :(得分:0)

Metastore会在您的Hive表中存储元信息 - 名称,分区,列,SSN,SerDes等。

Metastore连接参数存储在hive-site.xml中(相关属性名为javax.jdo.option.ConnectionURL,javax.jdo.option.ConnectionUserName和javax.jdo.option.ConnectionPassword)

您可以使用连接参数连接到Postgres数据库,然后运行这些命令:

-- that'll get you the names of all Hive tables
SELECT tbl_name FROM TBLS;

-- that'll list all Metastore tables
\d