shardingSphere的精确分片和复杂分片的应用。

订单表实际表DDL如下:

CREATE TABLE `t_order_06` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`user_id` bigint(20) DEFAULT NULL,`order_id` bigint(20) DEFAULT NULL,`regdate` timestamp NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

Maven依赖

<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-core</artifactId><version>4.0.0-RC1</version>
</dependency>

自定义单一字段精确分区算法

public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<java.util.Date> {@Overridepublic String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {String logicTableName = preciseShardingValue.getLogicTableName();Date date = preciseShardingValue.getValue();List<String> shardingSuffix = new ArrayList<>();//获取日期时间所在的月份String str = DateFormatUtil.formatMonth(date);//添加记录所在分表表名集合shardingSuffix.add(logicTableName + "_" + str);return null;}
}

自定义时间字段复杂分区算法

@Slf4j
public class MyComplexShardingAlgorithm implements ComplexKeysShardingAlgorithm {@Overridepublic Collection<String> doSharding(Collection collection, ComplexKeysShardingValue complexKeysShardingValue) {log.info("自定义按照日期进行分表");List<String> shardingSuffix = new ArrayList<>();//获取分表字段及字段值Map<String, Collection<Date>> map = complexKeysShardingValue.getColumnNameAndShardingValuesMap();//获取字段值Collection<Date> shardingValues = map.get("regdate");if (!CollectionUtils.isEmpty(shardingValues)) {for (Date date : shardingValues) {//获取日期时间所在的月份String str = DateFormatUtil.formatMonth(date);//添加记录所在分表表名集合shardingSuffix.add(complexKeysShardingValue.getLogicTableName() + "_" + str);}}return shardingSuffix;}
}

单库分表代码

@Slf4j
public class Demo {public static void main(String[] args) throws SQLException {Map<String, DataSource> dataSourceMap = new HashMap<>();DruidDataSource dataSource1 = new DruidDataSource();dataSource1.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource1.setUrl("jdbc:mysql://localhost:3306/spark?autoReconnect=true&useUnicode=true&characterEncoding" +"=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true");dataSource1.setUsername("root");dataSource1.setPassword("root");dataSourceMap.put("database0", dataSource1);//数据库表分库分表规则TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("t_order");//根据字段进行分库
//        tableRuleConfiguration.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id",
//                "database${user_id % 2}"));//根据字段进行分表
//        tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id",
//                "t_order_${order_id % 2}"));//自定义复杂分表设置 MyComplexShardingAlgorithm自定义分表算法tableRuleConfiguration.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("regdate",new MyComplexShardingAlgorithm()));ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfiguration,new Properties());String sql = "insert into t_order (user_id,order_id,regdate) values (?, ?, ?)";Date date = new Date(System.currentTimeMillis());Connection connection = dataSource.getConnection();PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1, 3);preparedStatement.setInt(2, 2);preparedStatement.setDate(3, date);preparedStatement.execute();}
}

shardingsphere实例应用相关推荐

  1. ShardingSphere实践(1)——ShardingSphere介绍

    目录 一.分库分表 1. 为什么需要分库分表 (1)突破性能瓶颈 (2)提高可用性 2. 什么时候考虑分库分表 3. 如何分库分表 (1)水平拆分与垂直拆分 (2)水平分库分表策略 (3)分成多少库多 ...

  2. ShardingSphere介绍

    什么是 ShardingSphere https://shardingsphere.apache.org/document/current/cn/overview/ 介绍 Apache Shardin ...

  3. ShardingSphere Mode 模式新起航:运行模式详解

    在 5.0.0 GA 版本中,Apache ShardingSphere 新增了运行模式的概念,同时提供了 Memory/Standalone/Cluster 3 种配置方式.ShardingSphe ...

  4. Sharding-JDBC 基础

    Sharding-JDBC 是当当网开源的适用于微服务的分布式数据访问基础类库,完整的实现了分库分表,读写分离和分布式主键功能,并初步实现了柔性事务. 从 2016 年开源至今,在经历了整体架构的数次 ...

  5. SpringBoot配置读写分离

    SpringBoot配置读写分离 1 概述 本文讲述了如何使用MyBatisPlus+ShardingSphereJDBC进行读写分离,以及利用MySQL进行一主一从的主从复制. 具体步骤包括: My ...

  6. 前端开发基础知识汇总

    一.HTML 1.前言与常用标签 浏览器 内核 备注 IE Trident IE.猎豹安全.360极速浏览器.百度浏览器 firefox Gecko 可惜这几年已经没落了,打开速度慢.升级频繁.猪一样 ...

  7. Spring Cloud微服务系统架构的一些简单介绍和使用

    Spring Cloud 目录 特征 云原生应用程序 Spring Cloud上下文:应用程序上下文服务 引导应用程序上下文 应用程序上下文层次结构 改变Bootstrap的位置Properties ...

  8. 京东sdk调用实例_Apache ShardingSphere(Incubating)对接京东白条实战

    作者 张永伦,京东数科高级软件工程师,Apache ShardingSphere(Incubating) PPMC.长期从事分布式系统的高可用.高并发相关工作.热衷于网络IO.性能优化方面的技术挑战. ...

  9. sharding-sphere按月动态分表

    公司有个记录表,每天有几百万的数据,所以我决定按月把他分下表. 用spring整合的. 首先,sharding-sphere不支持自动创建表,所以我提前创建了两年的表,命名规则 logicTableN ...

最新文章

  1. linux创建redis容器,docker-compose实现redis部署及键值添加
  2. 关于未来的蝴蝶效应,《崛起的超级智能》创作有感
  3. 使用 core dump 查找程序遇到严重问题退出的原因
  4. [Linux] 修改系统默认编码
  5. Web框架——Flask系列之数据库迁移(二十)
  6. 使用Unoconv和LibreOffice进行格式转换实现在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件
  7. web系统权限之数据权限
  8. 命名之法 —— 时间、季节、地点
  9. 第四季-专题16-触摸屏驱动程序设计
  10. 建模步骤_SolidWorks建模气球球拖,有步骤,新手都在找的练习题
  11. 细说VAE的来龙去脉 (Variational Autoencoder)
  12. 任正非讲话稿400篇_任正非讲话稿400余篇分享,最全任正非演讲稿下载
  13. 会matlab的简历怎么写,算法工程师简历项目经验填写样本
  14. linux程序休眠,Linux 休眠原理与实现
  15. 吴恩达 :机器学习的六个核心算法
  16. 宝塔php不能上传大文件,解决BT宝塔无法上传大文件的方法,请收藏
  17. 【C++】pcl中的简单点云可视化
  18. 我写代码的这十年——致逝去的青春
  19. Layui表格排序例子
  20. 在群晖中批量删除重复文件

热门文章

  1. 新概念二-非谓语动词
  2. 甲乙两列客车的长分别为150m和200m,它们相向行驶在平行的轨道上,已知甲车上某乘客测得乙车在他窗口外经过的时间为10秒,那么,乙车上的乘客看见甲车在他窗口外经过的时间是()
  3. File上传文件后缀名 限制
  4. ngrok使用操作指南,有了这个不用买服务器了
  5. 潭州教育python学院_潭州什么学院,直播python课程内幕
  6. LP和MIP基础知识
  7. 基于RT-Thread系统的机智云数字仪表教程(一)——移植RT-Thread的BSP模板
  8. 多字节与宽字节的区别
  9. 前端架构带你 封装axios,一次封装终身受益「美团后端连连点赞」
  10. 2016蓝桥杯C组C/C++决赛(公费旅游)总结