Springboot集成Phoenix+Hbase+MybatisPlus

南笙酒味 提交于 2019-12-10 18:29:00

环境配置

Springboot2.2.1.RELEASE
JDK1.8
Phoenix5.0.0
Hbase2.0.0
MybatisPlus3.1.0
Squirrel SQL Client数据库连接客户端
Hbase部署在Centos7 本地虚拟机中

hbase-2.0.0-bin.tar.gz 下载地址:
https://archive.apache.org/dist/hbase/2.0.0/hbase-2.0.0-bin.tar.gz
apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz 下载地址:
http://archive.apache.org/dist/phoenix/apache-phoenix-5.0.0-HBase-2.0/bin/apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz
Squirrel 客户端工具下载地址:
http://sourceforge.net/projects/squirrel-sql/files/1-stable/4.0.0/squirrel-sql-4.0.0-standard.jar/download

相关配置文件

  1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.2.1.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.yuan</groupId>
   <artifactId>demo-druid</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo-druid</name>
   <description>Demo project for Spring Boot</description>

   <properties>
       <java.version>1.8</java.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>
       </dependency>
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid-spring-boot-starter</artifactId>
           <version>1.1.10</version>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
           <version>3.1.0</version>
       </dependency>
       <!-- mybatis plus 代码生成器依赖 -->
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-generator</artifactId>
           <version>3.1.0</version>
       </dependency>
       <!-- 代码生成器模板 -->
       <dependency>
           <groupId>org.freemarker</groupId>
           <artifactId>freemarker</artifactId>
       </dependency>

       <!--phoenix core-->
       <dependency>
           <groupId>org.apache.phoenix</groupId>
           <artifactId>phoenix-core</artifactId>
           <version>5.0.0-HBase-2.0</version>
       </dependency>

       <dependency>
           <groupId>org.apache.hadoop</groupId>
           <artifactId>hadoop-common</artifactId>
           <version>2.9.2</version>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <scope>runtime</scope>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <optional>true</optional>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>

</project>

  1. hbase-2.0.0-bin/conf: hbase-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property>
         <name>hbase.rootdir</name>
         <value>file:///usr/hbase-release/rootdir</value>
         <description>The directory shared byregion servers.</description>
     </property>

     <property>
             <name>fs.defaultFS</name>
             <value>file:///usr/hbase-release/dfs</value>
     </property>

     <!-- hbase的端口 -->
     <property>
             <name>hbase.zookeeper.property.clientPort</name>
             <value>2181</value>
             <description>Property from ZooKeeper'sconfig zoo.cfg. The port at which the clients will connect.
     </description>
     </property>

     <!--  超时时间 -->
     <property>
             <name>zookeeper.session.timeout</name>
             <value>620000</value>
     </property>

     <!--  zookeeper 集群配置。如果是集群,则添加其它的主机地址 -->
     <property>
             <name>hbase.zookeeper.quorum</name>
             <value>192.168.100.11:2181</value>
     </property>

     <property>
             <name>hbase.tmp.dir</name>
             <value>/usr/hbase-release/tmp</value>
     </property>

     <property>
             <name>hbase.cluster.distributed</name>
             <value>false</value>
     </property>

     <property>
             <name>hbase.zookeeper.property.dataDir</name>
             <value>/usr/hbase-release/datadir</value>
     </property>

     <property>
