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

相关文章
相关标签/搜索