Hive學習筆記 6 Hive的JDBC連線

土豆拍死馬鈴薯發表於2017-10-09

1、配置Hadoop的配置檔案/hadoop-2.6.5/etc/hadoop/

 <property>
            <name>hadoop.proxyuser.root.hosts</name>
            <value>127.0.0.1,localhost,pc1</value>
        </property>
    <property>
            <name>hadoop.proxyuser.root.groups</name>
            <value>*</value>
        </property>

2、啟動Hive遠端服務

./hive --service hiveserver2


3、準備好需要的jar包

/chenjie/hadoop-2.6.5/share/hadoop/common下找到hadoop-common-2.6.5.jar
/chenjie/hadoop-2.6.5/share/hadoop/common/lib下找到commons-collections-3.2.2.jar  commons-logging-1.1.3.jar  slf4j-api-1.7.5.jar

/chenjie/apache-hive-2.3.0-bin/lib下找到hive-common-2.3.0.jar   hive-exec-2.3.0.jar  hive-jdbc-2.3.0.jar   hive-metastore-2.3.0.jar   hive-service-2.3.0.jar

httpclient-4.4.jar  httpcore-4.4.jar   libfb303-0.9.3.jar


4、程式碼編寫

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

public class JDBCUtils {

	//private static String driver = "org.apache.hadoop.hive.jdbc.HiveDriver";
	private static String driver = "org.apache.hive.jdbc.HiveDriver";
	private static String url = "jdbc:hive2://pc1:10000/default";
	
	//註冊驅動
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	//獲取連線
	public static Connection getConnection(){
		try {
			return DriverManager.getConnection(url,"chenjie","chenjie");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				st = null;
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				conn = null;
			}
		}
	}
}

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


public class HiveJDBCDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		
		String sql = "select * from student4";
		try {
			//獲取連線
			conn = JDBCUtils.getConnection();
			System.out.println("1");
			//建立執行環境
			st = conn.createStatement();
			System.out.println("2");
			//執行HQL
			rs = st.executeQuery(sql);
			System.out.println("3");
			//處理資料
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int age = rs.getInt("age");
				System.out.println(id+"\t"+name);
			}
			System.out.println("4");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//JDBCUtils.release(conn, st, rs);
		}
	}

}

5、執行


相關文章