-1

我想从两个文件中检索 CSV 文件数据,但采用嵌套格式

这是三个包含服务器信息并将其放入 hashmap 的文件

 List<Server> list=new ArrayList<Server>();
    List<Server> list1=new ArrayList<Server>();
    List<Server> list2=new ArrayList<Server>();

    HashMap<String,Server> map=new HashMap<String,Server>();
    HashMap<String,Server> map1=new HashMap<String,Server>();
    try {
        ipReader = new FileReader(SAMPLE_CSV_FILE_PATH);   //Reading 1_IP_spec.csv
        Iterable<CSVRecord> csvRecord = CSVFormat.DEFAULT  //
                  .withFirstRecordAsHeader()
                  .parse(ipReader);

        hardwareReader=new FileReader(SAMPLE_CSV_FILE_PATH01);  //Reading 2_Hardware_spec.csv
        Iterable<CSVRecord> csvRecord01 = CSVFormat.DEFAULT
                  .withFirstRecordAsHeader()
                  .parse(hardwareReader);

        utilizationReader=new FileReader(SAMPLE_CSV_FILE_PATH02); //Reading 4_Utilization.csv
        Iterable<CSVRecord> csvRecord02 = CSVFormat.DEFAULT
                  .withFirstRecordAsHeader()
                  .parse(utilizationReader);

        for(CSVRecord record: csvRecord) {
            Server server=new Server();
            String IPServer=record.get("NODEIP");
            String serverName=record.get("NODENAME");
            server.setIp_Address(IPServer);
            server.setServerName(serverName);
            map.put(IPServer, server);
        }
        for(CSVRecord record01: csvRecord01) {
            Server server=new Server();
            String IPServer=record01.get("NODEIP");
            String osName=record01.get("OSNAME");
            String osVersion=record01.get("OSVERSION");
            String osArchitecture=record01.get("OS_ARCHITECTURE");
            String hdCapacity=record01.get("HARDDISK_CAPACITY");
            String ramCapacity=record01.get("RAM_CAPACITY");
            server.setIp_Address(IPServer);
            server.setOsName(osName);
            server.setOsArchitecture(osArchitecture);
            server.setOsVersion(osVersion);
            server.setHardDiskCapacity(hdCapacity);
            server.setRamCapacity(ramCapacity);
            list1.add(server);
            map1.put(IPServer, server);
        }
        map.putAll(map1);

        for(CSVRecord record02: csvRecord02) {
            Server server=new Server();
            server.setIp_Address(record02.get("NODEIP"));
            server.setAvgNetWorkUtilizationSent(record02.get("NETWORK_UTILIZATION_SENT"));
            server.setAvgNetworkUtilizationReceived(record02.get("NETWORK_UTILIZATION_RECEIVE"));
            server.setAvgCPUtilization(record02.get("CPU_UTILIZATION"));
            server.setAvgRamUtilization(record02.get("RAM_UTILIZATION"));
            list.add(server);
        }

         Set<Map.Entry<String, Server>> set = map.entrySet();
            int count=0;
            for (Map.Entry<String, Server> me : set) {
              String IPaddress=me.getKey();

                int netUtilSentCount=0; // counting same IP for Network Utilization Sent for calculating Average
                int netUtilReceivedCount=0; // counting same IP for Network Utilization Received for calculating Average
                int ramUtilCount=0; // counting same IP for Ram Utilization for calculating Average
                int cpuUtilCount=0; // counting same IP for Cpu Utilization for calculating Average
                double avgNetworkUtilizationSent=0;
                double avgNetworkUtilizationReceived=0;
                double avgRamUtilization=0;
                double avgCpuUtilization=0;


              for(int i=0;i<list.size();i++) {
                  if(IPaddress.equals(list.get(i).getIp_Address())) {
                        String networkUtilizationSent=list.get(i).getAvgNetWorkUtilizationSent();
                        String networkUtilizationReceived=list.get(i).getAvgNetworkUtilizationReceived();
                        String ramUtilization=list.get(i).getAvgRamUtilization();
                        String cpuUtilization=list.get(i).getAvgCPUtilization();
                    // Converting cell value in Digits(with Decimal) only using regex then Parsing into Integer
                        if(!networkUtilizationSent.trim().equals("")) {
                        avgNetworkUtilizationSent=avgNetworkUtilizationSent+Double.parseDouble(networkUtilizationSent.replaceAll("[^\\d.]",""));
                        netUtilSentCount++;
                        }
                        if(!networkUtilizationReceived.trim().equals("")) {
                        avgNetworkUtilizationReceived=avgNetworkUtilizationReceived+Double.parseDouble(networkUtilizationReceived.replaceAll("[^\\d.]",""));
                        netUtilReceivedCount++;
                        }
                        if(!ramUtilization.trim().equals("")) {
                        avgRamUtilization=avgRamUtilization+Double.parseDouble(ramUtilization.replaceAll("[^\\d.]",""));                                        
                        ramUtilCount++;
                        }
                        if(!cpuUtilization.trim().equals("")) {
                        avgCpuUtilization=avgCpuUtilization+Double.parseDouble(cpuUtilization.replaceAll("[^\\d.]",""));
                        cpuUtilCount++;
                        }
                        /*if(!ramUtilization.trim().equals("")) {
                              System.out.println(avgRamUtilization);
                          }*/
                  }

              }
              System.out.println("For Ip "+IPaddress+" Average utilization "+avgRamUtilization);
            }
            //System.out.println(count);


        }catch(IOException e) {
        e.printStackTrace();    
        }

在 Downvoting 之前,请听我需要使用计算更新我的服务器信息,然后更新 hashmap 用简单的话来说,在这段代码中,第二个 For-Each 循环只迭代一次,而不是它应该为第一个的每个值迭代。

4

1 回答 1

3

一个明显的非答案:您从错误的角度看待这个问题。这不是 CSV 问题。

您应该使用适当的数据结构(如 HashMap)简单地将这两个文件读入内存。

然后,之后,你拿两张地图并“加入”它们。

您绝对不想根据其他文件的内容“循环”文件读取。

这里的另一部分是定义漂亮的小类来表示每个“表”的内容(使用有用的 getter 方法),以便您可以使用Map<BookId, BorrowerStatus>和之类的东西Map<BookId, BookInformation>

你真的不应该把收集信息和处理信息混为一谈。当然,当您的文件有数百万行时,可能需要逐行处理,但好吧:那么您肯定会遇到可伸缩性问题,因为您绝对永远不会循环超过百万行 乘以 数百万行

于 2018-08-03T10:39:39.800 回答