JDBC - Hive 연결
먼저 의존성
당시 사용하던건 클라우데라꺼 였는데 의존성은 클라우데라의 의존성을 쓰지 않아도 driver를 잘 찾았다.
(클라우데라의 jar파일을 수동으로 넣었는데 driver를 못찾음;; 왜지??)
버전은 각 환경에 맞는 버전으로 설정
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.7</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libfb303</artifactId>
<version>0.9.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
다음은 Java 코드 (driver는 hive 외 다른 db도 바꿔주면 연결 가능)
// hive query문 날려서 map형태로 return
public List<Map<String, Object>> hivequery(String sql) throws Exception {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String driverName = "org.apache.hive.jdbc.HiveDriver";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = null;
Statement stmt = null;
ResultSet res = null;
try {
con = DriverManager.getConnection("jdbc:hive2://bdmn01.kcg.go.kr:10000/;auth=noSasl", "hive", null);
stmt = con.createStatement();
res = stmt.executeQuery(sql);
while (res.next()) {
Map<String, Object> map = new HashMap<String, Object>();
ResultSetMetaData rmd = res.getMetaData();
for (int i = 1; i <= rmd.getColumnCount(); i++) {
map.put(rmd.getColumnName(i), res.getString(rmd.getColumnName(i)));
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
Leave a comment