Hive通過Jdbc獲取表的字段信息


參考代碼如下:

 /**
     * 按順序返回字段
     * desc table的返回結果形式如下:
     hive> describe ind01acoM;
     OK
     acq_ins_id_cd           string
     cups_sig_card_in        string
     resv                    string
     ins_id_cd               string
     hp_settle_dt            string

     # Partition Information
     # col_name              data_type               comment

     ins_id_cd               string
     hp_settle_dt            string
     */
    public Map<String, String> queryAllColumnsAndType(String table) throws Exception {
        //Map<String, String> allColumnsAndType = new CaseInsensitiveMap();
        //為了保證字段順序
        Map<String, String> allColumnsAndType = new LinkedHashMap<>();
        Connection connection = null;
        try{
            connection = getConnection();
            ResultSet rs = connection.createStatement().executeQuery("describe " + table);
            if (rs != null) {
                while (rs.next()) {
                    String col_name = rs.getString("col_name");
                    if(!StringUtils.isBlank(col_name)){
                        allColumnsAndType.put(rs.getObject("col_name").toString(), rs.getObject("data_type").toString());
                    }else break;
                    //System.out.println(rs.getString("col_name") + "\t" + rs.getString("data_type"));
                }
            }
            //去除分區列
            allColumnsAndType.remove("hp_settle_dt");
            allColumnsAndType.remove("ins_id_cd");
            return allColumnsAndType;
        }catch(Exception e){
            e.printStackTrace();
            logger.error("獲取字段-類型失敗,表:{}",table);
            throw e;
        }finally {
            closeConn(null, null, connection);
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM