1. 程式人生 > >JDBC連線impala Java連線Hive JDBC連線Hive

JDBC連線impala Java連線Hive JDBC連線Hive

JDBC連線impala Java連線Hive JDBC連線Hive    

大家都知道impala是用hive的資料來源,但速度要比hive快的多,在秒級即可完成任務查詢

package cn.com.tengen.hive;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
 
public class ImpalaJdbcTest {
     
    private static String driverName =
                   "com.cloudera.impala.jdbc41.Driver";
   
    public static void main(String[] args)
                            throws SQLException {
        try {
            Class.forName(driverName);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        Connection con = DriverManager.getConnection(
                           "jdbc:impala://quickstart.cloudera:21050/default", "cloudera", "cloudera");
        Statement stmt = con.createStatement();
        StringBuffer sb = new StringBuffer("(88,'helen','女')");
        StringBuffer sb2 = new StringBuffer("(88,1002088)");
        int number = 900000;
        for(int i=number;i<number+3000;i++) {
        	sb.append(",("+i+",'lucky"+i+"','男')");
        	sb2.append(",("+i+","+(1000000+i+(i+"").hashCode()%2*1000)+")");
        }
        stmt.execute("INSERT INTO default.test1 VALUES "+sb.toString());
        stmt.execute("INSERT INTO default.test VALUES "+sb2.toString());
        String sql = "";
       ResultSet res = null;
 
// 
//        sql = "select * from test t1 join test1 t2 on t1.id = t2.id";
//        res = stmt.executeQuery(sql);
//        while (res.next()) {
//            System.out.println(String.valueOf(res.getInt(1)) 
//            		+ "\t"+ res.getString(2)
//            		+ "\t"+ res.getString(3)
//            		+ "\t"+ res.getString(4)
//            		+ "\t"+ res.getString(5)
//            		);
//        }
// 
        sql = "select count(t1.id) from test t1 join test1 t2 on t1.id = t2.id";
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1));
        }
        
//        stmt.close();
    }
}