简体   繁体   中英

Reading CSV file using Apache Commons

I want to retrieve CSV file data from two files but in Nested format

Here is three files containing server information and getting it into 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();    
        }

Before Downvoting please listen that I need to update my server information using calculations then update hashmap In simple words in this code the second For-Each Loop Iterate only once rather it should iterate for each value of first one.

A distinct non-answer: you are looking at this from the wrong angle. This isn't a CSV problem.

You should simply read both files into memory, using an appropriate data structure, like a HashMap.

Then, afterwards, you take the two maps and "join" them.

You absolutely do not want to "loop" file reading based in the content of some other file.

The other part here is to define nice little classes representing the content of each "table" (with helpful getter methods), so that you can use things like Map<BookId, BorrowerStatus> and Map<BookId, BookInformation> .

You really should not mix up gathering of information with processing it. Of course, when your files have many millions of rows, then line-by-line processing might be necessary, but well: then you have for sure a scalability issue, as you absolutely never ever loop over millions of lines times millions of lines .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM