应用场景
既然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下载的依赖包如下所示: