HBase + Phoenix + mybatis + springboot integrate query data

Time:2021-7-6

Phoenix query server provides an alternative way to interact with Phoenix and HBase. Soon, this will allow access from an environment other than the JVM.

  • In versions 4. X and 5.0, the query server and its JDBC client are part of the standard Phoenix distribution. They do not require other dependencies.
  • After version 5.0, the query server has been bundled into the Phoenix query server repository, and its version number has been reset to 1.0. At the time of writing, there was no independent query server release.

Since we are installing apache-phoenix-5.0.0-hbase-2.0, query server is built in

1. Start queryserver and listen to 8765 by default

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

2. User defined configuration VIM hbase-site.xml

attribute describe default
phoenix.queryserver.http.port Specifies the port on which the server will listen. The default value is 8765. 8765
phoenix.queryserver.metafactory.class Avatica meta.factory class to instantiate. org.apache.phoenix.queryserver.server.PhoenixMetaFactoryImpl
phoenix.queryserver.serialization Transport / serialization format, protobuf or JSON. PROTOBUF
//Change the default port 8765 to 8888
<property>
  <name>phoenix\.queryserver\.http\.port</name>
  <value>8888</value>
</property>

3. Introducing Phoenix client into pom.xml

//Lightweight client
<dependency>
  <groupId>org.apache.phoenix</groupId>
  <artifactId>phoenix-queryserver-client</artifactId>
  <version>5.0.0-HBase-2.0</version>
</dependency>

4. Complete 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. Data source configuration

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();
        //Setting up Phoenix driver
        druidDataSource.setDriverClassName(Driver.class.getName());
        druidDataSource.setUrl(dataSourceProperties.getUrl());
        return druidDataSource;
    }
}

6. Database operation class usermapper

Note: since my HBase database table name and field name are both lowercase, I must add double quotation marks to the table name and field name here, otherwise the table name and field name will become uppercase, no corresponding relationship can be found, and an error will be reported.

If both table names and field names are uppercase in HBase database, double quotation marks are not required

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 {
    //Double quotation marks must be added to the table name and field name. Otherwise, the table name and field name will be capitalized. If no corresponding relationship is found, an error will be reported
    @Select("select * from \"test2\" where \"id\"=#{id}")
    User selectById(Integer id);
}

7. Entity class

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. Access control 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. Visit http://127.0.0.1 :8080/user/index

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

Source code project: https://github.com/mifunc/Spr…

HBase + Phoenix + mybatis + springboot integrate query data

Recommended Today

“Self test” stay up late to summarize 50 Vue knowledge points, all of which will make you God!!!

preface Hello everyone, I’m Lin Sanxin. A lot of things have happened these days (I won’t say what’s specific). These things have scared me to treasure my collection these yearsVue knowledge pointsI took out my notes and tried my best to recall them. Finally, I realized these 50Knowledge points(let’s not be too vulgar. It’s not […]