easy-upsert-starter 轻量级数据转换存储服务
简介:
针对轻量级数据(单次包数据量10万)处理,提供数据转移存储到MySQL、Kafka、ES、HBase、Redis 配置相应的数据源,注入IUpser对象并使用upsert方法
快速开始:
我们将通过一个简单的 Demo 来阐述 Upsert功能,在此之前,我们假设您已经:
- 拥有 Java 开发环境以及相应 IDE
- 熟悉 Spring Boot
- 熟悉 Maven
现有一个数据库 temp
初始化工程
创建一个空的 Spring Boot 工程
可以使用 Spring Initializer (opens new window)快速初始化一个 Spring Boot 工程
添加依赖
引入 Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>top.wu2020</groupId>
<artifactId>wu-easy-upsert-starter</artifactId>
</dependency>
</dependencies>
引入 spring-boot-starter
、wu-easy-upsert-starter依赖:
MySQL 数据源配置和使用
配置
Spring Boot 启动类:
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
@Bean(name = "dataSourceMySQL")
public DataSource dataSourceMySQL() {
MysqlDataSource build = DataSourceBuilder.create().type(MysqlDataSource.class).build();
build.setUrl("jdbc:mysql://127.0.0.1:3306/temp?rewriteBatchedStatements=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
build.setUser("root");
build.setPassword("wujiawei");
return build;
}
}
编码
编写实体类 UserLog.java
(此处使用了 Lombok 简化代码)
@Data
@EasySmart(perfectTable = true)
public class UserLog {
private Integer userId;
@EasySmartField(name = "`current_time`")
private LocalDateTime currentTime;
@EasySmartField(name = "`content`")
private String content;
@EasySmartField(name = "is_succeed")
private boolean isSucceed;
private String type;
}
编写实体类 UpsertBinary.java
(此处使用了 Lombok 简化代码)
@EasySmart(perfectTable = true)
@Data
public class UpsertBinary {
private File file = new File("/Users/wujiawei/Desktop/aa.mp3");
private String name = file.getName();
}
开始使用
- 添加测试类,使用IUpsert测试:
/**
* description 创建测试数据
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:09
*/
public List<UserLog> createUserLog(Integer size) {
List<UserLog> userLogList = new ArrayList<>();
size = size == null ? 10000 : size;
for (int i = 0; i < size; i++) {
UserLog userLog = new UserLog();
userLog.setCurrentTime(LocalDateTime.now());
userLog.setContent("创建时间:" + userLog.getCurrentTime());
userLog.setUserId(i);
userLogList.add(userLog);
}
return userLogList;
}
/**
* description IUpsert操作数据入DB
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/15 上午9:50
*/
@EasyUpsertDS(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "IUpsert操作数据入DB")
@GetMapping()
public List<UserLog> upsert(@RequestParam(required = false, defaultValue = "100") Integer size) {
List<UserLog> userLogList = createUserLog(size);
iUpsert.upsert(userLogList, userLogList, new UserLog());
return userLogList;
}
- 使用注解测试
/**
* description 使用注解实现数据插入
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:11
*/
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "使用注解实现数据插入")
@GetMapping("/size")
public List<UserLog> upsertSize(@RequestParam(required = false, defaultValue = "100") Integer size) {
return createUserLog(size);
}
- 使用注解&特殊实体EasyHashMap测试
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "复杂数据EasyHashMap")
@GetMapping("/easyHashMap")
public List<EasyHashMap> easyHashMap(@RequestParam(required = false, defaultValue = "1000") Integer size) {
List<EasyHashMap> easyHashMapList = new ArrayList<>();
for (int i = 0; i < size; i++) {
EasyHashMap easyHashMap = new EasyHashMap("uniqueLabel");
easyHashMap.put("第一个字段", "第一个字段");
easyHashMap.put("第二个字段", "第二个字段");
easyHashMap.put("第三个字段", "第三个字段");
easyHashMap.put("第四个字段", "第四个字段");
easyHashMapList.add(easyHashMap);
}
return easyHashMapList;
}
- 数据中含有二进制数据
/**
* description binary 或者文件类型数据插入
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:11
*/
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "binary 数据插入")
@GetMapping("/binary")
public List<UpsertBinary> binary(@RequestParam(required = false, defaultValue = "1000") Integer size) {
List<UpsertBinary> upsertBinaryList = new ArrayList<>();
for (int i = 0; i < size; i++) {
upsertBinaryList.add(new UpsertBinary());
}
return upsertBinaryList;
}
注解
介绍 upsert 注解包相关类详解(更多详细描述可点击查看源码注释)
@EasySmart
- 描述注解 简单灵性数据标注 (表注解)
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | 类名驼峰转下滑线 | 表名 |
tableName | String | 否 | 类名驼峰转下滑线 | 表名 |
perfectTable | boolean | 否 | false | 是否完善表,当表不存在创建表、表字段格式不一致会、数据库表为主,实体字段为辅 |
comment | String | 否 | "" | 表注释(创建表时起效) |
dataDrillDown | boolean | 否 | false | 是否支持数据下钻(当类中属性含有@SmartMark并且不是基本数据类型时有效) |
schema | String | 否 | "" | schema |
关于`EasySmart`的说明:
EasySmart注解集成LayerClass注解、LazyTable注解并支持自定义继承当前注解:
- 定义在comment中的描述当调用自动创建表语句时有效如:SQLConverter.creatTableSQL(UserLog.class) 控制台打印sql
- value与tableName声明同个字段表名
@EasySmartField
- 描述注解 灵性字段注解 (表对应的字段)
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | 字段驼峰转换下划线 | 数据库字段名 |
name | String | 否 | 字段驼峰转换下划线 | 数据库字段名 |
exist | boolean | 否 | true | 是否为数据库表字段 |
type | String | 否 | "" | 数据库对应字段类型,创建数据时有效 |
comment | String | 否 | "" | 字段描述 |
fieldDefaultValue | String | 否 | "" | 字段默认值(字段为空是有效) |
iEnum | Enum | 否 | DefaultIEnum | 当前字段值和枚举类中的code值进行比较,如果找到将item值赋值给字段,转换失败赋值-1 |
indexType | Enum | 否 | LayerField.LayerFieldType.FILE_TYPE | 字段类型FILE_TYPE,ID, UNIQUE, AUTOMATIC; |
@EasyUpsertDS
- 描述注解 进行数据源切换(mysql配置多数据源进行数据源切换)
关于`EasyUpsertDS`的说明:
EasyUpsertDS支持自定义继承当前注解:
- 作用域方法上、类上
- MySQL下使用,支持MyBatis的动态数据源配置的多数据源,当自定义注入DataSource数据源名称为
@Bean(name = "dataSourceMySQL")中的name
@QuickEasyUpsert
- 描述注解 进行数据源切换并将返回的数据保存(mysql配置多数据源进行数据源切换)
关于`QuickEasyUpsert`的说明:
QuickEasyUpsert继承了EasyUpsertDS支持自定义继承当前注解:
- 作用域方法上、类上,作用将方法返回数据直接存储
- MySQL下使用,支持MyBatis的动态数据源配置的多数据源,当自定义注入DataSource数据源名称为
@Bean(name = "dataSourceMySQL")中的name
核心功能
iUpsert.upsert();
拓展
架构设计