Hive Thrift Server

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yulei_qq/article/details/82224643

Thrift 允许客户端使用包括Java、C++、Ruby和其他很多种语言,通过编程的方式远程访问Hive 

一、启动Thrift Server

通过如下命令启动 Thrift Server

[hadoop@s201 ~]$hive --service hiveserver2 &

启动之后,使用jps 命令可以看到对应的进程RunJar

同时可以使用netstat 命令查看10000端口是否打开并监听连接.

[hadoop@s201 ~]$netstat -anop | grep 10000
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      61819/java           off (0.00/0/0)

以上表示Thrift Server 已启动成功.

二、Java 通过连接Thrift Server 操作Hive数据库

 1、首先是Maven配置

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
    </dependencies>

2、Java 代码

/**
 * 使用jdbc方式连接到hive数据仓库,数据仓库需要开启hiveserver2服务。
 */
public class App {
    public static void main(String[] args) throws  Exception {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.44.128:10000/ts");
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select id , name ,age from t2");
        while(rs.next()){
            System.out.println(rs.getInt(1) + "," + rs.getString(2)) ;
        }
        rs.close();
        st.close();
        conn.close();
    }
}

输出:

1,tom
2,hameimei
3,lilei
4,mayun
1,ssss

猜你喜欢

转载自blog.csdn.net/yulei_qq/article/details/82224643