Maven hive-jdbc教程


由於項目中需要用到hive-jdbc從數據倉庫拉數據下來,所以簡單的學一下hive,hive數據倉庫建構在hadoop集群之上,數據存在hdfs文件系統中,hive中執行的操作會裝換成mapreduce作業進行執行,hive支持類似SQL的語言HQL,hive采用元數據對表進行管理,元數據有三種存放模式:嵌入模式,遠程模式,本地模式;hive提供了強大的編程接口,hive jdbc可以讓你如使用普通的jdbc一般來操作hive表以及數據。

1.添加依賴


        <dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.6.4</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.4</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.1.0</version>
			<exclusions>
				<exclusion>
					<artifactId>
						pentaho-aggdesigner-algorithm
					</artifactId>
					<groupId>org.pentaho</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId>
			<version>1.1.0</version>
		</dependency>

2.jdbc連接hive


public class TestHive {
        private static String driverName = "org.apache.hive.jdbc.HiveDriver";//jdbc驅動路徑
        private static String url = "jdbc:hive2://hiveserver.xxx.com:10000/dbName";//hive庫地址+庫名
        private static String user = "username";//用戶名
        private static String password = "pwd";//密碼
        private static String sql = "";
        private static ResultSet res;

        public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = getConn();
			System.out.println(conn);
			stmt = conn.createStatement();
                        String tableName="tab_name";//hive表名
                        sql = "select * from " + tableName;
		        System.out.println("Running:" + sql);
		        res = stmt.executeQuery(sql);
		        System.out.println("執行 select * query 運行結果:");
		        while (res.next()) {
		            System.out.println(res.getInt(1) + "\t" + res.getString(2));
		        }

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			try {
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private static Connection getConn() throws ClassNotFoundException,
			SQLException {
		Class.forName(driverName);
		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}
}

3.查詢hive表數據

這個就和普通的jdbc差不太多,也是用sql的方式進行查詢,具體的查詢語法,可以參考hive官網

4.封裝hive工具類

寫完了再貼上來

@落雨
ae6623.cn


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM