jdbc连接Hive

话不多说撸代码

public void createdriver(){
    authKrb5();
 try{
     //第一步:把JDBC驱动通过反射的方式加载进来
     Class.forName(driver);
     /**
      *第二步:通过JDBC建立和Hive的连接器,默认端口是10000,默认用户名和密码都为空
      */
     conn = DriverManager.getConnection(url, user, password);
     /**
      * 第三步:创建Statement句柄,基于该句柄进行SQL的各种操作;
      */
     stmt = conn.createStatement();
 }catch (Exception e){
     e.printStackTrace();
 }
}  
   /**
     * 接下来就是SQL的各种操作;
     * 第4.1步骤:建表Table,如果已经存在的话就要首先删除;
     */
    public void creattable(String sql,String tableName) {
        try {
//            String tableName = "tdw_user_music_d";
            stmt.execute("drop table if exists " + tableName);
//            sql="create table if not exists tdw_user_music_d(province string,serv_number string," +
//                    "imei string,viz_music_class_cont_cat_code string,music_lang_cont_cat_code string,music_emot_cont_cat_code string," +
//                    "music_time_cont_cat_code string,imsi string,city string,req_cnt bigint)" +
//                    "row format delimited fields terminated by '|' stored as rcfile";
            stmt.execute(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     *第4.2步骤:查询建立的Table;
     */
    public void showtable(String sql)    {
        try {
//            sql = "show tables '" + tableName + "'";
            System.out.println("Running: " + sql);
            res = stmt.executeQuery(sql);
            if (res.next()) {
                System.out.println(res.getString(1));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 第4.3步骤:查询建立的Table的schema;
     * */
    public void describetable(String sql){
        try {
//            sql = "describe " + tableName;
            System.out.println("Running: " + sql);
            stmt.executeQuery("set tez.queue.name=root.bdoc.renter_1.renter_6.dev_12");
            res = stmt.executeQuery(sql);
            while (res.next()){
                System.out.println(res.getString(1) + "\t" + res.getString(2));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
/**
 * 第4.4步骤:加载数据进入Hive中的Table;
 * */
public  void inserttable(String sql){
    createdriver();
    try {
        stmt.execute("set tez.queue.name=root.bdoc.renter_1.renter_6.dev_12");
        stmt.execute("set mapred.max.split.size=536870912");
        stmt.execute("set mapred.min.split.size=536870912");
        stmt.execute("set mapred.min.split.size.per.nod=536870912");
        stmt.execute("set mapred.min.split.size.per.nod=536870912");
        stmt.execute("set hive.exec.parallel=true");
        stmt.execute("set hive.exec.parallel.thread.number=8");
        stmt.execute("set hive.merge.mapfiles=true");
        stmt.execute("set hive.merge.mapredfiles=true");
        stmt.execute("set hive.merge.size.per.task=536870912");
        stmt.execute("set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat");
        stmt.execute("set hive.exec.reducers.bytes.per.reducer=536870912");
        stmt.execute("set hive.exec.compress.intermediate=true");
        stmt.execute("set hive.exec.reducers.max=1000");
        LOG.info("Running: " + sql) ;
        stmt.execute(sql);
        LOG.warn("Running:  " + sql+"===============执行成功") ;
    }catch (Exception e){
        LOG.error("\n\n***********");
        LOG.error(sql+"\n\n执行失败"+e);
        LOG.error("***************************\n\n");
        CALLBACK_SQL.put(sql,1);
    }finally {
        try {
            if(stmt !=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            LOG.error("",e);
        }
    }
}
public void executeTransition(List<String> list) {
String driver = GetProperties.getInstance().getProperyValue(GLOBAL_PROPERTY_FILE,"driver");
String url = GetProperties.getInstance().getProperyValue(GLOBAL_PROPERTY_FILE,"url");
String user = GetProperties.getInstance().getProperyValue(GLOBAL_PROPERTY_FILE,"user");
String password = GetProperties.getInstance().getProperyValue(GLOBAL_PROPERTY_FILE,"password");
System.out.println("driver="+driver+"\nurl="+url);
//list集合立放的是SQL语句集合
List<String> SQLlist = list;
ExecutorService exec = Executors.newFixedThreadPool(this.threadCount);
for (String strSQL : SQLlist) {
    ExcuteSQLDate ExcSQL=new ExcuteSQLDate(strSQL,driver,url,user,password);
    exec.execute(ExcSQL);
    try {
        Thread.sleep(60000);
    } catch (InterruptedException e) {
        LOG.error("",e);
    }
}
exec.shutdown();
}
/**权限认证
**/
public static void authKrb5(){
    //设置jvm启动时krb5的读取路径参数
    System.setProperty("java.security.krb5.conf", "PATH");
    //配置kerberos认证
    Configuration conf = new Configuration();
    conf.setBoolean("hadoop.security.authorization", true);
   conf.set("hadoop.security.authentication", "kerberos");
   //        System.out.println(System.getProperty("java.security.krb5.conf"));
    UserGroupInformation.setConfiguration(conf);
    try {
        UserGroupInformation.loginUserFromKeytab("用户/集群, "PATH");
    } catch (IOException e) {
        e.printStackTrace();
    }
    //        System.out.println("Succeeded in authenticating through Kerberos!");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Barry Lzq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值