前言
Phoenix 最早是 saleforce 的一个开源项目,后来成为 Apache 的顶级项目。
Phoenix 构建在 HBase 之上的开源 SQL 层。能够让我们使用标准的 JDBC API 去建表,插入数据和查询 HBase 中的数据,从而可以避免使用 HBase 的客户端 API。
Apache Phoenix 通过结合两个方面的优点,在Hadoop中为低延迟应用提供了OLTP和运营分析:
- 具有完全ACID事务功能的标准SQL和JDBC api的强大功能
- 通过利用HBase作为后台存储,从NoSQL获得后期绑定的、读时模式功能的灵活性
- Phoenix通过协处理器在服务器端执行操作,最小化客户机/服务器数据传输
Apache Phoenix与Spark、Hive、Pig、Flume、Map Reduce等Hadoop产品完全集成。
通过定义良好的行业标准api,成为OLTP和Hadoop操作分析的可信数据平台。
在我们的应用和 HBase 之间添加了 Phoenix,并不会降低性能,而且我们也少写了很多代码。
phoenix 特点
- 将 SQL 查询编译为 HBase 扫描
- 完美支持 HBase 二级索引创建
- 支持完整的ACID事务、UDF、分页查询
- 确定扫描 Rowkey 的最佳开始和结束位置、扫描并行执行
- 将 where 子句推送到服务器端的过滤器
- 通过协处理器进行聚合操作
- DML 命令以及通过 DDL 命令创建和操作表和版本化增量更改。
- 容易集成:如Spark,Hive,Pig,Flume 和 Map Reduce。
- 支持java、python的Driver
一、Springboot集成mybatis+phoenix操作Hbase
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- hbaseClient依赖 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- phoenix依赖 -->
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>5.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置application.yml
spring:
datasource:
driverClassName: org.apache.phoenix.jdbc.PhoenixDriver
# zookeeper 地址
url: jdbc:phoenix:node1,node2,node3:2181
# 如果不想配置对数据库连接池做特殊配置的话,以下关于连接池的配置就不是必须的
# spring-boot 2.X 默认采用高性能的 Hikari 作为连接池 更多配置可以参考 https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby
type: com.zaxxer.hikari.HikariDataSource
hikari:
# 池中维护的最小空闲连接数
minimum-idle: 10
# 池中最大连接数,包括闲置和使用中的连接
maximum-pool-size: 20
# 此属性控制从池返回的连接的默认自动提交行为。默认为 true
auto-commit: true
# 允许最长空闲时间
idle-timeout: 30000
# 此属性表示连接池的用户定义名称,主要显示在日志记录和 JMX 管理控制台中,以标识池和池配置。 默认值:自动生成
pool-name: custom-hikari
#此属性控制池中连接的最长生命周期,值 0 表示无限生命周期,默认 1800000 即 30 分钟
max-lifetime: 1800000
# 数据库连接超时时间,默认 30 秒,即 30000
connection-timeout: 30000
# 连接测试 sql 这个地方需要根据数据库方言差异而配置 例如 oracle 就应该写成 select 1 from dual
connection-test-query: SELECT 1
mybatis:
mapper-locations: classpath:mappers/*.xml
hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<configuration>
<property>
<name>hbase.regionserver.wal.codec</name>
<value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
</property>
<!-- 注意:为了开启hbase的namespace和phoenix的schema的映射,在程序中需要加这个配置文件,另外在linux服务上,也需要在hbase以及phoenix的hbase-site.xml配置文件中,加上以上两个配置,并使用xsync进行同步-->
<property>
<name>phoenix.schema.isNamespaceMappingEnabled</name>
<value>true</value>
</property>
<property>
<name>phoenix.schema.mapSystemTablesToNamespace</name>
<value>true</value>
</property>
</configuration>
StudentMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hbase.phoenix.mapper.StudentMapper">
<select id="queryAll" resultType="com.hbase.phoenix.pojo.Student">
SELECT * FROM "school"."stu"
</select>
<insert id="save">
UPSERT INTO "school"."stu" VALUES( #{id}, #{name}, #{age} )
</insert>
<select id="queryById" resultType="com.hbase.phoenix.pojo.Student">
SELECT * FROM "school"."stu" WHERE id=#{id}
</select>
<delete id="deleteById">
DELETE FROM "school"."stu" WHERE id=#{id}
</delete>
</mapper>
PhoeinxApplication
mport org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hbase.phoenix.mapper")
public class PhoeinxApplication {
public static void main(String[] args) {
SpringApplication.run(PhoeinxApplication.class);
}
}
Student
package com.hbase.phoenix.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String id;
private String name;
private String age;
}
StudentMapper
import com.hbase.phoenix.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper {
List<Student> queryAll();
void save(Student student);
Student queryById(String id);
void deleteById(String id);
}
PhoenixTest
import com.hbase.phoenix.mapper.StudentMapper;
import com.hbase.phoenix.pojo.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PhoenixTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void nihao(){
System.out.println("fjaidsfj");
}
@Test
public void queryAll() {
List<Student> StudentList = studentMapper.queryAll();
if (StudentList != null) {
for (Student student : StudentList) {
System.out.println(student);
}
}
}
@Test
public void save() {
studentMapper.save(new Student("1003", "Dallas", "6666"));
Student Student = studentMapper.queryById("1003");
System.out.println(Student);
}
@Test
public void update() {
studentMapper.save(new Student("1001", "小五", "99999"));
Student Student = studentMapper.queryById("1001");
System.out.println(Student);
}
@Test
public void delete() {
studentMapper.deleteById("1003");
Student Student = studentMapper.queryById("1001");
System.out.println(Student);
}
}
二、安装使用DBeaver操作HBase
DBeaver下载:https://dbeaver.io/download/
- 1、下载DBeaver安装包dbeaver-ce-7.1.3-win32.win32.x86_64.zip,解压后进入目录,在配置文件dbeaver.ini中增加以下配置指定jdk
-vm
C:\Program Files\jdk1.8.0_66\bin
- 2、运行dbeaver.exe,选择创建新连接,选中“Hadoop/BigData”栏的“Apache Phoenix”点击下一步,编辑驱动设置中添加驱动文件phoenix-5.0.0-HBase-2.0-client.jar,点选“找到类”即可搜索到对应的驱动类,如图所示;
参考:
https://blog.csdn.net/S1124654/article/details/125586358
https://www.jianshu.com/p/18e09c184865
https://blog.csdn.net/dndndnnffj/article/details/121801897
网友评论