springboot jdbc方式连接hive

准备工作

查看${HIVE_HOME}/conf/hive-site.xml

  <property>
    <name>hive.server2.thrift.bind.host</name>
    <value>master</value>
    <description>Bind host on which to run the HiveServer2 Thrift service.</description>
  </property>
  
  <property>
    <name>hive.server2.thrift.port</name>
    <value>10222</value>
    <description>Port number of HiveServer2 Thrift interface when hive.server2.transport.mode is 'binary'.</description>
  </property>
开启hiveserver2服务
hive --service metastore &
hive --service hiveserver2  > hiveserver2.log 2>&1 &

查看是否成功开启
监听端口测试
netstat -antp|grep 10222
tcp        0      0 0.0.0.0:10222      0.0.0.0:*               LISTEN      1776430/java   
beeline测试

[root@home bin]# ${HIVE_HOME}/beeline 
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/bigdata/binfile/hive-2.3.7/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/bigdata/binfile/hadoop-2.10.0/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Beeline version 2.3.7 by Apache Hive
## 输入连接命令
beeline> !connect jdbc:hive2://master:10222/default
## 没有账号密码就回车回车就好了
Connecting to jdbc:hive2://master:10222/default
Enter username for jdbc:hive2://master:10222/default: 
Enter password for jdbc:hive2://master:10222/default: 
Connected to: Apache Hive (version 2.3.7)
Driver: Hive JDBC (version 2.3.7)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://master:10222/default> !exit
Closing: 0: jdbc:hive2://master:10222/default


pom

<dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.3.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.jetty.aggregate</groupId>
                    <artifactId>jetty-all</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.hive</groupId>
                    <artifactId>hive-shims</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.10.0</version>
        </dependency>

demo

package per.spring.demo.util;

//hivedriverClassName=org.apache.hive.jdbc.HiveDriver
//        hiveurl=jdbc:hive2://192.168.31.243:10222/default
//        hiveusername=root
//        hivepassword=

public class testHive {

    public static void main(String[] args) throws SQLException {
     hiveJDBC_RowCount("select * from ads.ads_bill_receivable limit 20");
    }
    public static void hiveJDBC_RowCount(String sql) throws SQLException {

        Connection conn = DriverManager.getConnection("jdbc:hive2://xx.xx.xx.xx:10222/default", "username", "passd");

        System.out.println("---- Begin executing sql: " + sql + " ----");
        java.sql.PreparedStatement statement = conn.prepareStatement(sql);
        ResultSet result = statement.executeQuery();
        ResultSetMetaData resultMetaData = result.getMetaData();
        Integer colNum = resultMetaData.getColumnCount();
        for (int j = 1; j <= colNum; j++) {
            System.out.println(resultMetaData.getColumnLabel(j) + "\t");
        }
        System.out.println();

        while (result.next()) {
            for (int j = 1; j <= colNum; j++) {
                System.out.println(result.getString(j) + "\t");
            }
            System.out.println();
        }
        System.out.println("---- Done executing sql: " + sql + " ----");

    }

}

博主公众号
求关注
在这里插入图片描述
s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值