準(zhǔn)備
- 確保
hadoop hbase tephra都已經(jīng)開啟。tephra開啟phoenix支持事務(wù)。 -
CREATE SCHEMA SVD_PIC創(chuàng)建SCHEMA -
create table SVD_PIC.PIC (id varchar not null primary key, file VARBINARY) TRANSACTIONAL=true創(chuàng)建表
踩坑
- 開啟
phoenix事務(wù)支持,SCHEMA支持
在hbase中的hbase-site.xml加入以下代碼段。需要在項目中resources文件夾下加入hbase-site.xml文件,服務(wù)端也要加入??蛻舳蓑?qū)動包也要加入該文件。不然數(shù)據(jù)源鏈接時會報錯。
<!-- 開啟phoenix事務(wù)支持 -->
<property>
<name>phoenix.transactions.enabled</name>
<value>true</value>
</property>
<!-- 存儲tx狀態(tài)的快照目錄 -->
<property>
<name>data.tx.snapshot.dir</name>
<value>/tmp/tephra/snapshots</value>
</property>
<!-- 事務(wù)超時時間 -->
<property>
<name>data.tx.timeout</name>
<value>60</value>
</property>
<property>
<name>phoenix.schema.isNamespaceMappingEnabled</name>
<value>true</value>
</property>
<property>
<name>phoenix.schema.mapSystemTablesToNamespace</name>
<value>true</value>
</property>
- 在
intellij idea中創(chuàng)建鏈接phoenix數(shù)據(jù)源
第一步
-
driver files添加phoenix-4.14.1-HBase-1.4-client.jar該jar包來自phoenix發(fā)布包中 - 需要在
phoenix-4.14.1-HBase-1.4-client.jar中添加剛修改過的hbase-site.xml因為 client 包是一個集成包,里面的hbase-site.xml是默認(rèn)的,剛加入的啟用事務(wù)支持,SCHEMA支持需要同步hbase-site.xml文件。不然會報錯。
添加 url 和 driver -
jdbc:phoenix:10.211.55.4:2181;schema=SVD_PIC2181是zookeeper的端口。10.211.55.4ip為集群機(jī)器ip,我配的是偽分布式,所以只有一個ip。ip可以替換成對應(yīng)的host
查詢 phoneix
- 解決關(guān)于
springboot2 + phoenix + swagger2中包沖突的問題。先上解決后的文件。
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
}
}
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.bd'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
// spring
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// 數(shù)據(jù)庫連接池
compile('com.alibaba:druid-spring-boot-starter:1.1.16')
// io 包
compile('commons-io:commons-io:2.6')
// phoenix
compile('org.apache.phoenix:phoenix-core:4.14.1-HBase-1.4') {
// 排除這三個包,跟 spring 包有沖突
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'tomcat', module: 'jasper-runtime'
exclude group: 'log4j', module: 'log4j'
}
compile('io.springfox:springfox-swagger2:2.0.1')
compile('io.springfox:springfox-swagger-ui:2.0.1')
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'com.google.guava', name: 'guava', version: '16.0'
}
apply plugin: "com.arenagod.gradle.MybatisGenerator"
configurations {
mybatisGenerator
}
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/generator.xml'
}
-
phoenix-core包中與springboot的日志、tomcat包沖突排除這三個包的引用 - 高版本的
swagger2中引用高版本的guava包,該包在phoenix-core中也有引用低版本的guava包,gradle會替換低版本的guava包。但是phoenix-core使用一些guava低版本提供的api在高版本中被移除。所以啟動時會報錯。這里我把swagger2的版本降低到最低,同時又把guava指定到16版本,問題解決。
- 要使
phoenix支持事務(wù),需要在服務(wù)端開啟tephra start。我服務(wù)端跑著一臺烏班圖的虛擬機(jī)。開啟tephra start后,通過jps可以看到TransactionServiceMain進(jìn)程。還可以查看日志,日志文件在tmp文件夾中自己找吧。
jps
使用 mybatis generator 生成 sql、model、mapper
具體怎么操作這里我就不說了。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
<classPathEntry location="/Users/zhangbo/Documents/ideaProject/dbDriver/phoenix-4.14.1-HBase-1.4-client.jar" />
<context id="DB2Tables" defaultModelType="flat" targetRuntime="MyBatis3">
<!-- 自動識別數(shù)據(jù)庫關(guān)鍵字,默認(rèn)false,如果設(shè)置為true,根據(jù)SqlReservedWords中定義的關(guān)鍵字列表;
一般保留默認(rèn)值,遇到數(shù)據(jù)庫關(guān)鍵字(Java關(guān)鍵字),使用columnOverride覆蓋
-->
<property name="autoDelimitKeywords" value="false"/>
<!-- 生成的Java文件的編碼 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 格式化java代碼 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代碼 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- beginningDelimiter和endingDelimiter:指明數(shù)據(jù)庫的用于標(biāo)記數(shù)據(jù)庫對象名的符號,比如ORACLE就是雙引號,MYSQL默認(rèn)是`反引號; -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--自動實現(xiàn)Serializable接口-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!--必須有,否則會生成很多注釋-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="org.apache.phoenix.jdbc.PhoenixDriver"
connectionURL="jdbc:phoenix:10.211.55.4:2181;schema=SVD_PIC">
</jdbcConnection>
<!-- java類型處理器
用于處理DB中的類型到Java中的類型,默認(rèn)使用JavaTypeResolverDefaultImpl;
注意一點,默認(rèn)會先嘗試使用Integer,Long,Short等來對應(yīng)DECIMAL和 NUMERIC數(shù)據(jù)類型;
-->
<!--<javaTypeResolver type="org.mybatis.generator.internal.type.JavaTypeResolverDefaultImpl">-->
<!--<!–-->
<!--true:使用BigDecimal對應(yīng)DECIMAL和 NUMERIC數(shù)據(jù)類型-->
<!--false:默認(rèn),-->
<!--scale>0;length>18:使用BigDecimal;-->
<!--scale=0;length[10,18]:使用Long;-->
<!--scale=0;length[5,9]:使用Integer;-->
<!--scale=0;length<5:使用Short;-->
<!--–>-->
<!--<property name="forceBigDecimals" value="false"/>-->
<!--</javaTypeResolver>-->
<javaModelGenerator targetPackage="com.bd.springphoenixhbase.pic.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<!-- 設(shè)置是否在getter方法中,對String類型字段調(diào)用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mapper">
<!-- 在targetPackage的基礎(chǔ)上,根據(jù)數(shù)據(jù)庫的schema再生成一層package,最終生成的類放在這個package下,默認(rèn)為false -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.bd.springphoenixhbase.pic.mapper" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 選擇一個table來生成相關(guān)文件,可以有一個或多個table,必須要有table元素
選擇的table會生成一下文件:
1,SQL map文件
2,生成一個主鍵類;
3,除了BLOB和主鍵的其他字段的類;
4,包含BLOB的類;
5,一個用戶生成動態(tài)查詢的條件類(selectByExample, deleteByExample),可選;
6,Mapper接口(可選)
tableName(必要):要生成對象的表名;
注意:大小寫敏感問題。正常情況下,MBG會自動的去識別數(shù)據(jù)庫標(biāo)識符的大小寫敏感度,在一般情況下,MBG會
根據(jù)設(shè)置的schema,catalog或tablename去查詢數(shù)據(jù)表,按照下面的流程:
1,如果schema,catalog或tablename中有空格,那么設(shè)置的是什么格式,就精確的使用指定的大小寫格式去查詢;
2,否則,如果數(shù)據(jù)庫的標(biāo)識符使用大寫的,那么MBG自動把表名變成大寫再查找;
3,否則,如果數(shù)據(jù)庫的標(biāo)識符使用小寫的,那么MBG自動把表名變成小寫再查找;
4,否則,使用指定的大小寫格式查詢;
另外的,如果在創(chuàng)建表的時候,使用的""把數(shù)據(jù)庫對象規(guī)定大小寫,就算數(shù)據(jù)庫標(biāo)識符是使用的大寫,在這種情況下也會使用給定的大小寫來創(chuàng)建表名;
這個時候,請設(shè)置delimitIdentifiers="true"即可保留大小寫格式;
可選:
1,schema:數(shù)據(jù)庫的schema;
2,catalog:數(shù)據(jù)庫的catalog;
3,alias:為數(shù)據(jù)表設(shè)置的別名,如果設(shè)置了alias,那么生成的所有的SELECT SQL語句中,列名會變成:alias_actualColumnName
4,domainObjectName:生成的domain類的名字,如果不設(shè)置,直接使用表名作為domain類的名字;可以設(shè)置為somepck.domainName,那么會自動把domainName類再放到somepck包里面;
5,enableInsert(默認(rèn)true):指定是否生成insert語句;
6,enableSelectByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵查詢對象的語句(就是getById或get);
7,enableSelectByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)查詢語句;
8,enableUpdateByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵修改對象的語句(即update);
9,enableDeleteByPrimaryKey(默認(rèn)true):指定是否生成按照主鍵刪除對象的語句(即delete);
10,enableDeleteByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)刪除語句;
11,enableCountByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)查詢總條數(shù)語句(用于分頁的總條數(shù)查詢);
12,enableUpdateByExample(默認(rèn)true):MyBatis3Simple為false,指定是否生成動態(tài)修改語句(只修改對象中不為空的屬性);
13,modelType:參考context元素的defaultModelType,相當(dāng)于覆蓋;
14,delimitIdentifiers:參考tableName的解釋,注意,默認(rèn)的delimitIdentifiers是雙引號,如果類似MYSQL這樣的數(shù)據(jù)庫,使用的是`(反引號,那么還需要設(shè)置context的beginningDelimiter和endingDelimiter屬性)
15,delimitAllColumns:設(shè)置是否所有生成的SQL中的列名都使用標(biāo)識符引起來。默認(rèn)為false,delimitIdentifiers參考context的屬性
注意,table里面很多參數(shù)都是對javaModelGenerator,context等元素的默認(rèn)屬性的一個復(fù)寫;
-->
<table schema="SVD_PIC" tableName="PIC"
domainObjectName="Pic"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByPrimaryKey="false"
>
<!--<generatedKey column="id" sqlStatement="JDBC"/>-->
</table>
</context>
</generatorConfiguration>



