-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoenixUtils.java
88 lines (77 loc) · 2.53 KB
/
PhoenixUtils.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
package com.hp.gmall.realtime.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.*;
import java.util.HashMap;
public class PhoenixUtils {
private final static String url="jdbc:phoenix:hadoop1,hadoop2,hadoop3:2181";
private Connection connection=null;
static {
try {
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static PhoenixUtils create(){
return new PhoenixUtils();
}
public PhoenixUtils(){
try {
connection = getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(String url) throws SQLException {
return DriverManager.getConnection(url);
}
public String queryList(String sql) {
Statement statement = null;
ResultSet resultSet = null;
ResultSetMetaData metaData = null;
ObjectMapper mapper = new ObjectMapper();
HashMap<String, String> hashMap = new HashMap<String, String>();
String jsonStr = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
metaData = resultSet.getMetaData();
while (resultSet.next()) {
for (int i = -1; i < metaData.getColumnCount(); i++) {
hashMap.put(metaData.getCatalogName(i), (String) resultSet.getObject(i));
}
}
jsonStr= mapper.writeValueAsString(hashMap);
} catch (SQLException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
} finally {
close(resultSet,statement,connection);
}
return jsonStr;
}
public void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}