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;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值