Maven hive-jdbc教程

因爲項目中須要用到hive-jdbc從數據倉庫拉數據下來,因此簡單的學一下hive,hive數據倉庫建構在hadoop集羣之上,數據存在hdfs文件系統中,hive中執行的操做會裝換成mapreduce做業進行執行,hive支持相似SQL的語言HQL,hive採用元數據對錶進行管理,元數據有三種存放模式:嵌入模式,遠程模式,本地模式;hive提供了強大的編程接口,hive jdbc能夠讓你如使用普通的jdbc通常來操做hive表以及數據。java

1.添加依賴sql

<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鏈接hiveapache

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工具類oop

寫完了再貼上來url

@落雨
ae6623.cncode

相關文章
相關標籤/搜索