通過JDBC訪問phoenix
阿新 • • 發佈:2019-01-27
應用場景
既然hbase支援sql語句對資料進行操作,那麼我們能不能通過程式碼連線Hbase,通過phoenix,在程式碼中書寫sql語句,對phoenix進行操作呢?
操作方案
1. 新建maven專案
為什麼要建maven專案,因為引用的包實在太多了!
2. 程式碼
package com.epoint.test.phoenix;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql .Statement;
public class Phoenix {
private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Statement stmt = null;
ResultSet rs = null;
Connection con = DriverManager.getConnection("jdbc:phoenix:hadoop0,hadoop1,hadoop2:2181");
stmt = con.createStatement();
String sql = "select * from test";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.print("id:" +rs.getString("id"));
System.out.println(",name:"+rs.getString("name"));
}
stmt.close();
con.close();
}
}
3. pom中加入如下dependency
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.10.0-HBase-1.2</version>
</dependency>
maven下載的依賴包如下所示: