ShardingSphere水平、垂直分库、分表和公共表

news2024/9/29 18:06:49

目录

  • 一、ShardingSphere简介
  • 二、ShardingSphere-分库分表
    • 1、垂直拆分
      • (1)垂直分库
      • (2)垂直分表
    • 2、水平拆分
      • (1)水平分库
      • (2)水平分表
  • 三、水平分库操作
    • 1、创建数据库和表
    • 2、配置分片的规则
    • 3、测试类
  • 四、水平分表操作
    • 1、引入依赖
    • 2、创建数据库、表
    • 3、分片策略
    • 4、编写代码实现
      • (1)实体类
      • (2)编写Mapper
      • (3)启动类修改(添加扫描mapper的入口)
      • (4)测试类(采用springboot给我们提供的)
  • 五、垂直分库/分表操作
    • 1、创建数据库、表
    • 2、分片策略
    • 3、编写代码实现
      • (1)创建User实体类
      • (2)Mapper
    • 4、测试
  • 六、公共表
    • 1、概念
    • 2、实现
      • (1)在三个数据库中创建公共表
      • (2)公共表的配置
      • (3)测试代码
    • 3、相关问题及解决方法
      • (1)Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'null' in 'class com.chenxin.shardingsphere.entity.Udict'

一、ShardingSphere简介

Apache ShardingSphere 是一个开源的分布式数据库中间件解决方案组成的生态圈,且它的产品有Sharding-JDBC和Sharding-Proxy组成(他们两个之间是独立的),同时又能混合部署(组合起来一起使用)。它们都提供了标准化的数据分片、分布式事务和数据库的治理能力,可适用如Java、云原生开发的应用场景。

ShardingSphere定位是关系型数据库中间件,目的是充分为了合理地在分布式的场景下利用关系型数据库的计算能力和存储能力,而不是实现一个全新的关系型数据库。

二、ShardingSphere-分库分表

1、垂直拆分

(1)垂直分库

垂直分库:把单一的数据库按照业务的不同进行划分(专库专表)
在这里插入图片描述

(2)垂直分表

操作数据库中的某张表,我们把这张表里面的一部分字段拿出来存储到一张新的表里,剩下的字段放在另一张表里。

在这里插入图片描述

2、水平拆分

(1)水平分库

水平分库相当于把数据库水平切割,原来一个表中的数据可能会分配到不同的数据库中,这就是水平分库。
在这里插入图片描述

(2)水平分表

水平分表就是把表中的数据进行了水平切割,意味着按照行进行切割,也就是说不同行的数据被切割后可能在不同的表中。
在这里插入图片描述

三、水平分库操作

1、创建数据库和表

-- 创建两个数据库
CREATE DATABASE edu_db_1;
CREATE DATABASE edu_db_2;
-- 需要在两个数据库中都执行下面的脚本
CREATE TABLE course_1(
    cid BIGINT(20) PRIMARY KEY,
    cname VARCHAR(20) NOT NULL,
    user_id BIGINT(20) NOT NULL,
    cstatus VARCHAR(20) NOT NULL
);


CREATE TABLE course_2(
    cid BIGINT(20) PRIMARY KEY,
    cname VARCHAR(20) NOT NULL,
    user_id BIGINT(20) NOT NULL,
    cstatus VARCHAR(20) NOT NULL
);

2、配置分片的规则

spring.shardingsphere.datasource.names=ds-1,ds-2

spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=root

spring.shardingsphere.datasource.ds-2.jdbc-url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2.username=root
spring.shardingsphere.datasource.ds-2.password=root


spring.main.allow-bean-definition-overriding=true


spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-$->{1..2}.course_$->{1..2}


spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}

spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=ds-$->{user_id % 2 + 1}


spring.shardingsphere.props.sql.show=true

3、测试类

// ------------- 测试分库 ---------------------
    @Test
    void addCorseFpdb() {
        Course course = new Course();
        course.setCname("java01");
        // 根据user_id进行分库,偶数是在edu_db_1,基数是在edu_db_2
        course.setUserId(100L);
        course.setCstatus("已启用");
        courseMapper.insert(course);
    }

四、水平分表操作

1、引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.17</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.0.0</version>
    </dependency>

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2、创建数据库、表

create table course_1(
    cid bigint(20) primary key,
    cname varchar(20) not null,
    user_id bigint(20) not null,
    cstatus varchar(20) not null
);

create table course_2(
    cid bigint(20) primary key,
    cname varchar(20) not null,
    user_id bigint(20) not null,
    cstatus varchar(20) not null
);

3、分片策略

spring.shardingsphere.datasource.names=ds-0

spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/course_db?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=root


spring.main.allow-bean-definition-overriding=true


spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-0.course_$->{1..2}


spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1}


spring.shardingsphere.props.sql.show=true

4、编写代码实现

(1)实体类

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Course {
    private Long cid;
    private String cname;
    private Long userId;
    private String cstatus;
}

(2)编写Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.Course;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface CourseMapper extends BaseMapper<Course> {
}

(3)启动类修改(添加扫描mapper的入口)

@SpringBootApplication
@MapperScan("com.chenxin.shardingsphere.mapper")
public class ShardingsphereApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShardingsphereApplication.class, args);
    }

}

(4)测试类(采用springboot给我们提供的)

@SpringBootTest
class ShardingsphereApplicationTests {

    @Resource
    CourseMapper courseMapper;
    
    @Test
    void addCourse() {
        Course course = null;
        for (int i = 0; i < 100; i++) {
            course = new Course();
            course.setCname("Java");
            course.setUserId(1000L);
            course.setCstatus("Nor1");
            courseMapper.insert(course);
        }
    }

}

五、垂直分库/分表操作

1、创建数据库、表

CREATE DATABASE user_db;
USE user_db;
CREATE TABLE T_USER(
	user_id BIGINT(20) NOT NULL PRIMARY KEY,
    username VARCHAR(100) NOT NULL,
    ustatus VARCHAR(50) NOT NULL
);

2、分片策略

spring.shardingsphere.datasource.names=ds-1,ds-2,ds-3

spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=root

spring.shardingsphere.datasource.ds-2.jdbc-url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2.username=root
spring.shardingsphere.datasource.ds-2.password=root

spring.shardingsphere.datasource.ds-3.jdbc-url=jdbc:mysql://localhost:3306/user_db?serverTimezone=Asia/Shanghai&useSSL=false
spring.shardingsphere.datasource.ds-3.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-3.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-3.username=root
spring.shardingsphere.datasource.ds-3.password=root


spring.main.allow-bean-definition-overriding=true


# spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds-$->{1..2}.course_$->{1..2}
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=ds-$->{3}.t_user


spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user

spring.shardingsphere.props.sql.show=true

3、编写代码实现

(1)创建User实体类

import lombok.Data;
import lombok.ToString;

// 实体类添加注解
@TableName(value = "t_user")
@Data
@ToString
public class User {
    private Long userId;
    private String username;
    private String ustatus;
}

(2)Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

4、测试

// ------------- 测试垂直分库 ----------------------
@Test
void addUser() {
    User user = new User();
    user.setUsername("chenxin");
    user.setUstatus("a");
    userMapper.insert(user);
}

六、公共表

1、概念

存储固定数据的表,表里面的数据很少发生变化,查询的时候经常进行关联查询

在每个数据库中创建出相同的结构的表(我们将每个数据库中共有的表成为公共表)

2、实现

(1)在三个数据库中创建公共表

CREATE TABLE t_udict(
	dicid bigint(20) primary key,
    ustatus varchar(20) not null,
    uvalue varchar(100) not null
);

(2)公共表的配置

spring.shardingsphere.sharding.broadcast-tables=t_udict
spring.shardingsphere.sharding.tables.t_udict.key-generator.column=dicid
spring.shardingsphere.sharding.tables.t_udict.key-generator.type=SNOWFLAKE

(3)测试代码

创建实体类

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
@TableName(value = "t_udict")
public class Udict {
    private Integer dicid;
    private String ustatus;
    private String uvalue;
}

Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenxin.shardingsphere.entity.Udict;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UdictMapper extends BaseMapper<Udict> {
}

测试代码

@Test
void addDict() {
    Udict udict = new Udict();
    udict.setUstatus("0");
    udict.setUvalue("已启用");
    udictMapper.insert(udict);
}

@Test
void updateDict() {
    Udict udict = new Udict();
    udict.setDicid(792033627201339393L);
    udict.setUstatus("1");
    udict.setUvalue("未启用");
    udictMapper.updateById(udict);
}

