1. 程式人生 > >Hive----JDBC工具類的建立使用(包括HIVEsql工具類)---IDEA開發

Hive----JDBC工具類的建立使用(包括HIVEsql工具類)---IDEA開發

 

Hive-----JDBC---util

package com.www.hive.util;

import java.sql.*;
import java.util.Date;
import java.util.*;
import java.util.List;

public class HiveUtil {

    //成員變數
    private Connection connection=null;
    private Statement

statemet=null;
    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;

public class HiveSQLutil {

/*
* 1.通過資料庫名,表名,查詢某些列或者函式操作的列,返回結果
* */
    public String SCAN(String databaseName,String tableName,String[] columns,String column,String type){
        String sql="";
        String temp="";
        for (String c:columns) {
            temp+=c+",";
        }
        sql="select " + temp + type +"(" + column + ")" + " from " + databaseName + "." + tableName + " group by " + temp.substring(0,temp.length() - 1);
        return sql;
    }

/*
* 2.獲得前n條資料
* */
    public String getTableDataByN(String database, String table, String[] columns, int n){
        String cls = "";
        for (String c : columns){
            cls += c + ",";
        }
        cls = cls.substring(0,cls.length() - 1);
        String sql = "select " + cls + " from " + database + "." + table + " limit " + n;
        return sql;
    }

    /**
     * 3.獲得指定資料表的前百分之n的資料
     *
     */
    public String getDataByNPercent(String database, String table, String[] columns, int n){
        String cls = "";
        for (String c : columns){
            cls += c + ",";
        }
        cls = cls.substring(0,cls.length() - 1);
        String sql = "select " + cls + " from " + database + "." + table + " tablesample(" + n + " percent)";
        return sql;
    }

    /**
     * 4.隨機抽樣
     *
     */
    public String getRandomDataN(String database, String table, String[] columns, int n){
        String cls = "";
        for (String c : columns){
            cls += c + ",";
        }
        cls = cls.substring(0,cls.length() - 1);
        String sql = "select " + cls + ",RAND() r from " + database + "." + table + " order by r limit " + n;
        return sql;
    }

    /**
     * 條件查詢
     * where id=? and name=?
     */
    public String getDataByCondition(String database, String table, String[] columns, String[] conditions, String[] values){
        String c = "";
        for (int i = 0;i < columns.length;i ++){
            c += " and " + columns[i] + " " + conditions[i] + " " + values[i];
        }
        String sql = "select * from " + database + "." + table + " where 1 = 1" + c;
        return sql;
    }
}

 

 

Test

package com.www.hive.test;

import com.www.hive.util.HiveUtil;
import com.www.hive.utilSql.HiveSQLutil;

import java.sql.ResultSet;
import java.sql.SQLOutput;
import java.util.*;

public class Test {
    public static void main(String[] args) {
        HiveUtil hiveUtil=new HiveUtil();
        List<String> list=new ArrayList();


        //1.獲取資料庫下的所有表
//        list=hiveUtil.getTables("test");
//
//        for(Object li:list){
//            System.out.println(li);
//        }
        System.out.println(".........................");
        //2.有返回值查詢
//        try {
//            ResultSet re=hiveUtil.getResult("test","select * from " +
//                    "orders limit 10",null);
//            while(re.next()){
//               list.add(re.getString(1)+" "+
//                       re.getString(2)+" "+
//                       re.getString(3)+" "+
//                       re.getString(4));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//        for(String li:list){
//            System.out.println(li);
//        }
        //3.查看錶結構
        //list=hiveUtil.getDesc("test","orders");
        //4.遍歷臨時的新表
        //list=hiveUtil.getNewTable("select * from test.orders limit 10",new int[]{1,3});
       //5.隨機抽樣
        //list=hiveUtil.getRandomData("test","make",10,0);
        String sql="";
        HiveSQLutil hi=new HiveSQLutil();

        sql=hi.SCAN("test","purchaser",new String[]{"age"},"age","count");

        list=hiveUtil.getDataBySql(sql);

        hiveUtil.catDesc(list);

        hiveUtil.close();
    }
}