首页 > 解决方案 > 使用 Java 中的 Stream 迭代后未获得预期的列表大小

问题描述

在下面的方法中,我从 SAP HANA 中获取数据,在其中我使用映射来保存列表对象,tbl_guid作为键,dRListMap进一步传递给另一个方法fetchUniquForTblGuid执行。

public static void gdprDeleteReqStatus() {
            LOGGER.info("Fetching the records GDPR_DEL_REQ_STATUS  in HANA");
            String dbName = hanaProp.getProperty("database");
            int mysqlMergeLimit=Integer.parseInt(hanaProp.getProperty("mysql.limit"));
            String sql = String.format("select * from %s .GDPR_DEL_REQ_STATUS ", dbName);
            Map<String, List<DeletedRecord>> dRListMap = new HashMap<>();
            CommonService commonObject=new CommonService();
            try (Statement stmt = hanaConnection.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
                 int i=0;
                 //Thread.sleep(10000);
                    while ((rs.next())) {
                        DeletedRecord delRecord = new DeletedRecord(rs.getString(1), rs.getString(2), rs.getString(3),
                                rs.getString(4), rs.getDate(5));

                        String key = rs.getString(2);
                        List<DeletedRecord> recordList = dRListMap.get(key) == null ? new ArrayList<>() : dRListMap.get(key);

                        recordList.add(delRecord);
                        dRListMap.put(key, recordList);
                        i++;
                        if (i ==mysqlMergeLimit) {
                            //LOGGER.info(String.format("HANA batch size %s and records %s ",i,dRListMap.toString()));
                            LOGGER.info("List Size "+dRListMap.values().size());
                            commonObject.fetchUniquForTblGuid(dRListMap);
                            dRListMap.clear();

                            i=0;

                        }

                    }if(i>0) {
                        //LOGGER.info(String.format("HANA batch size %s and records %s ",i,dRListMap.toString()));
                        commonObject.fetchUniquForTblGuid(dRListMap);
                    }


            } catch (Exception ee) {
                LOGGER.error("Exception occurred while fetching the records from GDPR_DEL_REQ_STATUS", ee);
            }


            }

一个问题陈述Map 将和的reqHistMap组合保存为 key 和 value ,同时迭代List 的形成, 如果 key 已经存在,则需要将其添加到相同的列表中,因此需要修改相同的列表打印此地图时发现了问题,每个列表的预期大小为 10,但我发现它们也小于 10。DateReq_id#List<String>dRLskeyDatereq_idreqHistMaptbl_guidreqHistMap

   public  void fetchUniquForTblGuid(Map<String, List<DeletedRecord>> dRListMap) {
            LOGGER.info("Fetching the unique seq for each recieved quid from delete_status_table");
            List<List<DeletedRecord>> valueList = Collections.list(Collections.enumeration(dRListMap.values()));
            List<String> values = valueList.stream().flatMap(Collection::stream).map(DeletedRecord::getTblGuid)
                    .collect(Collectors.toCollection(ArrayList::new));

            Map<String, String> tblSeqMap =new HashMap<>();
            tblSeqMap.put("TRNFRM_ECC_CCM.DWEPLOY_LOOKUP", "1");
            tblSeqMap.put("REPLICN_DYLAN.BILLING_INFO", "3");
            tblSeqMap.put("TRNFRM_SUBSCRPN.SUBSCRIPTION_FILTER", "2");
            tblSeqMap.put("REPLICN_ETS.STAGE_USER_LVT_PROFILE_PARSED","4");

                    //getTheUniqueNoForGuids(values);
            // System.out.println(dRListMap);

            for (Map.Entry<String, String> map : tblSeqMap.entrySet()) {

                if (dRListMap.containsKey(map.getKey())) {

                    dRListMap.get(map.getKey()).forEach((DeletedRecord del) -> del.setTblGuid(map.getValue()));
                }
            }
            // System.out.println(dRListMap);

            List<List<DeletedRecord>> withUpdatedGuid = Collections.list(Collections.enumeration(dRListMap.values()));
            List<DeletedRecord> dRLs = withUpdatedGuid.stream().flatMap(Collection::stream)
                    .collect(Collectors.toCollection(ArrayList::new));
            Map<String, List<String>> reqHistMap = new HashMap<>();
            dRLs.parallelStream().forEach(deleteRecord -> {

                String key = String.format("%s#%s", deleteRecord.getReqDts(), deleteRecord.getReqId());

                List<String> value = reqHistMap.get(key) == null ? new ArrayList<>() : reqHistMap.get(key);
                value.add(deleteRecord.getTblGuid());

                reqHistMap.put(key, value);

            });
            List<RequestTableMapping> finalList = reqHistMap.entrySet().parallelStream().map(entry -> {
                String[] key = entry.getKey().split("#");
                return new RequestTableMapping(key[1], key[0], entry.getValue());

            }).collect(Collectors.toCollection(ArrayList::new));

            HbaseDao hDao=new HbaseDao();

              finalList.stream().forEach(x->{

              LOGGER.info(String.format("Request id %s and no. of guid's %s",x.getRequestId
              (),x.getTableGuidSmallMapping().size()));

              });


    //      hDao.insertRecords(finalList, true);
            //System.out.println(finalList);

            reqHistMap.clear();


        }

