-
Notifications
You must be signed in to change notification settings - Fork 4
/
HiveOperateTest.java
213 lines (197 loc) · 6.04 KB
/
HiveOperateTest.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.hdp2.project.hive;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author liuyzh
* @description: 基于Kerberos环境,使用Java远程执行Hive操作
* date 2019/8/7 16:22
*/
public class HiveOperateTest {
private static String url = "jdbc:hive2://node72.xdata:10000/default;principal=hive/node72.xdata@XDATA.COM";
private static String url2 = "jdbc:hive2://node71.xdata:2181,node72.xdata:2181,node73.xdata:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2;principal=hive/node72.xdata@XDATA.COM";
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
/**
* @description: 通过jdbc连接hive2
*/
@Test
@Before
public void getConnection() {
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
System.setProperty("krb5_ini", System.getProperty("user.dir") + "\\krb5\\krb5.ini");
System.setProperty("hive_keytab", System.getProperty("user.dir") + "\\krb5\\liuyzh.service.keytab");
System.setProperty("java.security.krb5.conf", System.getProperty("krb5_ini"));
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab("liuyzh/node71.xdata@XDATA.COM", System.getProperty("hive_keytab"));
} catch (IOException e) {
e.printStackTrace();
}
try {
// 使用hive用户登陆
conn = DriverManager.getConnection(url2, "", "");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 创建数据库
*/
@Test
public void cresteDatabase(){
try {
conn.prepareStatement("CREATE database test").execute();
System.out.println("数据库创建成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 查询数据
*/
@Test
public void getAll() {
String sql = "select * from mytable";
System.out.println(sql);
try {
ps = conn.prepareStatement(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) + "\t");
}
System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 执行DDL语句
*/
@Test
public void execute() {
String sql = "INSERT overwrite directory '/user/hue/learn_oozie/mazy_hive_1/output'\n" +
"row format delimited fields terminated by \"\\t\"\n" +
"SELECT sid,sname FROM mytable LIMIT 10";
System.out.println(sql);
try {
ps = conn.prepareStatement(sql);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 创建表格
*/
@Test
public void createTables() {
String createSql = "create table if not exists default.mytable(id String, name String) row format delimited fields terminated by \",\" stored as textfile";
try {
conn.prepareStatement(createSql).execute();
System.out.println("创建表成功");
} catch (SQLException e) {
e.printStackTrace();
System.err.println("创建表失败");
}
}
/**
* @description: 进入数据库,展示所有表
*/
@Test
public void showTables() {
try {
// 进入default数据库
ps = conn.prepareStatement("use default");
ps.execute();
// 展示所有表
rs = ps.executeQuery("show tables");
// 处理结果集
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 加载数据
*/
@Test
public void loadTable() {
String loadSql = "load data inpath \"/user/liuyzh/mytable.txt\" into table mytable";
try {
conn.prepareStatement(loadSql).execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 删除数据表
*/
@Test
public void deleteTable(){
try {
conn.prepareStatement("DROP table default.mytable").execute();
System.out.println("数据表被删除");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 删除数据库
*/
@Test
public void dropDatabase(){
String sql = "drop database test";
try {
conn.prepareStatement(sql).execute();
System.out.println("删除数据库成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* @description: 关闭连接
*/
@Test
@After
public void closeConnect() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}