Hive----JDBC工具類的建立使用(包括HIVEsql工具類)---IDEA開發
Hive-----JDBC---util
package com.www.hive.util; private PreparedStatement preparedStatement=null; ResultSet resultSet=null; /** * 1.載入jdbc的驅動 */ static{ try Class.forName("org.apache.hive.jdbc.HiveDriver"); } catch (Exception e) { e.printStackTrace(); } } //2.獲得連結,獲得操作物件 public HiveUtil(){ try { //獲得連結 connection=DriverManager. getConnection("jdbc:hive2://192.168.136.14:10010/","hadoop",""); //獲得操作物件 statemet=connection.createStatement(); } catch (Exception e) { e.printStackTrace(); System.out.println("獲取連結失敗!"); } } //3.獲得資料庫列表 public List<String> getDatebases(){ List list=new ArrayList<String>(); try { preparedStatement=connection.prepareStatement("show databases"); resultSet=preparedStatement.executeQuery(); while(resultSet.next()){ list.add(resultSet.getString(1)); } } catch (Exception e) { e.printStackTrace(); return null; } return list; } //4.切換資料庫 public void ChangeData(String dataName){ try { statemet=connection.createStatement(); statemet.execute("use "+dataName); } catch (SQLException e) { e.printStackTrace(); System.out.println("資料庫不存在!"); } } //5.列出資料庫下的所有表 public List<String> getTables(String dataName){ List list=new ArrayList<String>(); //切換資料庫 try { ChangeData(dataName); preparedStatement=connection.prepareStatement("show tables"); resultSet=preparedStatement.executeQuery(); while(resultSet.next()){ list.add(resultSet.getString(1)); } } catch (Exception e) { e.printStackTrace(); return null; } return list; } //6.* 將傳入的引數進行賦值,構建一個完整的PreparedStatement // * @param paras 傳入的引數 private void getParas(Object[] paras){ if (paras != null){ for (int i = 0;i < paras.length;i ++){ try { preparedStatement.setObject(i + 1,paras[i]); } catch (SQLException e) { e.printStackTrace(); } } } } //7.執行查詢語句有返回結果 public ResultSet getResult(String adtaName,String sql,Object[] para){ try { ChangeData(adtaName); preparedStatement = connection.prepareStatement(sql); getParas(para); return preparedStatement.executeQuery(); } catch (Exception e) { e.printStackTrace(); return null; } } //8.查看錶的結構 public List<String> getDesc(String database,String table){ List<String> list=new ArrayList<String>(); try { preparedStatement =connection.prepareStatement("desc "+database+"."+table); ResultSet re=preparedStatement.executeQuery(); while(re.next()){ list.add(re.getString(1)+":"+re.getString(2)); } } catch (Exception e) { e.printStackTrace(); return null; } return list; } //9.遍歷資訊 public void catDesc(List<String> list){ for (String li:list) { System.out.println(li); } } //10.建立臨時新表返回結果 public List<String> getNewTable(String sql,int[] in){ String tableName="temp_"+new Date().getTime(); try { preparedStatement =connection.prepareStatement("create table temp."+tableName+" as "+sql); preparedStatement.execute(); int size = getDesc("temp",tableName).size(); preparedStatement=connection.prepareStatement("select * from temp."+tableName); resultSet=preparedStatement.executeQuery(); if(in==null){ return getData(resultSet,size); }else{ return getclomData(resultSet,in); } } catch (Exception e) { e.printStackTrace(); return null; } } //11.遍歷表資料與建立臨時表結合使用 private List<String> getData(ResultSet resultSet,int size){ List<String> list = new ArrayList<>(); try { while (resultSet.next()){ String line = ""; for(int i = 0;i < size;i ++){ line += resultSet.getString(i + 1) + ","; } line = line.substring(0,line.length() - 1); list.add(line); } return list; }catch (Exception e) { e.printStackTrace(); return null; } } //12.遍歷指定的資料表與建立臨時表結合使用 private List<String> getclomData(ResultSet resultSet,int[] clumIndex){ List<String> list=new ArrayList<>(); try { while(resultSet.next()){ String line=" "; for(int i=0;i<clumIndex.length;i++){ line+=resultSet.getString(i+1)+" "; } list.add(line); } return list; } catch (Exception e) { e.printStackTrace(); return null; } } //13.對錶資料進行隨機抽樣 public List<String> getRandomData(String databaseName,String tableName,int count,int clom ){ List<String> list=new ArrayList<>(); int size=getDesc(databaseName,tableName).size(); try { preparedStatement=connection.prepareStatement("select *,rand() r from "+databaseName+"."+tableName+" order by r limit "+count); resultSet=preparedStatement.executeQuery(); if(clom==0){ list=getData(resultSet,size); }else{ list=getData(resultSet,count); } return list; } catch (Exception e) { e.printStackTrace(); return null; } } //14.自定義SQL建立為一張表返回結果 public List<String> getDataBySql(String sql){ String tableName="temp_"+new Date().getTime(); List<String> list=new ArrayList<>() ; try { preparedStatement =connection.prepareStatement("create table temp."+tableName+" as "+sql); preparedStatement.execute(); int size = getDesc("temp",tableName).size(); preparedStatement=connection.prepareStatement("select * from temp."+tableName); resultSet=preparedStatement.executeQuery(); list=getData(resultSet,size); return list; } catch (Exception e) { e.printStackTrace(); return null; } } //.關閉資源 public void close(){ try { if(preparedStatement!=null){ preparedStatement.close(); } if(statemet!=null){ statemet.close(); } if(connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); System.out.println("不能關閉資源!"); } } }
|
Hive-----SQL-----util
package com.www.hive.utilSql;
|
Test
package com.www.hive.test;
|