diff --git a/src/main/java/com/baiyee/adcallback/service/DslBaseService.java b/src/main/java/com/baiyee/adcallback/service/DslBaseService.java index 14b8c64..da58a23 100644 --- a/src/main/java/com/baiyee/adcallback/service/DslBaseService.java +++ b/src/main/java/com/baiyee/adcallback/service/DslBaseService.java @@ -30,7 +30,7 @@ public interface DslBaseService { * @param num * @return */ - Integer deleteDateByDateAndLimitNum(DateTime date, Integer num); + Long deleteDateByDateAndLimitNum(DateTime date, Integer num); /** * 查询数量 diff --git a/src/main/java/com/baiyee/adcallback/service/impl/BdBackDataServiceImpl.java b/src/main/java/com/baiyee/adcallback/service/impl/BdBackDataServiceImpl.java index 26fc995..d266be5 100644 --- a/src/main/java/com/baiyee/adcallback/service/impl/BdBackDataServiceImpl.java +++ b/src/main/java/com/baiyee/adcallback/service/impl/BdBackDataServiceImpl.java @@ -23,6 +23,7 @@ import com.querydsl.sql.SQLExpressions; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.sql.Timestamp; import java.util.Date; import java.util.List; @@ -34,6 +35,7 @@ import java.util.List; @Service @RequiredArgsConstructor public class BdBackDataServiceImpl extends AbstractService implements BdBackDateService { + QTbBdNewBackdataEntity bdNewBackdata = QTbBdNewBackdataEntity.tbBdNewBackdataEntity; @Override public List queryByTagAndTimeAndFlag (Date startTime, Date endTime, List tagList, Integer flag) { @@ -41,7 +43,7 @@ public class BdBackDataServiceImpl extends AbstractService { StringPath t3 = Expressions.stringPath("t3"); StringTemplate dateExpr; - QTbBdNewBackdataEntity bdNewBackdata = QTbBdNewBackdataEntity.tbBdNewBackdataEntity; + if (flag == 1) { dateExpr = Expressions.stringTemplate(SqlConstant.DATE_EXPR, bdNewBackdata.gmtCreate); SimpleTemplate t3Tag = Expressions.template(String.class, "t3.tag"); @@ -72,12 +74,13 @@ public class BdBackDataServiceImpl extends AbstractService implements JlV1BackDataService { + + @PersistenceContext + protected EntityManager entityManager; + QTbJLBackDataV1Entity v1Entity = QTbJLBackDataV1Entity.tbJLBackDataV1Entity; + @Override public List queryByTagAndTimeAndFlag (Date startTime, Date endTime, List tagList, Integer flag) { return this.applyJPA(query -> { StringPath t3 = Expressions.stringPath("t3"); StringTemplate dateExpr; - QTbJLBackDataV1Entity v1Entity = QTbJLBackDataV1Entity.tbJLBackDataV1Entity; + if (flag == 1) { dateExpr = Expressions.stringTemplate(SqlConstant.DATE_EXPR, v1Entity.gmtCreate); SimpleTemplate t3Tag = Expressions.template(String.class, "t3.tag"); @@ -48,9 +58,9 @@ public class JlV1BackDataServiceImpl extends AbstractService t3Date = Expressions.template(Date.class, "t3.gmtCreate"); // 封装子查询 SubQueryExpression gmtCreate = SQLExpressions.select - (v1Entity.imei, v1Entity.tag,v1Entity.gmtCreate.max().as(SqlConstant.CREATE_TIME)).from + (v1Entity.imei, v1Entity.tag, v1Entity.gmtCreate.max().as(SqlConstant.CREATE_TIME)).from (v1Entity).where(v1Entity.tag.in(tagList).and(v1Entity.imei.ne(CharSequenceUtil.EMPTY).and - (v1Entity.imei.ne(DataConstant.IMEI_NULL_TAG)).and(v1Entity.imei.ne(DataConstant.IMEI_TAG)))).groupBy(v1Entity.imei,v1Entity.tag); + (v1Entity.imei.ne(DataConstant.IMEI_NULL_TAG)).and(v1Entity.imei.ne(DataConstant.IMEI_TAG)))).groupBy(v1Entity.imei, v1Entity.tag); // 结果返回 List voList = query.select(Projections.bean @@ -64,31 +74,19 @@ public class JlV1BackDataServiceImpl extends AbstractService imeiVOList = query.select(Projections.bean (QueryImeiVO.class, v1Entity.imei, v1Entity.tag, dateExpr.as(SqlConstant.CREATE_TIME))).distinct().from(v1Entity).where - (v1Entity.tag.in(tagList).and(v1Entity.imei.ne(CharSequenceUtil.EMPTY).and(v1Entity.imei.ne(DataConstant.IMEI_NULL_TAG)).and(v1Entity.imei.ne - (DataConstant.IMEI_TAG)))).groupBy(v1Entity.imei,v1Entity.tag).fetch(); + (v1Entity.tag.in(tagList).and(v1Entity.imei.ne(CharSequenceUtil.EMPTY).and(v1Entity.imei.ne(DataConstant.IMEI_NULL_TAG)).and(v1Entity.imei.ne + (DataConstant.IMEI_TAG)))).groupBy(v1Entity.imei, v1Entity.tag).fetch(); return Convert.toList(DmpMonitorJpaDto.class, imeiVOList); }); } @Override - public Integer deleteDateByDateAndLimitNum(DateTime date, Integer num) { - - - //querydsl查询实体 - QTbJLBackDataV1Entity v1Entity = QTbJLBackDataV1Entity.tbJLBackDataV1Entity; - - this.newQuery() - //删除对象 - .delete(v1Entity) - //删除条件 - .where(v1Entity.gmtCreate.gt(date)) - //执行删除 - .execute(); - return null; + public Long deleteDateByDateAndLimitNum(DateTime date, Integer num) { + return this.newQuery().delete(v1Entity).where(v1Entity.gmtCreate.gt(date)).execute(); } @Override public Long countByDate(DateTime date) { - return null; + return 0L; } } diff --git a/src/main/java/com/baiyee/adcallback/service/impl/JlV2BackDataServiceImpl.java b/src/main/java/com/baiyee/adcallback/service/impl/JlV2BackDataServiceImpl.java index 9dd8bac..598051b 100644 --- a/src/main/java/com/baiyee/adcallback/service/impl/JlV2BackDataServiceImpl.java +++ b/src/main/java/com/baiyee/adcallback/service/impl/JlV2BackDataServiceImpl.java @@ -33,7 +33,7 @@ import java.util.List; @Service @RequiredArgsConstructor public class JlV2BackDataServiceImpl extends AbstractService implements JlV2BackDataService { - + QTbJLBackDataV2Entity v2Entity = QTbJLBackDataV2Entity.tbJLBackDataV2Entity; @Override public List queryByTagAndTimeAndFlag (Date startTime, Date endTime, List tagList, Integer flag) { @@ -41,7 +41,7 @@ public class JlV2BackDataServiceImpl extends AbstractService { StringPath t3 = Expressions.stringPath("t3"); StringTemplate dateExpr; - QTbJLBackDataV2Entity v2Entity = QTbJLBackDataV2Entity.tbJLBackDataV2Entity; + if (flag == 1) { dateExpr = Expressions.stringTemplate(SqlConstant.DATE_EXPR, v2Entity.gmtCreate); SimpleTemplate t3Tag = Expressions.template(String.class, "t3.tag"); @@ -72,12 +72,12 @@ public class JlV2BackDataServiceImpl extends AbstractService implements KsBackDateService { - + QTbKSNewBackdataEntity ksNewBackdata = QTbKSNewBackdataEntity.tbKSNewBackdataEntity; @Override public List queryByTagAndTimeAndFlag (Date startTime, Date endTime, List tagList, Integer flag) { @@ -42,7 +43,7 @@ public class KsBackDataServiceImpl extends AbstractService { StringPath t3 = Expressions.stringPath("t3"); StringTemplate dateExpr; - QTbKSNewBackdataEntity ksNewBackdata = QTbKSNewBackdataEntity.tbKSNewBackdataEntity; + if (flag == 1) { dateExpr = Expressions.stringTemplate(SqlConstant.DATE_EXPR, ksNewBackdata.gmtCreate); SimpleTemplate t3Tag = Expressions.template(String.class, "t3.tag"); @@ -73,12 +74,13 @@ public class KsBackDataServiceImpl extends AbstractService implements UcBackDateService { - + QTbUcBackdataEntity ucBackdata = QTbUcBackdataEntity.tbUcBackdataEntity; @Override public List queryByTagAndTimeAndFlag (Date startTime, Date endTime, List tagList, Integer flag) { @@ -42,7 +43,7 @@ public class UcBackDataServiceImpl extends AbstractService { StringPath t3 = Expressions.stringPath("t3"); StringTemplate dateExpr; - QTbUcBackdataEntity ucBackdata = QTbUcBackdataEntity.tbUcBackdataEntity; + if (flag == 1) { dateExpr = Expressions.stringTemplate(SqlConstant.DATE_EXPR, ucBackdata.gmtCreate); SimpleTemplate t3Tag = Expressions.template(String.class, "t3.tag"); @@ -73,12 +74,13 @@ public class UcBackDataServiceImpl extends AbstractService 0); - log.info("======== delete tag{} table size as [{}] ========", dslService, totalDeleted); + Long executeUpdate = dslService.deleteDateByDateAndLimitNum(date, 50000); + log.info("======== delete service {} table size as [{}] ========", dslService, executeUpdate); } } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index b8c15ef..a9b1249 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -74,36 +74,42 @@ spring: props: # 分片算法的行表达式(算法自行定义,此处为方便演示效果) algorithm-expression: db$->{id % 2} + allow-range-query-with-inline-sharding: true jl-v1-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_jl_v1_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true jl-v2-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_jl_v2_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true bd-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_bd_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true gdt-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_gdt_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true ks-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_ks_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true # 分布式序列算法配置 key-generators: snowflake: @@ -222,7 +228,7 @@ spring: sql-show: true jpa: - show-sql: false + show-sql: true platform: authToken: JI8AeA7POKsdGcBC diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 81dad73..26c9381 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -72,36 +72,42 @@ spring: props: # 分片算法的行表达式(算法自行定义,此处为方便演示效果) algorithm-expression: db$->{id % 2} + allow-range-query-with-inline-sharding: true jl-v1-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_jl_v1_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true jl-v2-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_jl_v2_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true bd-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_bd_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true gdt-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_gdt_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true ks-table-inline: # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/ type: INLINE props: # 分片算法的行表达式 algorithm-expression: tb_ks_new_backdata_$->{(tag.hashCode() & Integer.MAX_VALUE) % 16} + allow-range-query-with-inline-sharding: true # 分布式序列算法配置 key-generators: snowflake: diff --git a/src/test/java/com/baiyee/adcallback/DeleteTest.java b/src/test/java/com/baiyee/adcallback/DeleteTest.java new file mode 100644 index 0000000..fa87489 --- /dev/null +++ b/src/test/java/com/baiyee/adcallback/DeleteTest.java @@ -0,0 +1,38 @@ +package com.baiyee.adcallback; + +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import com.baiyee.adcallback.service.JlV1BackDataService; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; + +/** + * @author Enzo + * @date : 2023/7/4 + */ + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest(classes = AdCallbackApplication.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class DeleteTest { + + + @Resource + private JlV1BackDataService jlV1BackDataService; + + @Test + @Transactional(rollbackFor = Exception.class) + public void test1() { + + DateTime dateTime = DateUtil.offsetMonth(DateUtil.date(), -1); + jlV1BackDataService.deleteDateByDateAndLimitNum(dateTime, 5000); + + } +} diff --git a/src/test/java/com/baiyee/adcallback/TagTest.java b/src/test/java/com/baiyee/adcallback/TagTest.java index 527c5b4..fc4f4b1 100644 --- a/src/test/java/com/baiyee/adcallback/TagTest.java +++ b/src/test/java/com/baiyee/adcallback/TagTest.java @@ -9,6 +9,7 @@ import com.baiyee.adcallback.api.vo.QueryQuantityVO; import com.baiyee.adcallback.repository.TbJLV1BackdataEntityRepository; import com.baiyee.adcallback.repository.entity.QTbJLBackDataV1Entity; import com.baiyee.adcallback.repository.entity.TbJLBackDataV1Entity; +import com.baiyee.adcallback.service.JlV1BackDataService; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.querydsl.core.Tuple; @@ -53,6 +54,10 @@ public class TagTest { private EntityManager entityManager; + @Resource + private JlV1BackDataService jlV1BackDataService; + + @Resource private TbJLV1BackdataEntityRepository jlv1BackdataEntityRepository; @@ -90,7 +95,7 @@ public class TagTest { List num = jpasqlQuery.select(Projections.bean(QueryQuantityVO.class, tbJLBackDataV1Entity.imei.count().as("num"), tbJLBackDataV1Entity.tag, dateExpr.as("gmtCreate"))).from(tbJLBackDataV1Entity).innerJoin(gmtCreate, t3).on(tbJLBackDataV1Entity.imei.eq - (t3Imei).and(tbJLBackDataV1Entity.gmtCreate.eq(t3Date))).groupBy(dateExpr ,tbJLBackDataV1Entity.tag).fetch(); + (t3Imei).and(tbJLBackDataV1Entity.gmtCreate.eq(t3Date))).groupBy(dateExpr, tbJLBackDataV1Entity.tag).fetch(); System.out.println(num);