Java中sharding-jdbc按年月分片的示例代碼
Pom依賴
<!--shardingjdbc分片,和Druid不兼容,如果不使用sharding則需要注釋-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>Yml配置
spring:
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
main:
allow-bean-definition-overriding: true
shardingsphere:
#配置數(shù)據(jù)源
datasource:
names: ds-master
ds-master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://***:3306/aihosp?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true
username: ***
password: ***
sharding:
tables:
table1:
actual-data-nodes: ds-master.table1_$->{2021..2025} #按年分表
tableStrategy:
standard: #用于單分片鍵的標(biāo)準(zhǔn)分片場(chǎng)景
sharding-column: create_time
precise-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm # 精確分片算法類名稱,用于=和IN。該類需實(shí)現(xiàn)PreciseShardingAlgorithm接口并提供無(wú)參數(shù)的構(gòu)造器
range-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm #范圍分片算法類名稱,用于BETWEEN,可選。該類需實(shí)現(xiàn)RangeShardingAlgorithm接口并提供無(wú)參數(shù)的構(gòu)造器
key-generator:
column: id
type: SNOWFLAKE #分布式全局ID(雪花算法)
retry-interval-milliseconds: 500
table2:
actual-data-nodes: ds-master.table2_$->{2022..2025}0$->{1..9},ds-master.table2_$->{2022..2025}1$->{0..2} #按月分表
tableStrategy:
standard: #用于單分片鍵的標(biāo)準(zhǔn)分片場(chǎng)景
sharding-column: create_date
precise-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm
range-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm
key-generator:
column: id
type: SNOWFLAKE #分布式全局ID(雪花算法)
retry-interval-milliseconds: 500
#其他運(yùn)行屬性
props:
sql:
show: false # 是否顯示日志時(shí)間策略
/**
*
* 按年分片
* 精準(zhǔn)分庫(kù)PreciseShardingDBAlgorithm
*
* 范圍分庫(kù)RangeShardingDBAlgorithm
*
* 精準(zhǔn)分表PreciseShardingTableAlgorithm
*
* 范圍分表RangeShardingTableAlgorithm:
*/
@Slf4j
public class PreciseRangeShardingAlgorithm implements PreciseShardingAlgorithm<String>,RangeShardingAlgorithm<String> {
/**
* RangeShardingAlgorithm的重寫(xiě) 根據(jù)傳入的分片健的值,對(duì)所有待選擇的表中 根據(jù)自己的業(yè)務(wù)邏輯進(jìn)行判斷,選擇符合條件的表返回
* @param tableNameList 返回需要查詢的表
* @param shardingValue 傳入的分片健的值
* @return 返回符合條件的表名稱
*/
@Override
public Collection<String> doSharding(Collection<String> tableNameList, RangeShardingValue<String> shardingValue) {
System.out.println("[MyTableRangeShardingAlgorithm] shardingValue: [{}]\n"+ shardingValue);
Set<String> tableNameResultList = new LinkedHashSet<>();
Range<String> rangeValue = shardingValue.getValueRange();
String flag = "year";
for (String tableName : tableNameList) {
if (tableName.startsWith("table2")) {
flag = "month";
break;
}
}
if ("year".equals(flag)) {
int lowInt = Integer.parseInt(rangeValue.lowerEndpoint().substring(0,5).replaceAll("-",""));
int upperInt = Integer.parseInt(rangeValue.upperEndpoint().substring(0,5).replaceAll("-",""));
for (String tableNameItem : tableNameList) {
String substring = tableNameItem.substring(tableNameItem.length() - 4);
int tableItem = Integer.valueOf(substring);
if(tableItem >= lowInt && tableItem <= upperInt ){
tableNameResultList.add(tableNameItem);
}
}
} else if ("month".equals(flag)) {
int lowInt = Integer.parseInt(rangeValue.lowerEndpoint().substring(0,7).replaceAll("-",""));
int upperInt = Integer.parseInt(rangeValue.upperEndpoint().substring(0,7).replaceAll("-",""));
for (String tableNameItem : tableNameList) {
String substring = tableNameItem.substring(tableNameItem.length() - 6,tableNameItem.length());
int tableItem = Integer.valueOf(substring);
if(tableItem >= lowInt && tableItem <= upperInt ){
tableNameResultList.add(tableNameItem);
}
}
}
return tableNameResultList;
}
/** PreciseShardingAlgorithm的重寫(xiě) */
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
String s = buildShardingTable(preciseShardingValue.getLogicTableName(), preciseShardingValue.getValue());
return s;
}
/**
* 構(gòu)建分片后的表名
* @param logicTableName
* @param date
* @return
*/
private String buildShardingTable(String logicTableName, String date) {
StringBuffer stringBuffer = new StringBuffer(logicTableName).append("_").append(date, 0, 4);
if (logicTableName.startsWith("table2")) {
// 月分表
stringBuffer = new StringBuffer(logicTableName).append("_").append(date, 0, 4)
.append(date, 5, 7);
}
return stringBuffer.toString();
}
}到此這篇關(guān)于Java中sharding-jdbc按年月分片的示例代碼的文章就介紹到這了,更多相關(guān)Java sharding-jdbc按年月分片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一個(gè)Servlet是如何處理多個(gè)請(qǐng)求的?
以前我一直以為一個(gè)Servlet只能處理一個(gè)請(qǐng)求,后來(lái)發(fā)現(xiàn)是自己太菜了,可以借助攜帶一個(gè)參數(shù)來(lái)完成多個(gè)請(qǐng)求的處理,根據(jù)參數(shù)的不同,在核心的service方法中調(diào)用不同的業(yè)務(wù)方法,來(lái)實(shí)現(xiàn)處理多個(gè)servlet請(qǐng)求的目的,廢話不多說(shuō),直接上代碼,需要的朋友可以參考下2021-06-06
Jboss Marshalling服務(wù)端無(wú)法接受消息
這篇文章主要介紹了Jboss Marshalling服務(wù)端無(wú)法接受消息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
簡(jiǎn)化API提升開(kāi)發(fā)效率RestTemplate與HttpClient?OkHttp關(guān)系詳解
這篇文章主要為大家介紹了簡(jiǎn)化API,提升開(kāi)發(fā)效率,RestTemplate與HttpClient?OkHttp關(guān)系介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
JAVA 字符串加密、密碼加密實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA 字符串加密、密碼加密實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
SpringBoot利用filter實(shí)現(xiàn)xss防御功能
Cross-Site?Scripting(跨站腳本攻擊)簡(jiǎn)稱?XSS,是一種代碼注入攻擊,攻擊者通過(guò)在目標(biāo)網(wǎng)站上注入惡意腳本,使之在用戶的瀏覽器上運(yùn)行,利用這些惡意腳本,攻擊者可獲取用戶的敏感信息,本文給大家介紹了SpringBoot利用filter實(shí)現(xiàn)xss防御功能,需要的朋友可以參考下2024-09-09
Java設(shè)計(jì)模式中的七大原則詳細(xì)講解
本篇文章主要對(duì)Java中的設(shè)計(jì)模式如,創(chuàng)建型模式、結(jié)構(gòu)型模式和行為型模式以及7大原則進(jìn)行了歸納整理,需要的朋友可以參考下,希望能給你帶來(lái)幫助2023-02-02
SpringBoot中動(dòng)態(tài)注入Bean的技巧分享
在 Spring Boot 開(kāi)發(fā)中,動(dòng)態(tài)注入 Bean 是一種強(qiáng)大的技術(shù),它允許我們根據(jù)特定條件或運(yùn)行時(shí)環(huán)境靈活地創(chuàng)建和管理 Bean,本文將介紹 Spring Boot 中三種動(dòng)態(tài) Bean 注入技巧,需要的可以參考一下2025-06-06
SpringBoot+Mybatis分頁(yè)插件PageHelper實(shí)現(xiàn)分頁(yè)效果
這篇文章主要介紹了SpringBoot+Mybatis實(shí)現(xiàn)分頁(yè)效果,本案例是采用Mybatis分頁(yè)插件PageHelper實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11

