借助 Apache Phoenix,我們可以使用標准 SQL 和 JDBC 接口來操作 HBase。前文演示了 Spring Boot 項目使用 JdbcTemplate 來操作 HBase 數據庫,本文接着演示使用 MyBatis 來操作 HBase 數據庫。
三、使用 MyBatis 操作 HBase
(1)執行下面命令創建一個名為 student 的表:
注意:在 phoenix 中,默認情況下,庫名,表名,字段名等會自動轉換為大寫,若要小寫,使用雙引號,如"student"。
CREATE TABLE IF NOT EXISTS "student"( id VARCHAR primary key, name VARCHAR, age VARCHAR);
2,項目配置
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 去掉springboot默認日志配置 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入log4j2依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 引入lombok依賴 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </dependency> <!-- MyBatis依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- phoenix相關依賴配置 --> <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> <version>5.0.0-HBase-2.0</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>2.9.2</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> </dependencies>
(2)接着在 application.properties 中配置數據庫連接信息:
spring.datasource.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver
spring.datasource.url=jdbc:phoenix:81.68.xx.xx:2181
yml版本
spring:
datasource:
driverClassName:org.apache.phoenix.jdbc.PhoenixDriver
url:jdbc:phoenix:81.68.xx.xx:2181
3,編寫代碼
@Setter @Getter @NoArgsConstructor @AllArgsConstructor @ToString public class Student { private String id; private String name; private String age; }
(3)接着創建 StudentMapper 接口:
要指明一個類是 Mapper 有如下兩種方式:
一種如本樣例所示,在 StudentMapper 上添加 @Mapper 注解,表明該接口是一個 MyBatis 中的 Mapper。這種方式就是需要在每一個 Mapper 上都添加注解。
另一種更簡單的方式是在配置類上添加 @MapperScan("com.example.demo.mapper") 注解,表示掃描 com.example.demo.mapper 包下的所有接口作為 Mapper。這樣就不需要在每個接口上配置 @Mapper 注解了。
@Mapper public interface StudentMapper { int upsertStudent(Student student); int deleteStudentById(String id); Student getStudentById(String id); List<Student> getAllStudents(); }
(4)接着在 StudentMapper 相同的位置創建 StudentMapper.xml 文件,內容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.phoenixdemo.mapper.StudentMapper"> <update id="upsertStudent" parameterType="com.example.phoenixdemo.model.Student"> UPSERT INTO "student" VALUES (#{id}, #{name}, #{age}) </update> <delete id="deleteStudentById" parameterType="String"> DELETE FROM "student" WHERE id=#{id} </delete> <select id="getStudentById" parameterType="String" resultType="com.example.phoenixdemo.model.Student"> SELECT * FROM "student" WHERE id=#{id} </select> <select id="getAllStudents" resultType="com.example.phoenixdemo.model.Student"> SELECT * FROM "student" </select> </mapper>
(5)由於在 Maven 工程中,XML 配置文件建議寫在 resources 目錄下,但上面的 StudentMapper.xml 文件寫在包下,Maven 在運行時會忽略包下的 XML 文件。因此需要在 pom.xml 文件中重新指明資源文件位置,配置如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!-- 重新指明資源文件位置 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
(6)最后我們在 Controller 中通過調用 StudentMapper 進行數據的增、刪、改、查操作。
@RestController public class HelloController { @Autowired StudentMapper studentMapper; @GetMapping("/test") public void test() { // 插入數據 System.out.println("\n--- 開始插入數據 ---"); studentMapper.upsertStudent(new Student("1001","大劉","20")); studentMapper.upsertStudent(new Student("1002","小星","22")); studentMapper.upsertStudent(new Student("1003","hangge","100")); // 刪除數據 System.out.println("\n--- 開始刪除數據 ---"); studentMapper.deleteStudentById("1002"); // 查詢數據 System.out.println("\n--- 查詢單條數據 ---"); Student student = studentMapper.getStudentById("1001"); System.out.println(student); System.out.println("\n--- 查詢多條數據 ---"); List<Student> list = studentMapper.getAllStudents(); System.out.println(list); } }
4,運行測試
在瀏覽器中訪問 http://localhost:8080/test 地址,可以看到控制台打印出的日志如下: