如何基于hdfs位置路径获取配置单元表名?没有连接到元存储

0yycz8jy  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(344)

我知道如何通过元存储基于hdfs位置获取配置单元表名。例如,如果我需要获取hdfs位置的表名 hdfs://xyz.com:8020/user/hive/warehouse/test 我将使用jdbc连接到配置单元元存储。
对表运行查询 TBLS 以及 SDS , SDS.location 将具有表的位置值,并获取 TBLS.tbl_name .
但是,我需要其他方法来获取表名吗?
有可能吗?

vmpqdwk3

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元存储的位置。

8yoxcaq7

8yoxcaq72#

通过这种方式,我们可以获得hdfs位置的表名(提供了…:)

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

相关问题