<name>zookeeper.znode.parent</name>
             <value>/hbase</value>
     </property>

	<property>
	  <name>hbase.regionserver.wal.codec</name>
	  <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value> 
	</property> 
	
	<property>
	  <name>hbase.region.server.rpc.scheduler.factory.class</name>
	  <value>org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory</value> 
	  <description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description> 
	</property> 

	<property>
	  <name>hbase.rpc.controllerfactory.class</name>
	  <value>org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory</value>
	   <description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description> 
	</property>

	<property>
	   <name>hbase.client.scanner.timeout.period</name>
	  <value>6000000</value>
	</property>
	 
	<property>
	   <name>hbase.regionserver.lease.period</name>
	  <value>6000000</value>
	</property>
	
	 <property>
	    <name>hbase.rpc.timeout</name>
	    <value>6000000</value>
	  </property>

	<property>
    		<name>hbase.client.operation.timeout</name>
    		<value>6000000</value>
  	</property>

	<property>
	   <name>hbase.client.ipc.pool.type</name>
	   <value>RoundRobinPool</value>
	</property>

	<property>
	   <name>hbase.client.ipc.pool.size</name>
	   <value>20</value>
	</property>

	<property>
	 <name>phoenix.query.timeoutMs</name>
	 <value>6000000</value>
	</property>
	 
	<property>
	 <name>phoenix.query.keepAliveMs</name>
	 <value>6000000</value>
	</property>

</configuration>
  1. apache-phoenix-5.0.0-HBase-2.0-bin:/bin/hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-->
<configuration>
  <property>
    <name>hbase.regionserver.wal.codec</name>
    <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
  </property>

<property>
<name>phoenix.query.timeoutMs</name>
<value>60000000</value>
</property>

</configuration>

安装Hbase,集成Phoenix

下载
hbase-2.0.0-bin.tar.gz
apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz

解压到 /usr/local
在这里插入图片描述

修改 hbase-site.xml配置文件,参考文章最前面的配置。
hbase-2.0.0/conf: hbase-site.xml
apache-phoenix-5.0.0-HBase-2.0-bin:/bin/hbase-site.xml

修改vi hbase-env.sh:
设置JDK地址:export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre
设置使用内置zookeeper:export HBASE_MANAGES_ZK=true

从apache-phoenix-5.0.0-HBase-2.0-bin中复制 phoenix-5.0.0-HBase-2.0-server.jar
到hbase-2.0.0/lib 下
cd /usr/local/apache-phoenix-5.0.0-HBase-2.0-bin
cp phoenix-5.0.0-HBase-2.0-server.jar /usr/local/hbase-2.0.0/lib

然后启动Hbase数据库
./hbase-2.0.0/bin/start-hbase.sh

停止是:
./hbase-2.0.0/bin/stop-hbase.sh

进入hbase shell
./hbase-2.0.0/bin/hbase shell
在这里插入图片描述

进入Phoenix 控制台:
./apache-phoenix-5.0.0-HBase-2.0-bin/bin/sqlline.py 192.168.100.11
在这里插入图片描述
查询数据库所有表:
!tables 或 !table
在这里插入图片描述
到此说明Hbase数据库已经部署成功,并且集成Phoenix成功了。

Squirrel客户端连接Phoenix

1.下载安装Squirrel到本地
在这里插入图片描述

2.添加Phoenix的驱动程序包(驱动包在apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz 这个包里。)到Squirrel的安装路径中,如:D:\Program Files\squirrel-sql-4.0.0\lib

phoenix-core-5.0.0-HBase-2.0.jar
phoenix-pherf-5.0.0-HBase-2.0.jar
phoenix-pherf-5.0.0-HBase-2.0-minimal.jar
phoenix-5.0.0-HBase-2.0-client.jar
phoenix-5.0.0-HBase-2.0-thin-client.jar #如果想支持thin连接,需要加这个包

3.Squirrel 客户端添加Phoenix驱动
在这里插入图片描述

4.胖连接驱动:
Name:自己取
Example URL:jdbc:phoenix:192.168.100.11:2181 (Centos地址:zookeeper端口)
ClassName:加入了驱动包后点击 List Drivers 会自动填充。org.apache.phoenix.jdbc.PhoenixDriver
填写完毕后点击OK
在这里插入图片描述

瘦连接:
Example URL:URI:jdbc:phoenix:thin:url=http://192.168.100.11:8765;serialization=PROTOBUF
Website URL:URI:jdbc:phoenix:thin:url=http://192.168.100.11:8765;serialization=PROTOBUF

