使用Java通过JDBC连接 Phoenix


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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* The class test.
* <p>
* Description:
*
* @author: bianhaonan
* @since: 2017/07/20
* @version:
*/
public class PhoenixJDBCTest {
@Test
public void PhoenixJDBCTest(){
System.out.println(getConnection());
}

@Test
public void create(){
Connection conn = null;
try {
// 获得链接
conn = getConnection();

// 检查链接是否为空
if (conn == null) {
System.out.println("conn is null...");
return;
}

// 检查表是否存在
ResultSet rs = conn.getMetaData().getTables(null, null, "USER",null);
if (rs.next()) {
System.out.println("table user is exist...");
return;
}
// 定义创建表sql
String sql = "CREATE TABLE user (id varchar PRIMARY KEY,INFO.account varchar ,INFO.passwd varchar)";

PreparedStatement ps = conn.prepareStatement(sql);

// 创建表
ps.execute();
System.out.println("create success...");

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@Test
public void add(){
Connection conn = null;
try {
// 获得链接
conn = getConnection();

// 检查链接是否为空
if (conn == null) {
System.out.println("conn is null...");
return;
}

// 插入数据sql
String sql = "upsert into user(id, INFO.account, INFO.passwd) values('001', 'admin', 'admin')";

PreparedStatement ps = conn.prepareStatement(sql);

// 插入数据
String msg = ps.executeUpdate() > 0 ? "insert success..."
: "insert fail...";

// 手动提交 *必须项,不然数据插入不成功*
conn.commit();
System.out.println(msg);

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Test
public void select(){
Connection conn = null;
try {
// 获得链接
conn = getConnection();

// 检查链接是否为空
if (conn == null) {
System.out.println("conn is null...");
return;
}

// 查询sql
String sql = "select * from user";

PreparedStatement ps = conn.prepareStatement(sql);

// 查询数据
ResultSet rs = ps.executeQuery();

System.out.println("id" + "\t" + "account" + "\t" + "passwd");
System.out.println("======================");

if (rs != null) {
while (rs.next()) {
System.out.print(rs.getString("id") + "\t");
System.out.print(rs.getString("account") + "\t");
System.out.println(rs.getString("passwd"));
}
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@Test
public void deleteData(){
Connection conn = null;
try {
// 获得链接
conn = getConnection();

// 检查链接是否为空
if (conn == null) {
System.out.println("conn is null...");
return;
}

// 创建sql
String sql = "delete from user where id='001'";

PreparedStatement ps = conn.prepareStatement(sql);

// 执行sql
String msg = ps.executeUpdate() > 0 ? "delete success..."
: "delete fail...";

// 手动提交 *必须项,不然数据删除不成功*
conn.commit();
System.out.println(msg);

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@Test
public void deleteTable(){
Connection conn = null;
try {
// get connection
conn = getConnection();

// check connection
if (conn == null) {
System.out.println("conn is null...");
return;
}

// create sql
String sql = "drop table user2";

PreparedStatement ps = conn.prepareStatement(sql);

// execute
ps.execute();

System.out.println("drop success...");

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public Connection getConnection(){
try {
// load driver
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
// jdbc 的 url 类似为 jdbc:phoenix [ :<zookeeper quorum> [ :<port number> ] [ :<root node> ] ],
// 需要引用三个参数:hbase.zookeeper.quorum、hbase.zookeeper.property.clientPort、and zookeeper.znode.parent,
// 这些参数可以缺省不填而在 hbase-site.xml 中定义。
return DriverManager.getConnection("jdbc:phoenix:slave1,slave2,slave3:2181");
//DriverManager.getConnection("jdbc:phoenix:host1,host2:2181");

} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

文章作者: hnbian
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 hnbian !
评论
  目录