HiveConf hc = new HiveConf(yourclass.class);
hc.set("hive.metastore.local", "false");
hiveuris = "thrift://xyz.com:9083";
hc.setVar(HiveConf.ConfVars.METASTOREURIS,hiveuris);
//hiveuri is the property "hive.metastore.uris" value from hive-site.xml
hc.setBoolVar(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL, false);
HiveMetaStoreClient hiveClient = HCatUtil.getHiveClient(hiveConf);
//get all tables
List<String> tables = hiveClient.getAllTables("default");//default is databasename
//loop through tables and complare the needed path
String path = "hdfs://xyz.com:8020/user/hive/warehouse/test"; //hdfs path to find table name
//find talbe for above path
for (String table:tables){
Table ht = HCatUtil.getTable(hiveClient, "default", table);
if (path.equals(ht.getMetadata().get("location")) ){
System.out.println("Found table name:"+ht.getTableName());
}
}
2条答案
按热度按时间vmpqdwk31#
配置单元仓库位置中的目录名是表名。
例如,如果您创建一个表
testTable
在配置单元中,相应地在配置单元仓库目录中创建一个同名的目录。此外,如果在配置单元表上创建分区,则每个分区都将Map到配置单元中的子目录
testTable
目录,即<hive_warehouse_path>/testTable/<partition>
. 特定分区下的所有数据都存储在分区子目录下的文件中。这是hive在hdfs上管理数据的方式。当然,它将表模式存储在元存储上,但实际数据如上所述存储在hdfs中。在您的问题中,您指出要获取hdfs位置的表名
hdfs://xyz.com:8020/user/hive/warehouse/test
. 在这种情况下,表名应该是test
.还有一种可能性是
/user/hive/warehouse
,这是配置单元用于存储表数据的默认仓库位置,在中定义hive-default.xml
可能已被覆盖&hive实际上可能正在为其仓库使用不同的位置。你应该检查一下hive-site.xml
在您的环境中确定Hive元存储的位置。8yoxcaq72#
通过这种方式,我们可以获得hdfs位置的表名(提供了…:)