ClassName:选择 org.apache.phoenix.queryserver.client.Driver
在这里插入图片描述

5.添加数据库连接实例
Name:自己取
Driver:选择上面刚创建的驱动
填写数据库+密码
点击Test测试连接。
在这里插入图片描述
连接成功后:
查看表、写SQL位置如下
在这里插入图片描述
查询10条记录:
在这里插入图片描述

集成到Springboot+myBatisPlus项目中

0.项目结构
在这里插入图片描述

1.配置信息
pom.xml配置:见文章开头。

application.properties:

spring.profiles.active=druid, mybatisplus

#spring.application.name=demo-druid
#server.servlet.context-path=/demo-druid

#server.port=8080

#logging.file=demo-druid.log

#debug=true
#logging.level.com.yuan=debug

application-druid.yml:

spring:
  datasource:
    driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver
    url: jdbc:phoenix:192.168.100.11:2181
    username:
    password:
#    type: com.alibaba.druid.pool.DruidDataSource
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      # 池中维护的最小空闲连接数
      minimum-idle: 10
      # 池中最大连接数,包括闲置和使用中的连接
      maximum-pool-size: 20
      # 此属性控制从池返回的连接的默认自动提交行为。默认为true
      auto-commit: true
      # 允许最长空闲时间
      idle-timeout: 30000
      # 此属性表示连接池的用户定义名称,主要显示在日志记录和JMX管理控制台中,以标识池和池配置。 默认值:自动生成
      pool-name: custom-hikari
      #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
      max-lifetime: 1800000
      # 数据库连接超时时间,默认30秒,即30000
      connection-timeout: 30000
      # 连接测试sql 这个地方需要根据数据库方言差异而配置 例如 oracle 就应该写成  select 1 from dual
      connection-test-query: SELECT 1

application-mybatisplus.yml:

mybatis-plus:
#  global-config:
#    db-config:
#      id-type: auto
#      field-strategy: not_empty
#      table-underline: true
#      db-type: mysql
#      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
#      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
  mapper-locations: classpath:/mapper/*.xml

相关查询语句编写:
Phoenix查询语句和Mysql 查询差不多
插入和更新不太一样 Phoenix是 UPSERT

例子如下:
MpUserMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuan.demodruid.mapper.MpUserMapper">

    <!-- 通用查询映射结果 -->
    <select id="getAll" resultType="com.yuan.demodruid.domain.entity.MpUser">
        select * from point
    </select>

    <insert id="putOne">
        upsert into TABLE50 values('test','test','test','test','test','test','test','test','test')
    </insert>

    <select id="getOne1" resultType="com.yuan.demodruid.domain.entity.MyHbase">
        select * from TABLE50 where NUMBER=#{id}
    </select>

    <insert id="upsertOne">
        upsert into TABLE50 values(#{MyHbase.number},#{MyHbase.area},#{MyHbase.point},#{MyHbase.type},#{MyHbase.bicycleId},#{MyHbase.time},#{MyHbase.company},#{MyHbase.isDownload},#{MyHbase.isOnline})
    </insert>

    <insert id="upsertOne500">
        upsert into TABLE500 values(#{MyHbase.number},#{MyHbase.area},#{MyHbase.point},#{MyHbase.type},#{MyHbase.bicycleId},#{MyHbase.time},#{MyHbase.company},#{MyHbase.isDownload},#{MyHbase.isOnline})
    </insert>

    <insert id="upsertOne5000">
        upsert into TABLE5000 values(#{MyHbase.number},#{MyHbase.area},#{MyHbase.point},#{MyHbase.type},#{MyHbase.bicycleId},#{MyHbase.time},#{MyHbase.company},#{MyHbase.isDownload},#{MyHbase.isOnline})
    </insert>
</mapper>

Springboot+MybatisPlus查询Hbase中数据 测试

http://localhost:8080/getPoint
在这里插入图片描述

在Phoenix中查询的结果:
在这里插入图片描述

完结

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!