1. 程式人生 > >Hive JDBC教程

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官網