使用jdbc无法访问hive数据库的问题

数据库访问的JDBC通用格式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 写一个工具类
 * 能够连接jdbc   hive支持sql语句需要加载mysql的驱动
 * 
 *
 *
 */
public class HiveUtils {
//Hive的驱动
	private static String driver = "org.apache.hive.jdbc.HiveDriver";
	//Oracle数据库: oracle.jdbc.OracleDriver
	
	//Hive的URL地址
	private static String url = "jdbc:hive2://192.168.128.111:10000/default";
	
	//注册数据库的驱动
	static{
		try{
			Class.forName(driver);
		}catch(Exception ex){
			throw new ExceptionInInitializerError(ex);
		}
	}

	//获取数据库Hive的链接
	public static Connection getConnection(){
		try{
			return DriverManager.getConnection(url,"hive","hive");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return null;
	}

	//释放资源
	public static void release(Connection conn,Statement st,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				rs = null;
			}
		}
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				st = null;
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				conn = null;
			}
		}
	}
}

测试JDBC程序获取想要的数据:

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

public class HiveDemo {

public static void main(String[] args) {
	// 执行插叙 select * from emp1;
	//查询员工信息
		String sql = "select * from emp1";
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try{
			conn = HiveUtils.getConnection();
			
			//得到SQL的运行环境
			st = conn.createStatement();
			
			//运行SQL
			rs = st.executeQuery(sql);
			
			while(rs.next()){
				//姓名和薪水
				String ename = rs.getString("ename");
				double sal = rs.getDouble("sal");
				System.out.println(ename+"\t"+sal);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			HiveUtils.release(conn, st, rs);
		}
	}
}

异常1:

Could not open client transport with JDBC Uri: jdbc:hive2://192.168.128.111:10000/default: java.net.ConnectException: Connection refused: connect

方案:连接hive数据库通过jdbc不能直接访问,需要通过hive服务,新版本使用hive service2 老版本使用Thrift Client;直接启动hiveservice2 &

再次执行程序,抛出一个异常;

java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous
使用jdbc无法访问hive数据库的问题_第1张图片

修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项,使用每个用户或者组都可以访问hdfs

/root/training/hadoop-2.7.3/etc/hadoop
vi core-site-xml

  hadoop.proxyuser.root.hosts
  *



  hadoop.proxyuser.root.groups
  *

如果再次发生异常如下:

Could not open client transport with JDBC Uri: jdbc:hive2://192.168.128.111:10000/default: Failed to open new session: java.lang.RuntimeException: org.apache.hadoop.security.AccessControlException: Permission denied: user=anonymous, access=EXECUTE, inode="/tmp/hive":root:supergroup:drwx-----

方案1:修改hdfs 目录权限检查:

vi hdfs-site.xml  //关闭权限检查


  dfs.permissions
  false

方案2:修改tmp的权限:

drwx------   - root supergroup          0 2018-07-02 10:45 /tmp  改为  hdfs hadoop fs -chmod -R 777  /tmp 

最终得到:

2018-07-02 11:48:43,559 main WARN Unable to instantiate org.fusesource.jansi.WindowsAnsiOutputStream
2018-07-02 11:48:43,567 main WARN Unable to instantiate org.fusesource.jansi.WindowsAnsiOutputStream
2018-07-02T11:48:43,752 INFO [main] org.apache.hive.jdbc.Utils - Supplied authorities: 192.168.128.111:10000
2018-07-02T11:48:43,756 INFO [main] org.apache.hive.jdbc.Utils - Resolved authority: 192.168.128.111:10000    
SMITH	800.0
ALLEN	1600.0
WARD	1250.0
JONES	2975.0
MARTIN	1250.0
BLAKE	2850.0
CLARK	2450.0
SCOTT	3000.0
KING	5000.0
TURNER	1500.0
ADAMS	1100.0
JAMES	950.0
FORD	3000.0
MILLER	1300.0

你可能感兴趣的:(问题解决方案:,JDBC,Hive)