您好,登錄后才能下訂單哦!
這篇文章給大家介紹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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。