testbook

JDBC AND ODBC

为了使用JDBC连接到Inceptor Server以执行SQL进行数据分析,需要以下jar包:

hive-exec-0.12.0-transwarp.jar
hive-jdbc-0.12.0-transwarp.jar
hive-metastore-0.12.0-transwarp.jar
hive-service-0.12.0-transwarp.jar
libfb303-0.9.0.jar
commons-cli-1.2.jar
commons-logging-1.1.1.jar
hadoop-annotations-2.2.0-transwarp.jar
hadoop-auth-2.2.0-transwarp.jar
hadoop-common-2.2.0-transwarp.jar
hadoop-hdfs-2.2.0-transwarp.jar
log4j-1.2.17.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar

  • 连接到Inceptor server并执行一些SQL语句的代码如下:
  • import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.DriverManager;
    public class inceptorJDBC{
        private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
        public static void main(String[] args) throws SQLException{
            try{
                Class.forName(driverName);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
                System.exit(1);
            }
            //172.16.1.110 is the IP address of Inceptor Server
            Connection connection =
                DriverManager.getConnection("jdbc:transwarp://172.16.1.110:10000/default","","");
            Statement st = connection.createStatement();
            //drop if exists table
            String tableName1 = "gsm";
            String dropIfExistsTable = "drop table if exists " + tableName1;
            st.execute(dropIfExistsTable);
            //create table
            st.execute("set mapred.reduce.tasks=4");
            String createTable = "create table " + tableName1 + "
            tblproperties('cache'='ram','filters'='hashbucket(4):msisdn') as
            select  from gsm_ext distribute by msisdn";
            st.execute(createTable);
            //make queries on existing tables gsm and userinfo
            String tableName2 = "userinfo";
            String sql1 = "select count() from " + tableName2;
            ResultSet res = st.executeQuery(sql1);
            while (res.next()){
                System.out.println(String.valueOf(res.getInt(1)));
            }
            String sql2 = "select msisdn, count(1) as count from " + tableName1 +"
            group by msisdn order by count";
            res = st.executeQuery(sql2);
            while (res.next()) {
                System.out.println(res.getString(1) + "\t" + String.valueOf(res.getInt(2)));
            }
        }
    }