HIVE JDBC方法连接

编程入门 行业动态 更新时间:2024-10-11 19:18:27

输入

hiveserver2

打开hive server2

在之前的学习和实践Hive中,使用的都是CLI或者hive –e的方式,该方式仅允许使用HiveQL执行查询、更新等操作,并且该方式比较笨拙单一。幸好Hive提供了轻客户端的实现,通过HiveServer或者HiveServer2,客户端可以在不启动CLI的情况下对Hive中的数据进行操作,两者都允许远程客户端使用多种编程语言如Java、Python向Hive提交请求,取回结果。HiveServer或者HiveServer2都是基于Thrift的,但HiveSever有时被称为Thrift server,而HiveServer2却不会。既然已经存在HiveServer为什么还需要HiveServer2呢?这是因为HiveServer不能处理多于一个客户端的并发请求,这是由于HiveServer使用的Thrift接口所导致的限制,不能通过修改HiveServer的代码修正。因此在Hive-0.11.0版本中重写了HiveServer代码得到了HiveServer2,进而解决了该问题。HiveServer2支持多客户端的并发和认证,为开放API客户端如JDBC、ODBC提供了更好的支持。

输入

beeline

测试

#!connect jdbc:hive2://IP地址:10000
beeline> !connect jdbc:hive2://cm1:10000
scan plete in 4ms
Connecting to jdbc:hive2://cm1:10000
#用户名 直接回车
Enter username for jdbc:hive2://cm1:10000: 
#密码 直接回车
Enter password for jdbc:hive2://cm1:10000: 
Connected to: Apache Hive (version 1.1.0-cdh5.14.4)
Driver: Hive JDBC (version 1.1.0-cdh5.14.4)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://cm1:10000>

打开IDE
建立maven工程
pom.xml添加

<dependency> <groupId>.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>1.2.1</version> 
</dependency> <dependency> <groupId>.apache.hadoop</groupId> <artifactId>hadoop-mon</artifactId> <version>2.4.1</version> 
</dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.6</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency> 

注意这里的版本要与你的hive版本一致
如果出现:

java.sql.SQLException: Could not establish connection to jdbc:hive2://:**** Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=***})
at .apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:594)
at .apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:192)
at .apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at java.lang.Thread.run(Thread.java:745)
Caused by: .apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=***})
at .apache.thrift.TApplicationException.read(TApplicationException.java:111)
at .apache.thrift.TServiceClient.receiveBase(TServiceClient.java:71)
at .apache.hive.service.cli.thrift.TCLIService$Client.recv_OpenSession(TCLIService.java:156)
at .apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:143)
at .apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:583)
... 10 more

说明你的hive-jdbc版本不对

查看hive版本方法
ive 没有直接查询当前版本的操作, 不过我们可以通过查询 jar 包的方式间接知道当前 hive 的版本.

可知我的hive版本为1.1.0
编写程序
test:

package hive_test;import java.sql.Connection;import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class test {private static String driverName =".apache.hive.jdbc.HiveDriver";   // 此Class 位于 hive-jdbc的jar包下private static String Url="jdbc:hive2://222.22.91.81:10000/";    //填写hive的IP,之前在配置文件中配置的IPprivate static Connection conn;public static Connection getConnnection(){try{Class.forName(driverName);conn = DriverManager.getConnection(Url,"","");        //只是连接hive, 用户名可不传}catch(ClassNotFoundException e)  {e.printStackTrace();System.exit(1);}catch (SQLException e) {e.printStackTrace();}return conn;}public static PreparedStatement prepare(Connection conn, String sql) {PreparedStatement ps = null;try {ps = conn.prepareStatement(sql);} catch (SQLException e) {e.printStackTrace();}return ps;}
}

test2:

package hive_test;import java.sql.Connection;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class test2 {private static Connection conn=test.getConnnection();private static PreparedStatement ps;private static ResultSet rs;public static void getAll(String tablename){String sql="select * from "+tablename;System.out.println(sql);try {ps=test.prepare(conn, sql);rs=ps.executeQuery();int columns=rs.getMetaData().getColumnCount();while(rs.next()){for(int i=1;i<=columns;i++){System.out.print(rs.getString(i));  System.out.print("\t\t");}System.out.println();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) { String tablename="students";test2.getAll(tablename);}}

编译

clean pile package -DskipTests

选择jdk

更多推荐

方法,HIVE,JDBC

本文发布于:2023-05-28 00:44:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   HIVE   JDBC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!