hiveql驱动程序如何指定数据库名称而不是默认名称

1mrurvl1  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(390)

我正在编写一个示例程序,使用org.apache.hadoop.hive.ql.driver类连接到HiveMetaStore。下面是一个示例片段

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;

我可以连接到默认的数据库并获得所有表的列表。但如何连接到其他数据库(默认数据库除外)?我尝试搜索配置单元配置属性,但找不到指定数据库名称的属性。有人能帮帮我吗。

ve7v8dk2

ve7v8dk21#

看起来您想要以一种艰难的方式完成任务,并重新实现beeline实用程序。对大多数人来说,这似乎是受虐狂的企图,但我该评判谁呢?
无论如何,在这一点上你必须执行hql命令,就像其他人一样。。。任何人都应该知道“使用”命令:

driver.run("use " +argDatabase) ;
// check status
driver.run("show tables") ;
// check status, parse output
driver.run("describe extended " +argTable) ;
// check status, parse output

相关问题