In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use JDBC to achieve mandatory routing in Sharding, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Sublibrary policy package com.zero.sharding.shardingrule;import java.util.Collection;import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;import com.google.common.collect.Sets;public class UserIdDataBaseHintAlgorithm implements HintShardingAlgorithm {@ Override public Collection doSharding (Collection availableTargetNames, HintShardingValue shardingValue) {Collection databaseSet = Sets.newConcurrentHashSet () Collection orderIds = shardingValue.getValues (); for (Long orderId:orderIds) {if (orderId = = 1l) {databaseSet.add ("sharding1") }} / / TODO is routed to the corresponding physical library return databaseSet;}} according to the passed shard value package com.zero.sharding.shardingrule;import java.util.Collection;import java.util.List;import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;import org.apache.shardingsphere.api.sharding.hint.HintShardingValue Import com.google.common.collect.Sets;public class OrderIdTableHintAlgorithm implements HintShardingAlgorithm {@ Override public Collection doSharding (Collection availableTargetNames, HintShardingValue shardingValue) {Collection tableSet = Sets.newConcurrentHashSet (); Collection orderIds = shardingValue.getValues () For (Long orderId:orderIds) {if (orderId = = 21) {tableSet.add ("t_order0");}} / / TODO is routed to the corresponding physical table return tableSet according to the passed shard value }} configure # data source spring.shardingsphere.datasource.names=sharding0 Sharding1# default data source spring.shardingsphere.sharding.default-data-source-name=sharding0# display sqlspring.shardingsphere.props.sql.show=true#sharding0 data source configuration spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.sharding0.url=jdbc:mysql://**:3306/sharding0?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULLspring.shardingsphere.datasource.sharding0 .username = rootspring.shardingsphere.datasource.sharding0.password=**#sharding1 data source configuration spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.sharding1.url=jdbc:mysql://**:3306/sharding1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULLspring.shardingsphere.datasource.sharding1.username=rootspring.shardingsphere.datasource.sharding1.password=**#hint forced routing spring.shardingsphere .sharding .tables.t _ order.database-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.UserIdDataBaseHintAlgorithmspring.shardingsphere.sharding.tables.t_order.table-strategy.hint.algorithm-class-name=com.zero.sharding.shardingrule.OrderIdTableHintAlgorithm# distributed primary key built-in support for these three SNOWFLAKE/UUID/LEAF_SEGMENTspring.shardingsphere.sharding.tables.t_order.key-generator.column=idspring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE usage examples @ Test public void testShardingRead () {OrderExample ex = new OrderExample () Ex.createCriteria (). AndUserIdEqualTo (11) .andOrderIdEqualTo (11); HintManager hintManager = HintManager.getInstance (); hintManager.addDatabaseShardingValue ("t_order", 11); hintManager.addTableShardingValue ("t_order", 21); List orders = orderMapper.selectByExample (ex) Orders.stream () .forEach (o-> {System.out.println ("userId:" + o.getUserId () + "orderId:" + o.getOrderId ();}); hintManager.close () } about how to use JDBC to achieve mandatory routing in Sharding is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 218
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.