Java 需要使用Jdbc程序访问Hive元数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21131461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to access Hive metadata tables using Jdbc program
提问by user2176576
Need to access Hive metadata tables using Jdbc program.What exactly does the Metastore actually store and how can I access it?
需要使用 Jdbc 程序访问 Hive 元数据表。Metastore 究竟存储了什么,我如何访问它?
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.
所以我得到了表的列表,但我想知道这些信息存储在哪个表中,以便我可以直接从该表中选择而不是触发 Hive 命令。
Also my Metastore is configured in PostGreSQL.(if that helps!) Thanks Hitz
我的 Metastore 也是在 PostGreSQL 中配置的。(如果有帮助!)谢谢 Hitz
采纳答案by Nigel Tufnel
Metastore, well, stores meta information on your Hive tables - names, partitions, columns, SSNs, SerDes, etc.
Metastore 将元信息存储在您的 Hive 表上 - 名称、分区、列、SSN、SerDes 等。
Metastore connection parameters are stored in hive-site.xml
(relevant properties are named javax.jdo.option.ConnectionURL, javax.jdo.option.ConnectionUserName, and javax.jdo.option.ConnectionPassword)
Metastore 连接参数存储在hive-site.xml
(相关属性命名为 javax.jdo.option.ConnectionURL、javax.jdo.option.ConnectionUserName 和 javax.jdo.option.ConnectionPassword)
You can connect to the Postgres database using the connection parameters and run, say, these commands:
您可以使用连接参数连接到 Postgres 数据库并运行,例如,这些命令:
-- that'll get you the names of all Hive tables
SELECT tbl_name FROM TBLS;
-- that'll list all Metastore tables
\d
回答by Lorand Bendig
You can query the metastore DB through JDBC.
E.g: list table names and their location on HDFS:
您可以通过 JDBC 查询 Metastore DB。
例如:列出表名及其在 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 existsan 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)
目前存在对metastore ER图,但可能无法达到最新的,所以我建议你运行metastore DDL脚本(蜂巢0.12),在测试模式,创造这些新的ER图表。(例如使用PowerArchitect)