溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Sharding JDBC分表怎么配置

發(fā)布時(shí)間:2021-12-22 11:44:12 來(lái)源:億速云 閱讀:203 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“Sharding JDBC分表怎么配置”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

分表配置

由于的單庫(kù)分表,所以只配置一個(gè)數(shù)據(jù)源就可以了

#配置數(shù)據(jù)源
spring.shardingsphere.datasource.names=test0
#test0
spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0
spring.shardingsphere.datasource.test0.username=devadmin
spring.shardingsphere.datasource.test0.password=

設(shè)置綁定表

# 設(shè)置綁定表
spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task

設(shè)置分表的策略

這里根據(jù) record_id 奇偶來(lái)分表的。分為 health_record0,health_record1 和 health_task0,health_task1

# health_record
spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE
# health_task
spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id
spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

完整配置

server.port=8080
#打印sql
spring.shardingsphere.props.sql.show=true
#配置數(shù)據(jù)源
spring.shardingsphere.datasource.names=test0
#test0
spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0
spring.shardingsphere.datasource.test0.username=devadmin
spring.shardingsphere.datasource.test0.password=


# 設(shè)置綁定表
spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task
# health_record
spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE
# health_task
spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id
spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

數(shù)據(jù)庫(kù)如下

Sharding JDBC分表怎么配置

health_level 數(shù)據(jù)如下

Sharding JDBC分表怎么配置

user數(shù)據(jù)如下

Sharding JDBC分表怎么配置

health_record0,health_record1

health_record0 中 record_id 都是偶數(shù) ,health_record1 都是奇數(shù)

Sharding JDBC分表怎么配置 Sharding JDBC分表怎么配置

health_task0,health_task1

health_task0 中 record_id 都是偶數(shù) ,health_task1 都是奇數(shù)

Sharding JDBC分表怎么配置 Sharding JDBC分表怎么配置

“Sharding JDBC分表怎么配置”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI