关于hbase:HbasePhoenixMybatisSpringboot整合查询数据

44次阅读

共计 5775 个字符,预计需要花费 15 分钟才能阅读完成。

Phoenix Query Server 提供了一种与 Phoenix 和 HBase 交互的代替办法。很快,这将容许从 JVM 以外的环境进行拜访。

  • 在 4.x 和 5.0 版本中,查问服务器及其 JDBC 客户端是规范 Phoenix 发行版的一部分。它们不须要其余依赖项。
  • 在 5.0 版本之后,查问服务器已被捆绑到 phoenix-queryserver 存储库中,并且其版本号已重置为 1.0。在撰写本文时,没有独立查问服务器的发行版本。

因为咱们装置的是 apache-phoenix-5.0.0-HBase-2.0, 所以外面内置了 queryserver

1. 启动 queryserver, 默认监听 8765

> apache-phoenix-5.0.0-HBase-2.0-bin/bin/queryserver.py

2. 自定义配置 vim hbase-site.xml

属性 形容 默认
phoenix.queryserver.http.port 指定服务器将侦听的端口。默认值为 8765。 8765
phoenix.queryserver.metafactory.class 要实例化的 Avatica Meta.Factory 类。 org.apache.phoenix.queryserver.server.PhoenixMetaFactoryImpl
phoenix.queryserver.serialization 传输 / 序列化格局,PROTOBUF 或 JSON。 PROTOBUF
// 将默认的 8765 端口改成 8888
<property>
  <name>phoenix\.queryserver\.http\.port</name>
  <value>8888</value>
</property>

3.pom.xml 中引入 Phoenix 客户端

// 轻量级客户端
<dependency>
  <groupId>org.apache.phoenix</groupId>
  <artifactId>phoenix-queryserver-client</artifactId>
  <version>5.0.0-HBase-2.0</version>
</dependency>

4. 残缺的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rumenz</groupId>
    <artifactId>phoenix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>phoenix</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-queryserver-client</artifactId>
            <version>5.0.0-HBase-2.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <configuration>
                    <mainClass>com.rumenz.phoenix.PhoenixApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

5. 数据源配置

package com.rumenz.phoenix;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.phoenix.queryserver.client.Driver;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.rumenz.phoenix.mapper")
public class PhoenixForMybatisConfiguration {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource phoenixDataSource() {DruidDataSource druidDataSource = new DruidDataSource();
        // 设置 Phoenix 驱动
        druidDataSource.setDriverClassName(Driver.class.getName());
        druidDataSource.setUrl(dataSourceProperties.getUrl());
        return druidDataSource;
    }
}

6. 数据库操作类 UserMapper

留神: 因为我的 HBase 数据库表名和字段名都是小写所以我这里表名和字段名必须加上双引号, 否则表名和字段名会变成大写,找不到对应关系,会报错。

如果 HBase 数据库中表名和字段名都是大写,那么则不须要双引号

package com.rumenz.phoenix.mapper;

import com.rumenz.phoenix.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    // 表名和字段名必须加上双引号, 否则表名和字段名会变成大写,找不到对应关系,会报错
    @Select("select * from \"test2\"where \"id\"=#{id}")
    User selectById(Integer id);
}

7. 实体类

package com.rumenz.phoenix.entity;

public class User {
     private Integer id;
     private String  name;
     private Integer age;

    public Integer getId() {return id;}

    public void setId(Integer id) {this.id = id;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public Integer getAge() {return age;}

    public void setAge(Integer age) {this.age = age;}

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

8. 访问控制 UserController

package com.rumenz.phoenix.controller;


import com.rumenz.phoenix.entity.User;
import com.rumenz.phoenix.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("index")
    public String index(){User user = userMapper.selectById(1);
        return user.toString();}
}

9. 拜访 http://127.0.0.1:8080/user/index

User{id=1, name='rumenz', age=30}

源码工程:https://github.com/mifunc/Spr…

正文完
 0