SpringBoot工程通过jdbc连接Hive
首先确定当前hive的版本
例如:我目前的版本就是
hive-common-1.1.0-cdh5.16.2.jar
SpringBoot工程通过jdbc连接Hive
注意:引入的Hive-jdbc依赖需与Hive版本保持一致。
依次对应hive-common-1.1.0引入的Hive-jdbc版本为1.1.0(其中排除log4j依赖是为了解决原工程引入Hive依赖后导致日志不输出问题)
org.apache.hive
hive-jdbc
org.eclipse.jetty.aggregate
*
org.apache.logging.log4j
log4j-slf4j-impl
org.slf4j
slf4j-log4j12
org.apache.log4j
org.apache.hive.org.apache.log4j
log4j
log4j
关引入Hive-jdbc1.1.0再去尝试连接同样会发生报错
报错信息如下
java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
at org.apache.hive.jdbc.HiveConnection.createBinaryTransport(HiveConnection.java:393)
at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:187)
at org.apache.hive.jdbc.HiveConnection.(HiveConnection.java:163)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.hundsun.duqa.admin.api.service.DuqaHiveJDBCTest.test2(DuqaHiveJDBCTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 35 more
产生错误的原因是我们需要同样引入Hadoop-common包,这里我选择的是2.6.5
org.apache.hadoop
hadoop-common
2.6.5
servlet-api
javax.servlet
org.apache.logging.log4j
log4j-slf4j-impl
org.slf4j
slf4j-log4j12
org.apache.log4j
org.apache.hive.org.apache.log4j
log4j
log4j
再去测试我们的Demo,成功了!
Demo代码如下:
public void test2() throws Exception{
// 注册数据库驱动,用的hive的jdbc,驱动名固定写死
Class.forName("org.apache.hive.jdbc.HiveDriver");
// 如果用的是hive2服务,则写jdbc:hive2,后面跟上hive服务器的ip以及端口号,端口号默认是10000
Connection conn = DriverManager.getConnection("jdbc:hive2://1服务器IP:10000/库名", "用户名", "密码");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select api_id from idc.ods_duas_data_services_mng where thedate ='20220424' and api_id regexp 'companyInfo'");
while (rs.next()) {
String api_id = rs.getString("api_id");
System.out.println(api_id);
}
stat.close();
conn.close();
}
完美解决SpringBoot通过jdbc连接Hive问题!
如果本文对你有帮助,别忘记给我个3连 ,点赞,转发,评论,,咱们下期见。
【SpringBoot工程通过jdbc连接Hive】收藏 等于白嫖,点赞才是真情。
推荐阅读
- REST|REST APIs
- 其他AutoCAD尺寸命令
- 硬件工程师|单片机是不是嵌入式呢,老生常谈了
- springBoot中application.properties的常用配置
- 以CSDN app 为例 软件工程 案例分析
- 哈鱼矿工程序APP开发
- AutoCAD显示(界面结构详细图解)
- AutoCAD中的弧用法图解
- 科股早知道|钛媒体科股早知道:水利设施在建重大工程规模超万亿,一季度投资同比增35%;新船需求量旺盛,行业龙头新增订单接近翻倍
- Android资深工程师进阶之路