jdbc方式连接hive报错解决

错误:

java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException)
User abcde is not allowed to impersonate anonymous 错误。

解决方案:

修改hadoop的配置文件,给用户abcde赋值一个权限,打开hadoop的配置文件etc/hadoop/core-site.xml,注意这里的abcde要换成你错误中提示的那个用户名,然后重启hadoop

     
    hadoop.proxyuser.abcde.hosts     
    * 
 
     
    hadoop.proxyuser.abcde.groups     
    * 

然后修改hive的配置文件conf/hive-site.xml


     
        hive.server2.thrift.port 
        10000 
    
    
       hive.server2.thrift.bind.host
       192.168.0.1
    

然后启动hive的服务.

mac$ ./hiveserver2

测试用例:

package com.hive.ut;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcHiveUt {

    public static Connection getHiveInstance() throws Exception{
        Class.forName("org.apache.hive.jdbc.HiveDriver") ;
        Connection con = DriverManager.getConnection("jdbc:hive2://10.255.168.23:10000/prod_risk", "hadoop", "hadoop");
        return con;
    }

    public static void main (String args[]) throws Exception{

        String sql = "select * from test1" ;
        Connection myHiveCon = JdbcHiveUt.getHiveInstance();

        Statement stHive = myHiveCon.createStatement();
        ResultSet rs = stHive.executeQuery(sql);
        while (rs.next())
            System.out.println(rs.getString("test1.name") + " , " + rs.getDouble("test1.salary"));
        System.out.println();
    }
}

 

你可能感兴趣的:(hive)