我正在编写一个示例程序,使用org.apache.hadoop.hive.ql.Driver类连接到Hive Metastore。示例代码段如下所示
String userName = "test";
HiveConf conf = new HiveConf(SessionState.class);
conf.set("fs.default.name", "hdfs://" + hadoopMasterHost + ":8020");
conf.set("hive.metastore.local","false");
conf.set("hive.metastore.warehouse.dir","/user/hive/warehouse");
conf.set("hive.metastore.uris","thrift://" + hadoopMasterHost + ":9083");
conf.set("hadoop.bin.path", "/usr/hdp/2.2.0.0-2041/hadoop/bin");
conf.set("yarn.nodemanager.hostname", hadoopMasterHost);
conf.set("yarn.resourcemanager.hostname", hadoopMasterHost);
ss = new MyCliSessionState(conf);
ss.out = new PrintStream(System.out, true, "UTF-8");
ss.err = new PrintStream(System.err, true, "UTF-8");
SessionState.start(ss);
driver = new Driver(conf);
query = "show tables";
if (userName == null || userName.isEmpty())
return driver.run(query);
UserGroupInformation ugi = createUgi(userName);
CommandProcessorResponse response = ugi.doAs(new PrivilegedExceptionAction<CommandProcessorResponse>() {
public CommandProcessorResponse run() throws Exception {
CliSessionState ss = null;
ss = new MyCliSessionState(conf);
ss.out = new PrintStream(System.out, true, "UTF-8");
ss.err = new PrintStream(System.err, true, "UTF-8");
// refresh thread local SessionState and Hive
SessionState.start(ss);
Hive.get(conf, true);
return driver.run(query);
}
});
return response;
我可以连接到默认数据库并获取所有表的列表。但是如何连接到其他数据库(默认除外)?我尝试搜索hive配置属性,但找不到一个指定数据库名称。请有人帮帮我。
答案 0 :(得分:1)
看起来你想要艰难地做事,并重新实现Beeline实用程序。对于大多数人来说,这似乎是一种受虐狂的尝试,但我该判断谁呢?
无论如何,此时你必须执行HQL命令,就像其他任何人一样......任何人都应该知道&#34;使用&#34;命令:
driver.run("use " +argDatabase) ;
// check status
driver.run("show tables") ;
// check status, parse output
driver.run("describe extended " +argTable) ;
// check status, parse output