3、相关问题及解决方法

(1)Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘null’ in ‘class com.chenxin.shardingsphere.entity.Udict’

问题原因
数据库中字段名和实体类中相差太大,程序自己对应不上

解决方法

// 需要在实体类指定主键是哪一个属性
@Data
@ToString
@TableName(value = "t_udict")
public class Udict {
    @TableId
    private Long dicid;
    private String ustatus;
    private String uvalue;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/375266.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

中级嵌入式系统设计师2016下半年上午试题及答案解析

中级嵌入式系统设计师2016下半年上午试题 单项选择题 1、(1)用来区分在存储器中以二进制编码形式存放的指令和数据。 A. 指令周期的不同阶段 B. 指令和数据的寻址方式 C. 指令操作码的译码结果 D. 指令和数据所在的存储单元 2、计算机在一个指令周期的过程中,为从…

web服务器(1)

阻塞和非阻塞、同步和异步 网络IO阶段一&#xff1a;数据就绪 操作系统&#xff0c;tcp接受缓冲区 阻塞&#xff1a;调用IO方法的线程进入阻塞状态 非阻塞&#xff1a;不会改变线程的状态&#xff0c;通过返回值判断 网络IO阶段二&#xff1a;数据读写 应用程序 同步…

接口自动化框架---升级版(Pytest+request+Allure)

目录&#xff1a;导读 一、简单介绍 二、目录介绍 三、代码分析 写在最后 接口自动化是指模拟程序接口层面的自动化&#xff0c;由于接口不易变更&#xff0c;维护成本更小&#xff0c;所以深受各大公司的喜爱。 第一版入口&#xff1a;接口自动化框架(PytestrequestAllure…

[Android Studio] Android Studio使用keytool工具读取Debug 调试版数字证书以及release 发布版数字证书

&#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea; Android Debug&#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea; Topic 发布安卓学习过程中遇到问题解决过程&#xff0c;希望我的解决方案可以对小伙伴们有帮助。 &#x1f4cb;笔记目…

学生宿舍管理系统

技术&#xff1a;Java、JSP等摘要&#xff1a;管理信息系统在现代社会已深入到各行各业&#xff0c;由于计算机技术的迅速发展和普及&#xff0c;信息管理系统MIS事实上已成为计算机管理信息系统,大学生宿舍管理系统就是一个典型的管理信息系统&#xff0c;它可以让宿舍管理工作…

【算法题】最大矩形面积,单调栈解法

力扣&#xff1a;84. 柱状图中最大的矩形 给定 n 个非负整数&#xff0c;用来表示柱状图中各个柱子的高度。每个柱子彼此相邻&#xff0c;且宽度为 1 。 求在该柱状图中&#xff0c;能够勾勒出来的矩形的最大面积。 题意很简单&#xff0c;翻译一下就是&#xff1a;求该图中…

模拟银行存取钱-课后程序(JAVA基础案例教程-黑马程序员编著-第八章-课后作业)

【案例8-3】 模拟银行存取钱 【案例介绍】 1.任务描述 在银行办理业务时&#xff0c;通常银行会开多个窗口&#xff0c;客户排队等候&#xff0c;窗口办理完业务&#xff0c;会呼叫下一个用户办理业务。本案例要求编写一个程序模拟银行存取钱业务办理。假如有两个用户在存取…

【Linux】-- POSIX信号量

目录 POSIX信号量 sem_init - 初始化信号量 sem_destroy - 销毁信号量 sem_wait - 等待信号量&#xff08;P操作&#xff09; 基于环形队列的生产消费模型 数据结构 - 环形结构 实现原理 POSIX信号量 #问&#xff1a;什么是信号量&#xff1f; 1. 共享资源 -> 任何一…

2. 驱动开发--驱动开发环境搭建

文章目录前言一、Linux中配置编译环境1.1 linux下安装软件的方法1.2 交叉编译工具链的安装1.2.1 测试是否安装成功1.3 设置环境变量1.3.1 将工具链导出到环境变量1.4 为工具链创建arm-linux-xxx符号链接二、 搭建运行开发环境2.1 tftp网络方式加载内核和设备树文件2.2 nfs网络方…

大事很妙,跨境电商用Reddit做营销做测评真的很有用

最近呢&#xff0c;东哥在和一个叫 jens 的海外社媒大佬聊天&#xff0c;聊起了Reddit&#xff0c;其实 Reddit 可是个不错的流量平台&#xff0c;里面有不少宝藏&#xff0c;跟我们国内的贴吧差不多啦。 作为美国热度排名前五的社交网站&#xff0c;流量如此不错的平台&#…

3、Improved Denoising Diffusion Probabilistic Models#

简介论文发现通过一些简单的修改&#xff0c;ddpm也可以在保持高样本质量的同时实现竞争对数可能性&#xff0c;反向扩散过程的学习方差允许以更少的正向传递数量级进行采样&#xff0c;而样本质量的差异可以忽略不计&#xff0c;这对于这些模型的实际部署非常重要。 github链接…

AOF:redis宕机,如何避免数据丢失

由于redis是基于内存的数据库&#xff0c;一旦宕机&#xff0c;数据就会丢失?如何解决&#xff1f; 目前&#xff0c;Redis 的持久化主要有两大机制&#xff0c;即 AOF&#xff08;Append Only File&#xff09;日志和 RDB&#xff08;Redis DataBase&#xff09; 快照。 AO…

SQL零基础入门学习(十四)

上篇&#xff1a;SQL零基础入门学习&#xff08;十三&#xff09; SQL NULL 值 NULL 值代表遗漏的未知数据。 默认地&#xff0c;表的列可以存放 NULL 值。 如果表中的某个列是可选的&#xff0c;那么我们可以在不向该列添加值的情况下插入新记录或更新已有的记录。这意味着该…

基于新一代kaldi项目的语音识别应用实例

本文是由郭理勇在第二届SH语音技术研讨会和第七届Kaldi技术交流会上对新一代kaldi项目在学术及“部署”两个方面报告的内容上的整理。如果有误&#xff0c;欢迎指正。 文字整理丨李泱泽 编辑丨语音小管家 喜报&#xff1a;新一代Kaldi团队三篇论文均被语音顶会ICASSP-2023接…

亿级高并发电商项目-- 实战篇 --万达商城项目 十三(编写购物车、优化修改商品、下架商品方法、购物车模块监听修改商品、删除商品消息)

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是小童&#xff0c;Java开发工程师&#xff0c;CSDN博客博主&#xff0c;Java领域新星创作者 &#x1f4d5;系列专栏&#xff1a;前端、Java、Java中间件大全、微信小程序、微信支付、若依框架、Spring全家桶 &#x1f4…

SSL证书对虚拟主机的用处有哪些?

虚拟主机是指在同一台服务器上&#xff0c;通过不同的域名或IP地址为多个网站提供服务的一种网络主机。而SSL证书则是一种数字证书&#xff0c;它用于加密网站与用户之间的通信&#xff0c;确保数据传输的安全性和完整性。在虚拟主机上&#xff0c;SSL证书有以下几个用处&#…

SQL Server2008详细安装步骤(保姆式教程)

安装包下载 链接&#xff1a;https://pan.baidu.com/s/1Rjx4DHJBeCW2asC_4Kzo6Q?pwdchui 提取码&#xff1a;chui 安装过程 1.解压后使用管理员身份打开安装程序 2.选择全新安装或向现有安装添加新功能 3.确认 4.输入产品密钥&#xff08;上方网盘安装包里有&#xff0…

【路径规划】基于前向动态规划算法在地形上找到最佳路径(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

【ArcGIS Pro二次开发】(10):属性表字段(field)的修改

在ArcGIS Pro中&#xff0c;经常会遇到用字段计算器对要素的属性表进行计算。下面以一个例子演示如何在ArcGIS Pro SDK二次开发中实现。 一、要实现的功能 如上图所示的要素图层&#xff0c;要实现如下功能&#xff1a; 当字段【市级行政区】的值为【泉州市】时&#xff0c;将…

用 .NET 启动你的 DJI Ryze Tello 无人机

大疆的 DJI Ryze Tello 是入门级的无人机&#xff0c;不仅在 STEM 教育中有非常广泛的应用&#xff0c;也可以作为编程入门的首选。通过 UDP 协议调用 DJI Ryze Tello SDK 可以让 DJI Ryze Tello 无人机执行起飞&#xff0c;降落&#xff0c;转向以及不同的花式动作。本文将会通…