目录
- 事件起因
- 环境和工具
- 解决办法
- 结束语
事件起因
在写一个市级的项目时,遇到了一个问题,这个项目涉及的数据内容非常大,光是数据库文件的大小就已经达到了12G,数据的规模大致是在百万级的,光是我这次参与处理的数据就有10w(最后我跑出来的数据是10w,但处理的数据不止如此)
本次运行的数据库,因为我的盘大小不够,我还额外装了一块2T硬盘
在处理这个项目的数据的过程中有一个 将数据存在原来多个表的同一个物料的信息汇总起来然后放入我所要进行存储的一张表中去,大致过程是:物料有唯一的sn码,然后每个阶段物料都有进行流转会产生业务流程的数据,这个数据分别在不同的表中,比如说物料入库,出库,入下级仓库,出下级仓库,入暂存点,出暂存点和安装,每个阶段都会产生业务数据信息,然后我要将每个阶段的信息进行汇总,(每个阶段的信息都在不同的数据库表中,单表的数据量都很大,上10w级)此时使用连表在数据库中去执行这样的效率比较低下,所以采用的是在每个阶段去查询满足查询条件的当前阶段的数据,然后在java的代码中去处理这些数据,让相同的物料编码sn去将这些实体属性的信息合并,
下面是这个实体的属性的展示,这个实体目前是有130多个属性值:(各阶段分别占有10多个属性)
现在各个阶段都查到了对应的数据,比如说,入库阶段有入库sn和入库相关信息,出库阶段有出库sn和出库相关信息,现要做的是将各个阶段同一sn的所有信息合并
环境和工具
jdk1.8
maven
idea2023
解决办法
在处理这个情况的方法在初步就有了,比如说,双重循环这两个列表,去遍历每一个对象,然后判断这些对象的sn是否相同,但是这样已经匹配的sn对象也会继续拿到其他的对象中去匹配,效率比较低下
有两个对象列表 List snInfo1 和List snInfo2
现优化这种情况,将其中的一个列表对象修改为map,其中的属性sn作为key,对应的每个对象作为value,代码如下:
/*** 对象复制到对象 source是之前就有的列表对象*/private List<SnInfoExtend> copyListToListBySn(List<SnInfoExtend> source, List<SnInfoExtend> source2){// 创建一个 Map,将 sn 作为键,Market 对象作为值Map<String, SnInfoExtend> snMap = source.stream().collect(Collectors.toMap(SnInfoExtend::getSourceSn, market -> market, (existing, replacement) -> replacement));// 合并 snList2 中的数据到 snList 关于下面那儿为什么要用toUpperCase: 因为 前面的流程 sn全是大写字母,到了安装的时候却变为了小写字母source2.forEach(market -> snMap.merge(market.getSourceSn().toUpperCase().trim(), market, (existing, replacement) -> {//仓库出库,这个比较特殊 只能根据箱码进行更新 箱码的话,肯定是会重复的//入暂存点 8个字段更新if(replacement.getPointRecName() != null){existing.setPointRecName(replacement.getPointRecName());}if(replacement.getPointRecCompany() != null){existing.setPointRecCompany(replacement.getPointRecCompany());}if(replacement.getPointRecPhonePro() != null){existing.setPointRecPhonePro(replacement.getPointRecPhonePro());}if(replacement.getPointShipNamePro() != null){existing.setPointShipNamePro(replacement.getPointShipNamePro());}if(replacement.getPointrRecAddPro() != null){existing.setPointrRecAddPro(replacement.getPointrRecAddPro());}if(replacement.getRecBusinessTypePoint() != null){existing.setRecBusinessTypePoint(replacement.getRecBusinessTypePoint());}if(replacement.getRecTimePoint() != null){existing.setRecTimePoint(replacement.getRecTimePoint());existing.setUpdatedTime(replacement.getRecTimePoint());}if(replacement.getPointRecProblem() != null){existing.setPointRecProblem(replacement.getPointRecProblem());}//出暂存点和领料//出暂存点if(replacement.getPointShipName() != null){existing.setPointShipName(replacement.getPointShipName());}if(replacement.getPointShipCompany() != null){existing.setPointShipCompany(replacement.getPointShipCompany());}if(replacement.getPointShipPhonePro() != null){existing.setPointShipPhonePro(replacement.getPointShipPhonePro());}if(replacement.getShipTimePoint() != null){existing.setShipTimePoint(replacement.getShipTimePoint());existing.setUpdatedTime(replacement.getShipTimePoint());}if(replacement.getShipBusinessTypePoint() != null){existing.setShipBusinessTypePoint(replacement.getShipBusinessTypePoint());}//领料if(replacement.getTeamRecName() != null){existing.setTeamRecName(replacement.getTeamRecName());}if(replacement.getTeamRecPhonePro() != null){existing.setTeamRecPhonePro(replacement.getTeamRecPhonePro());}if(replacement.getRecTimeTeam() != null){existing.setRecTimeTeam(replacement.getRecTimeTeam());existing.setUpdatedTime(replacement.getRecTimeTeam());}if(replacement.getTeamRecNamePro() != null){existing.setTeamRecNamePro(replacement.getTeamRecNamePro());}if(replacement.getTeamRecCropping() != null){existing.setTeamRecCropping(replacement.getTeamRecCropping());}if(replacement.getRecBusinessTypeTeam() != null){existing.setRecBusinessTypeTeam(replacement.getRecBusinessTypeTeam());}//安装if(replacement.getTeamShipName() != null){existing.setTeamShipName(replacement.getTeamShipName());}if(replacement.getTeamShipPhonePro() != null){existing.setTeamShipPhonePro(replacement.getTeamShipPhonePro());}if(replacement.getTeamShipNamePro() != null){existing.setTeamShipNamePro(replacement.getTeamShipNamePro());}if(replacement.getShipTimeTeam() != null){existing.setShipTimeTeam(replacement.getShipTimeTeam());existing.setUpdatedTime(replacement.getShipTimeTeam());}if(replacement.getTeamShipAdd() != null){existing.setTeamShipAdd(replacement.getTeamShipAdd());}if(replacement.getTeamShipProblem() != null){existing.setTeamShipProblem(replacement.getTeamShipProblem());}if(replacement.getTransferTime() != null){existing.setTransferTime(replacement.getTransferTime());existing.setUpdatedTime(replacement.getTransferTime());}if(replacement.getShipBusinessTypeTeam() != null){existing.setShipBusinessTypeTeam(replacement.getShipBusinessTypeTeam());}return existing;}));// 最终的合并结果在 snList 中List<SnInfoExtend> mergedList = new ArrayList<>(snMap.values());return mergedList;}
上面已经将这个过程封装了为了一个方法,
该方法的效果为:传入两个列表对象,将第一个对象转换为一个map,有一点需要注意的地方,这儿会将snSource这个字段的字符串的字母转换为大写字母toUpperCase,且去除收尾的空格trim(),若不需要自行去代码里面去修改
结束语
若是对你有所帮助的话,希望能获得你的 点赞、评论、收藏,这将是对我很大的鼓励!!! 这对我真的很重要!!!
蟹蟹٩(‘ω’)و