因为项目需求,数据库选择使用hbase,考虑到之后有索引等可能的需求,最终选定用phoenix调用hbase作为数据库使用。
添加依赖
这部分功能涉及到的依赖如下:
dependencies{
//phoenix
implementation(group: 'org.apache.phoenix', name: 'phoenix-core', version: "5.0.0-HBase-2.0")
}
编写工具类
class PhoenixHelper {//Phoenix工具类
private var connectionPool: Queue<Connection> = ConcurrentLinkedQueue()//简略的连接池
private fun getConnection(): Connection {//获取Connection
if (connectionPool.size > 0) {
return connectionPool.poll()
} else {
val connection = DriverManager.getConnection("jdbc:phoenix")
return connection
}
}
private fun releaseConnection(connection: Connection) {//Connection扔回池
connectionPool.add(connection)
}
fun executeUpdate(sqlString: String): Int {//不带变量的数据更新
val connection = getConnection()
v