数据处理,匹配 - 批量多文件匹配,文件选择性去重,选择性添加 - Java代码实现 - Java

Dataset描述:数据集按照某一列的数据选择性的添加和删除。 选择条件存在另一数据集中。通过遍历比对,重写原文件。

文件命名:原文件 master.*; 比对文件 compare.*;

文件格式:以逗号分隔。如果不是以逗号分隔,可以使用excel 分隔列工具帮助分隔原数据

比对选择条件: 对于一个master中特定比对对象,

  1.     如果对象的比对列在compare中存在,则continue,
  2.     如果不存在,插入compare中的数值,
  3.     如果存在且不是compare中的数据,删除该对象

举例: master:                    匹配条件            compare:         筛选项:                     result:

            01, a, b, 0203                  a-a                a, 0203            0203-0203                  01, a, b, 0203

            01, a, b, 0204                  a-a                a, 0206            0204 x                        01, a, b, 0206    

            01, a, b, 0205                  a-a                                        0205 x                         

            02, a, b, 0206                  a-a                                        0206-0206                  02, a, b, 0206

                                                                                                 0203 <--                      02, a, b, 0203

            03, c, b, 0206                  c-a x  

实现思想:

  1. 将数据分组, 01+a+b 可形成一个unique 组号
  2. 将 compare 文件数据 存在 cmp_map 中
  3. 将每一组的数据存在 master_map 中
  4. 将master_map 中将匹配的所有筛选项存在 match_map中
  5. 在一个分组结尾,比对 master_map 和 match_map 生成 record_map (存储在两个文件中都出现的筛选项)
  6. 比对 record_map 和 match_map 获取所需要插入master 文件的新筛选项
  7. 在操作完成之后清除 master_map, 使用 hashMap.clear(); 或在每个group 开头新建一个master_map
  8. 如果需要批量处理,在最外层添加文件和文件夹遍历,添加在 2,3 之间
  9. 如果对每个文件进行修改,最后使用 .bat 整合成一个完整的csv 文件


Shell 合并文件

https://blog.youkuaiyun.com/sinat_26933727/article/details/80710914

GitHub 完整代码下载:

https://github.com/Roundlet/Data-processing


部分功能代码块展示:

2. 将compare 数据讯在cmp_map 中

/*Set up the cmp_map*/

			/*<Test Id, Readline>*/

			Map<Integer,String> cmp_map = new HashMap<Integer,String>();

			while((readline_cmp = br_cmp.readLine())!=null) {

				String[] array = readline_cmp.split(",");

				int id = Integer.parseInt(array[0]);

				if(!cmp_map.containsKey(id)) {

					cmp_map.put(id, readline_cmp);

				}

			}

3. 将每一组的数据存在 master_map 中

/*The bound condition is that there are only one member in the group*/  
                    /*Loop in one group. 
                     *Loop will stop at the first element in the next group*/  
                    while((readline_master = br.readLine())!=null) {  
                        String cur_key = "";  
                        attr = readline_master.split(",");  
                        /*Get the current key*/  
                        for(int i=0;i<18;i++) {  
                            cur_key = cur_key + attr[i]+",";  
                        }  
                        /*Using keys to identify whether they belong to the 
                         * same group*/  
                        if(!cur_key.equals(pre_key)) {  
                            break;  
                        }  
                        master_map.put(readline_master, Integer.parseInt(attr[22]));      
                    }  

4. 将master_map 中将匹配的所有筛选项存在 match_map中


/*Set up the map that contains all the compare code (Test ID) that match the 
                     * dir name in master file use the parameter dir_master*/  
                    Map<String,Integer> match_map = new HashMap<String,Integer>();  
                    for(Map.Entry<Integer, String> entry :cmp_map.entrySet()) {  
                        attr = entry.getValue().split(",");  
                        String dir_cmp = attr[6];  
                        if(dir_master.equals(dir_cmp)) {  
                            String cmp_line = pre_key+attr[1]+","+","+","+","+entry.getValue();  
                            match_map.put(cmp_line, Integer.parseInt(attr[0]));  
                        }  
                          
                    }  

5. 在一个分组结尾,比对 master_map 和 match_map 生成 record_map (存储在两个文件中都出现的筛选项)

/*Record the code that existing in both master and match map*/  
                    Map<String,Integer> record_map = new HashMap<String,Integer>();  
                    for(Map.Entry<String,Integer> entry_master : master_map.entrySet()) {  
                        for(Map.Entry<String,Integer> entry_match : match_map.entrySet()) {  
                            if(entry_master.getValue().equals(entry_match.getValue() )) {  
                                record_map.put(entry_master.getKey(),entry_master.getValue());  
                                writer.write(entry_master.getKey());  
                                writer.newLine();  
                            }  
                        }  
                    }  
6.  比对 record_map 和 match_map 获取所需要插入master 文件的新筛选项
/*Find the element we need to insert*/  
                    if(record_map.isEmpty()) {  
                        for(Map.Entry<String,Integer> entry_match : match_map.entrySet()) {  
                            writer.write(entry_match.getKey());  
                            writer.newLine();  
                        }  
                    }  
                    else {  
                        for(Map.Entry<String,Integer> entry_match : match_map.entrySet()) {  
                            for(Map.Entry<String,Integer> entry_record : record_map.entrySet()) {  
                                if(!entry_match.getValue().equals(entry_record.getValue()) ) {  
                                    writer.write(entry_match.getKey());  
                                    writer.newLine();  
                                }  
                            }  
                        }  
                    }  
                      
                }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值