【问题标题】:How to get hive table name based on HDFS location path? with out connecting to metastore如何根据 HDFS 位置路径获取 hive 表名?没有连接到 Metastore
【发布时间】:2014-04-16 17:41:25
【问题描述】:

我知道如何通过 Meta-store 根据 HDFS 位置获取 hive 表名。例如,如果我需要获取 HDFS 位置的表名 hdfs://xyz.com:8020/user/hive/warehouse/test

  1. 我将使用 JDBC 连接到 Hive Metastore。
  2. 对表TBLSSDS运行查询,SDS.location将具有表的位置值,并得到TBLS.tbl_name

但是,我需要其他方法来获取表名吗?

有可能吗?

【问题讨论】:

    标签: hadoop hive


    【解决方案1】:

    Hive Warehouse 位置中的目录名称是表名称。

    例如在Hive中创建表testTable,对应的Hive仓库目录中会创建一个同名目录。

    此外,如果您在 Hive 表上创建分区,每个分区将映射到您的 testTable 目录中的一个子目录,即 <hive_warehouse_path>/testTable/<partition>。特定分区下的所有数据都存储在分区子目录下的文件中。这是 Hive 在 HDFS 上管理其数据的方式。当然,它将表模式存储在 Metastore 上,但实际数据如上存储在 HDFS 中。

    在您的问题中,您表示要获取 HDFS 位置 hdfs://xyz.com:8020/user/hive/warehouse/test 的表名。在这种情况下,表名应该是test

    /user/hive/warehouse 也有可能,这是 hive 用于存储表数据的默认仓库位置,在 hive-default.xml 中定义,可能已被覆盖,hive 实际上可能正在为其仓库使用不同的位置。您应该在您的环境中检查hive-site.xml 以确定配置单元元存储位置。

    【讨论】:

    • 感谢 Chaos 的回复,但是在创建 hive 表时,我们可以在 create 语句中使用“location”参数将任何 HDFS 目录路径提及为 hive 表位置。如果我选择了默认配置单元仓库目录以外的任何其他位置,在这种情况下,您的解决方案将失败。
    【解决方案2】:

    通过这种方式,我们可以获得提供的 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());
          }
     }                              
    

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      相关资源
      最近更新 更多