这是 POJO 需要保存在 Hbase 中

public class RequestTableMapping {

        public String requestId;
        public String date;
        List<String> tableGuidSmallMapping;


        public RequestTableMapping() {
            super();
        }
        public RequestTableMapping(String requestId, String date, List<String> tableGuidSmallMapping) {
            super();
            this.requestId = requestId;
            this.date = date;
            this.tableGuidSmallMapping = tableGuidSmallMapping;
        }
        public String getRequestId() {
            return requestId;
        }
        public void setRequestId(String requestId) {
            this.requestId = requestId;
        }
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }
        public List<String> getTableGuidSmallMapping() {
            return tableGuidSmallMapping;
        }
        public void setTableGuidSmallMapping(List<String> tableGuidSmallMapping) {
            this.tableGuidSmallMapping = tableGuidSmallMapping;
        }
        @Override
        public String toString() {
            return "RequestTableMapping {requestId:" + requestId + ", date:" + date + ", tableGuidSmallMapping:"
                    + tableGuidSmallMapping + "}";
        }   

    }

输出:

2020-05-18 17:56:18 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:56:18 INFO  HanaService:54 - List Size 10
2020-05-18 17:56:18 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:56:18 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of `guid's 9`
2020-05-18 17:56:18 INFO  HanaService:54 - List Size 10
2020-05-18 17:56:18 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:56:18 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of `guid's 7`
2020-05-18 17:56:18 INFO  HanaService:54 - List Size 10
2020-05-18 17:56:18 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:56:18 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:56:18 INFO  HanaService:54 - List Size 10

预期的:

2020-05-18 17:52:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:42 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:42 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-18 17:52:43 INFO  HanaService:54 - List Size 10
2020-05-18 17:52:43 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-18 17:52:43 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10

标签: javastreamhana

解决方案


我能够找出上述程序中的问题,所以基本上我使用并行流来处理dRLs列表,因为该元素没有按顺序处理,并且我在reqHistMapas 值中得到了不同大小的列表。我希望这对其他人也有帮助。

dRLs.stream().forEach(deleteRecord -> {

                String key = String.format("%s#%s", deleteRecord.getReqDts(), deleteRecord.getReqId());

                List<String> value = reqHistMap.get(key) == null ? new ArrayList<>() : reqHistMap.get(key);
                value.add(deleteRecord.getTblGuid());

                reqHistMap.put(key, value);

            }); 

预期输出:

2020-05-21 13:21:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:42 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:42 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:42 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:42 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10
2020-05-21 13:21:22 INFO  HanaService:54 - List Size 10
2020-05-21 13:21:22 INFO  CommonService:37 - Fetching the unique seq for each recieved quid from delete_status_table
2020-05-21 13:21:22 INFO  CommonService:84 - Request id 11E8D1EE51D64AB598CACF259031C1DF and no. of guid's 10

推荐阅读