SpringBoot 使用Phoenix操作HBase数据库教程
文章目录
借助 Apache Phoenix,可以让我们能够使用标准 SQL 和 JDBC 接口来操作 HBase。使用标准JDBC连接。
前提服务器安装 HBase,而且还安装了Phoenix,注意版本要一直
开发前准备
打开本地 C:\Windows\System32\drivers\etc下的名为hosts的系统文件。在文件底部插入集群名映射到ip
----服务器IP 集群名称
127.0.0.1 node001
添加 Phoenix 相关依赖
根据自己的版本下载对应依赖
<!--hbase数据库配置-->
<!-- phoenix相关依赖配置 -->
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-client-hbase-2.4.0</artifactId>
<version>5.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<!--HBase依赖 -->
控制器实现Hbase数据库操作
1,表名为什么加双引号?
在 phoenix 中,默认情况下,库名,表名,字段名等会自动转换为大写,若要小写,使用双引号,如"student"。
2,是否需要 Phoenix JDBC 连接池?
是不需要缓存 Phoenix JDBC 连接池。由于 HBase 的特殊性,Phoenix 连接对象有别于其他常规的 JDBC 连接。Phoenix 连接被设计为 thin 对象,创建它的代价很小。如果使用连接池来重用 HBase 连接,前一个用户的非正常退出可能会导致连接处于错误状态。因此最好每次创建一个新的连接。
如果实在需要使用连接池,可以对 Phoenix 连接做简单的代理,每次需要从池中获取连接的时候初始化一个就好,而将连接归还到连接池之后就把它关闭掉。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.*;
import java.util.*;
@RestController
public class TestController {
//phoenix驱动
private String phoenixDriver = "org.apache.phoenix.jdbc.PhoenixDriver";
//zookeeper地址
private String phoenixURL = "jdbc:phoenix:数据库IP:端口";
@GetMapping("/hbasedata")
public void hbasedata() throws Exception {
// 创建表
System.out.println("\n--- 开始创建 tabletest 表 ---");
createTable();
// 获取Phoenix中的表(系统表除外)
System.out.println("\n--- 获取Phoenix中的表(系统表除外) ---");
List<String> tables = getTables();
System.out.println(tables);
// 插入数据
System.out.println("\n--- 开始插入数据 ---");
insertData();
// 删除数据
System.out.println("\n--- 开始删除数据 ---");
deleteData();
// 查询数据
System.out.println("\n--- 开始查询数据 ---");
List<Map<String,String>> list = getData("hbasedata");
System.out.println(list);
//删除表
System.out.println("\n--- 开始删除 tabletest 表 ---");
dropTable();
}
// 获取连接
public Connection getConnection() throws Exception {
Class.forName(phoenixDriver);
Properties pros=new Properties();
pros.setProperty("phoenix.schema.isNamespaceMappingEnabled","true");
return DriverManager.getConnection(phoenixURL,pros);
}
// 创建表
public void createTable() throws Exception {
//获取连接
Connection connection = getConnection();
// 创建Statement对象
String sql = "CREATE TABLE IF NOT EXISTS \"tabletest\"(" +
"id VARCHAR primary key," +
"name VARCHAR," +
"age VARCHAR)";
PreparedStatement statement = connection.prepareStatement(sql);
// 执行sql操作
statement.execute();
// 关闭
statement.close();
connection.close();
}
// 获取Phoenix中的表(系统表除外)
public List<String> getTables() throws Exception {
//获取连接
Connection connection = getConnection();
List<String> tables = new ArrayList<>();
DatabaseMetaData metaData = connection.getMetaData();
String[] types = {"TABLE"}; //"SYSTEM TABLE"
ResultSet resultSet = metaData.getTables(null,null,types);
while (resultSet.next()) {
tables.add(resultSet.getString("TABLE_NAME"));
}
return tables;
}
// 删除表
public void dropTable() throws Exception {
//获取连接
Connection connection = getConnection();
// 创建Statement对象
String sql = "DROP TABLE \"tabletest\"";
PreparedStatement statement = connection.prepareStatement(sql);
// 执行sql操作
statement.execute();
// 关闭
statement.close();
connection.close();
}
// 插入数据
public void insertData() throws Exception {
//获取连接
Connection connection = getConnection();
//获取Statement对象,并进行数据插入
Statement statement = connection.createStatement();
statement.executeUpdate("upsert into \"tabletest\" values('1001','大刘','20')");
statement.executeUpdate("upsert into \"tabletest\" values('1002','小星','22')");
connection.commit();
statement.close();
//获取PreparedStatement对象,并进行数据插入
PreparedStatement preparedStatement = connection.prepareStatement(
"upsert into \"tabletest\" values(?,?,?)");
//给参数赋值
preparedStatement.setString(1,"1003");
preparedStatement.setString(2,"hangge");
preparedStatement.setString(3,"1000");
//执行插入
preparedStatement.execute();
connection.commit();
preparedStatement.close();
connection.close();
}
// 删除数据
public void deleteData() throws Exception {
//获取连接
Connection connection = getConnection();
//获取Statement对象,并进行数据删除
Statement statement = connection.createStatement();
statement.execute("delete from \"tabletest\" where id = '1002'");
connection.commit();
statement.close();
connection.close();
}
// 查询数据(获取表中的所有数据)
public List<Map<String,String>> getData(String tableName) throws Exception {
//获取连接
Connection connection = getConnection();
String sql = "SELECT * FROM " + tableName;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
List<Map<String,String>> resultList = new ArrayList<>();
while (resultSet.next()) {
Map<String,String> result = new HashMap<>();
for (int i = 1,len = resultSetMetaData.getColumnCount(); i <= len; i++) {
result.put(resultSetMetaData.getColumnName(i),resultSet.getString(i));
}
resultList.add(result);
}
return resultList;
}
}