java操作hive

版本

hadoop 2.7.4
hive 1.2.2

启动hive远程服务

hiveserver2 默认开启10000端口

1
2
3
4
5
hiveserver2
hive --service hiveserver2
# 验证是否启动成功
netstat -ntulp |grep 10000

依赖的jar包

java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver

缺少hive-jdbc依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>

java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration

原因:缺少hadoop-common依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>

jdbc工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package siys16877.HiveDemo.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String driver = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://127.0.0.1:10000/default";
//注册驱动
static{
try {
Class.forName(driver);
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
throw new ExceptionInInitializerError(e);
}
}
//获取连接
public static Connection getConnection(){
try {
System.out.println("获取连接成功");
return DriverManager.getConnection(url);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("获取连接失败");
e.printStackTrace();
}
return null;
}
//释放资源
public static void release(Connection conn,Statement st,ResultSet rs){
if(rs != null){
try {
rs.close();
System.out.println("release rs done !");
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
if(st != null){
try {
st.close();
System.out.println("release st done ! ");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
st = null;
}
}
if(conn != null){
try {
conn.close();
System.out.println("release conn done !");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn = null;
}
}
}
}

hive-jdbc-demo测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package siys16877.HiveDemo.hive;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import siys16877.HiveDemo.utils.JDBCUtils;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException {
try {
String sql = "select * from test1";
Connection con = JDBCUtils.getConnection();
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
String name = rs.getString(2);
System.out.println(name);
}
JDBCUtils.release(con, statement, rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}