Java HIVE 使用Jdbc连接Hive

1,使用Jdbc方式链接hive,首先需要启动hive的Thrift Server,否则会导致错误

hive --service hiveserver   是两”-“,

Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused

2,简单的Java代码实现;

----通过Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");加载hive驱动

----通过Connection conn = DriverManager.getConnection("jdbc:hive://localhost:10000/default","","");建立与数据库的连接

eg:

package hive.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HiveJdbcDriver {
    
    public static void main(String[] args) throws Exception {
        Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection("jdbc:hive://localhost:1000/default","","");
        Statement stmt = conn.createStatement();
        String tablename = "u_data_new";
        String quary_sql = "select weekday,count(*) from " + tablename + " group by weekday";
        ResultSet rs = stmt.executeQuery(quary_sql);
        while(rs.next()){
            System.out.println("weekday: "+rs.getInt(1)+"count: "+rs.getInt(2));
        }
    }
}

result:

weekday: 2    count: 13579
weekday: 3    count: 14430
weekday: 4    count: 15114
weekday: 5    count: 14743
weekday: 6    count: 18229
weekday: 7    count: 11651

hive执行结果:

hive> select weekday,count(*) from  u_data_new  group by weekday;



发布了253 篇原创文章 · 获赞 135 · 访问量 126万+
展开阅读全文

JDBC连接hive连接超时

11-12

hiveserver2启动了,然后日志也正常,但是用kettle连接或者自己的java代码用jdbc连接都是报错,报错日志如下: java.sql.SQLException: Could not open connection to jdbc:hive2://192.168.162.129:10000/hivedb: java.net.ConnectException: Connection timed out: connect at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:206) at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:178) at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at com.ljq.hive.HiveJdbcClient.run(HiveJdbcClient.java:21) at com.ljq.hive.HiveJdbcClient.main(HiveJdbcClient.java:46) Caused by: org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection timed out: connect at org.apache.thrift.transport.TSocket.open(TSocket.java:185) at org.apache.thrift.transport.TSaslTransport.open(TSaslTransport.java:248) at org.apache.thrift.transport.TSaslClientTransport.open(TSaslClientTransport.java:37) at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:203) ... 6 more Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at org.apache.thrift.transport.TSocket.open(TSocket.java:180) ... 9 more error 实在是不知道怎么搞了 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览