千家信息网

Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,这篇文章给大家介绍Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Phoenix Query S
千家信息网最后更新 2025年12月01日Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据

这篇文章给大家介绍Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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  phoenix\.queryserver\.http\.port  8888

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

//轻量级客户端  org.apache.phoenix  phoenix-queryserver-client  5.0.0-HBase-2.0

4.完整的pom.xml

    4.0.0    com.rumenz    phoenix    0.0.1-SNAPSHOT    phoenix    Demo project for Spring Boot            1.8        UTF-8        UTF-8        2.3.0.RELEASE                            org.springframework.boot            spring-boot-starter-web                            mysql            mysql-connector-java            runtime                            org.springframework.boot            spring-boot-starter-test            test                                                org.junit.vintage                    junit-vintage-engine                                                        org.mybatis.spring.boot            mybatis-spring-boot-starter            2.1.0                            org.apache.phoenix            phoenix-queryserver-client            5.0.0-HBase-2.0                            com.alibaba            druid            1.1.21                                                    org.springframework.boot                spring-boot-dependencies                ${spring-boot.version}                pom                import                                                                org.apache.maven.plugins                maven-compiler-plugin                3.8.1                                    1.8                    1.8                    UTF-8                                                        org.springframework.boot                spring-boot-maven-plugin                2.3.0.RELEASE                                    com.rumenz.phoenix.PhoenixApplication                                                                            repackage                                                    repackage                                                                                    

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;@Mapperpublic 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}

关于Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0