这篇文章给大家介绍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 <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}
关于Hbase+Phoenix+Mybatis+Springboot如何进行整合查询数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。