我想从两个文件中检索 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 循环只迭代一次,而不是它应该为第一个的每个值迭代。