gpt4 book ai didi

java - java读取行间数据

转载 作者:行者123 更新时间:2023-11-30 05:24:42 25 4
gpt4 key购买 nike

我有一个每 15 分钟收集一次 NMON 数据的进程。我对将内容读入 List 或仅使用 Java 8 Stream 有基本的了解。

我在迭代 ZZZZ,T0001.xxxxZZZZ,T000X,xxx 之间的数据时遇到困难

  1. 读取 ZZZZ,T0001,00:15:39,15-NOV-2019ZZZZ,T0002,00:30:39,15-NOV-2019 之间的数据>
  2. CPU_ALL,T0001,7.3,1.6,0.3,90.9,8.8,48 添加到 CPUData(实体)new CPUData('15-NOV-2019','00:15:39',7.3,1.6,0.3,90.9,8.8,48);
  3. 其他数据实体

重复直到找到最后一个 ZZZZ 条目。

如有任何建议,我们将不胜感激。

我当前的测试用例

public static void main(String[] args) {
String dirName = "c:\\sampleFiles";
String fileName = "c:\\sampleFiles\\test.nmon";
List<String> list = new ArrayList<>();
try {
Files.list(new File(dirName).toPath())
.forEach(path-> {
System.out.println(path);
BufferedReader reader;
FileReader fileReader;
try {
fileReader = new FileReader(path.toString());
reader = new BufferedReader(fileReader);
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}

以下是尝试迭代过程的行

try (Stream<String> stream = Files.lines(Paths.get(fileName))){
list = stream.filter(line -> line.startsWith("ZZZZ")).collect(Collectors.toList());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.forEach(System.out::println);

输出:

ZZZZ,T0001,00:15:39,15-NOV-2019
ZZZZ,T0002,00:30:39,15-NOV-2019
ZZZZ,T0003,00:45:39,15-NOV-2019
ZZZZ,T0004,01:00:39,15-NOV-2019
ZZZZ,T0005,01:15:39,15-NOV-2019
...
...

这是来自 NMON 文件的示例数据...

BBBP,514,netstat,"Route Tree for Protocol Family 24 (Internet v6):"
BBBP,515,netstat,"::1%1 ::1%1 UH 3 2275817 lo0 - - "
BBBP,516,emgr -l
ZZZZ,T0001,00:15:39,15-NOV-2019
CPU01,T0001,26.0,7.7,14.7,51.6
CPU02,T0001,0.5,0.2,0.1,99.2
CPU03,T0001,0.0,0.0,0.0,100.0
CPU04,T0001,0.0,0.0,0.0,100.0
CPU05,T0001,0.0,0.1,0.0,99.9
CPU06,T0001,0.0,0.1,0.0,99.9
CPU07,T0001,0.0,0.1,0.0,99.9
CPU08,T0001,0.0,0.0,0.0,100.0
CPU09,T0001,10.3,1.7,3.9,84.1
CPU10,T0001,0.1,2.4,0.0,97.5
CPU11,T0001,0.0,2.4,0.0,97.6
CPU12,T0001,0.0,2.4,0.0,97.6
CPU13,T0001,0.0,2.4,0.0,97.6
CPU14,T0001,0.0,2.4,0.0,97.6
CPU15,T0001,0.0,2.4,0.0,97.6
CPU16,T0001,0.0,2.4,0.0,97.6
CPU17,T0001,4.8,2.8,1.4,91.0
CPU18,T0001,0.0,2.9,0.0,97.1
CPU19,T0001,0.0,2.9,0.0,97.1
CPU20,T0001,0.0,2.9,0.0,97.1
CPU21,T0001,0.0,2.9,0.0,97.1
CPU22,T0001,0.0,2.9,0.0,97.1
CPU23,T0001,0.0,2.9,0.0,97.1
CPU24,T0001,0.0,2.9,0.0,97.1
CPU25,T0001,1.3,2.7,0.3,95.6
CPU26,T0001,0.0,3.0,0.0,97.0
CPU27,T0001,0.0,3.0,0.0,97.0
CPU28,T0001,0.0,3.0,0.0,97.0
CPU29,T0001,0.0,3.0,0.0,97.0
CPU30,T0001,0.0,3.0,0.0,97.0
CPU31,T0001,0.0,3.0,0.0,97.0
CPU32,T0001,0.0,3.0,0.0,97.0
CPU33,T0001,0.2,2.9,0.0,96.8
CPU34,T0001,0.0,3.0,0.0,97.0
CPU35,T0001,0.0,3.0,0.0,97.0
CPU36,T0001,0.0,3.0,0.0,97.0
CPU37,T0001,0.0,3.0,0.0,97.0
CPU38,T0001,0.0,3.0,0.0,97.0
CPU39,T0001,0.0,3.0,0.0,97.0
CPU40,T0001,0.0,3.0,0.0,97.0
CPU41,T0001,0.0,0.3,0.0,99.7
CPU42,T0001,0.0,3.0,0.0,97.0
CPU43,T0001,0.0,3.0,0.0,97.0
CPU44,T0001,0.0,3.0,0.0,97.0
CPU45,T0001,0.0,3.0,0.0,97.0
CPU46,T0001,0.0,3.0,0.0,97.0
CPU47,T0001,0.0,3.0,0.0,97.0
CPU48,T0001,0.0,3.0,0.0,97.0
CPU_ALL,T0001,7.3,1.6,0.3,90.9,8.8,48
BBBL,21,Flags,LPARed DRable SMT Shared UnCapped Migratable Not-Donating AMSable.
LPAR,T0001,0.554,6,48,16,2.00,128,0.00,3.46,3.46,1,0,7.26,1.56,0.06,18.81,2.42,0.52,0.02,6.27,0,0
POOLS,T0001,16,16.00,10.10,0.00,0.00,0.00,0.00,0,2.00
MEM,T0001,0.3,96.4,111.0,15797.4,36864.0,16384.0
MEMNEW,T0001,76.5,8.2,15.0,0.3,20.1,81.0
MEMUSE,T0001,8.2,3.0,90.0,960,1088,8.2,90.0, 9076000.0
PAGE,T0001,2200.6,1036.6,32.5,0.0,0.0,891.4,1940.5,0.0
MEMPAGES4KB,T0001,1556096,12859,774555,0,774555,682363,960,1088,0,0,682363,0,860874,501133,0,3655,37025,0,120731,1051308,1126800,0,146506,1047563,0,0,0,0,0,0,0,0,0,0,0,0,0
MEMPAGES64KB,T0001,492568,972,0,0,0,500238,60,68,0,0,491596,0,0,86831,0,0,7072,0,49256,405737,464431,0,79516,412066,0,0,0,0,0,0,0,0,0,0,0,0,0
MEMPAGES16MB,T0001,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MEMPAGES16GB,T0001,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
LARGEPAGE,T0001,0,0,0,0,16.0
PAGING,T0001,15798
PROC,T0001,7.11,0.21,2615,20384,2711,177,5,4,0,0,0,0,0
FILE,T0001,0,729,0,8601186,224748,0,0,0
NET,T0001,1.8,1.0,1.5,1.0
NETPACKET,T0001,7.3,6.1,6.4,6.1
NETSIZE,T0001,259.7,171.0,232.7,171.0
NETERROR,T0001,0.0,0.0,0.0,0.0,0.0,0.0
VGBUSY,T0001,6.2,0.2,0.2,0.0
VGREAD,T0001,3155.6,540.9,12.5,14.5
VGWRITE,T0001,220.4,107.3,28.3,0.1
VGSIZE,T0001,23.8,38.1,7.7,251.9
VGXFER,T0001,141.8,17.0,5.3,0.1
IOADAPT,T0001,0.0,0.0,0.0,3722.7,356.1,164.3
JFSFILE,T0001,18.8,20.5,94.6,52.2,79.0,0.2,84.5,0.1,83.4,70.8,55.7,0.0,0.0,47.5,14.9,55.2,91.0,95.9,95.8,80.5,21.1,21.1,12.2,6.2,0.6,3.9
JFSINODE,T0001,3.4,1.4,41.7,3.9,1.2,0.0,26.3,0.0,5.9,0.0,0.0,0.0,0.0,1.7,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
DISKBUSY,T0001,0.1,0.0,0.0,0.6,0.2,0.6,0.0,0.1,23.8,0.9,0.1,0.0
DISKREAD,T0001,0.1,0.0,0.3,488.6,12.5,12.4,0.0,39.6,2996.6,157.2,1.7,14.5
DISKWRITE,T0001,22.0,6.5,37.3,8.9,28.3,41.0,0.0,13.6,120.8,71.7,5.9,0.1
DISKXFER,T0001,3.6,1.6,0.5,9.6,5.3,3.9,0.0,1.3,122.7,14.8,0.7,0.1
DISKRXFER,T0001,0.0,0.0,0.0,7.8,1.1,1.1,0.0,0.4,108.5,10.6,0.2,0.0
DISKBSIZE,T0001,6.2,4.0,70.7,51.6,7.7,13.8,0.5,40.7,25.4,15.4,10.3,251.9
DISKRIO,T0001,0.0,0.0,0.0,7.8,1.1,1.1,0.0,0.4,108.5,10.6,0.2,0.0
DISKWIO,T0001,3.5,1.6,0.5,1.8,4.1,2.8,0.0,0.9,14.2,4.2,0.5,0.0
DISKAVGRIO,T0001,5.7,0.5,7.6,62.6,11.1,11.2,0.5,98.5,27.6,14.8,7.8,362.0
DISKAVGWIO,T0001,6.2,4.0,75.4,4.8,6.8,14.8,0.0,15.1,8.5,17.0,11.3,4.0
DISKSERV,T0001,0.4,0.4,0.9,1.0,0.4,1.9,0.4,0.9,2.3,0.7,2.0,1.6
DISKREADSERV,T0001,5.5,0.6,5.2,1.1,0.6,5.7,0.4,1.6,2.5,0.8,5.8,2.1
DISKWRITESERV,T0001,0.3,0.4,0.6,0.7,0.4,0.4,0.0,0.6,0.6,0.5,0.3,0.6
DISKWAIT,T0001,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
PROCAIO,T0001,144,0,0.0
TOP,19464642,T0001,2.08,1.48,0.60,7,48308,32624,23344,498928,0,0,BESClient,Unclassified
UARG,T0001,19464642, 1,BESClient, 7 , root, system,/opt/BESClient/bin/BESClient
TOP,5374162,T0001,0.22,0.00,0.22,17,632,8,632,29,0,0,syncd,Unclassified
UARG,T0001,5374162, 1,syncd , 17 , root, system,/usr/sbin/syncd 60
TOP,0262152,T0001,0.14,0.14,0.00,7,1024,0,1024,0,0,0,lrud,Unclassified
UARG,T0001,0262152, 0,lrud , 7 , root, system,lrud
ZZZZ,T0002,00:30:39,15-NOV-2019
CPU01,T0002,12.1,6.9,3.2,77.7
CPU02,T0002,0.5,0.2,0.1,99.3
CPU03,T0002,0.0,0.0,0.0,99.9
CPU04,T0002,0.0,0.0,0.0,100.0
CPU05,T0002,0.0,0.0,0.0,100.0
CPU06,T0002,0.0,0.0,0.0,100.0
CPU07,T0002,0.0,0.0,0.0,100.0
CPU08,T0002,0.0,0.0,0.0,100.0
CPU09,T0002,1.2,0.5,0.5,97.8
CPU10,T0002,0.0,2.8,0.0,97.2
CPU11,T0002,0.0,2.8,0.0,97.2
CPU12,T0002,0.0,2.8,0.0,97.2
CPU13,T0002,0.0,2.8,0.0,97.2
CPU14,T0002,0.0,2.8,0.0,97.2
CPU15,T0002,0.0,2.8,0.0,97.2
CPU16,T0002,0.0,2.8,0.0,97.2
CPU17,T0002,0.9,2.7,0.2,96.2
CPU18,T0002,0.0,2.9,0.0,97.0
CPU19,T0002,0.0,2.9,0.0,97.1
CPU20,T0002,0.0,2.9,0.0,97.1
CPU21,T0002,0.0,2.9,0.0,97.1
CPU22,T0002,0.0,2.9,0.0,97.1
CPU23,T0002,0.0,2.9,0.0,97.1
CPU24,T0002,0.0,2.9,0.0,97.1
CPU25,T0002,0.1,2.9,0.0,96.9
CPU26,T0002,0.0,3.0,0.0,97.0
CPU27,T0002,0.0,3.0,0.0,97.0
CPU28,T0002,0.0,3.0,0.0,97.0
CPU29,T0002,0.0,3.0,0.0,97.0
CPU30,T0002,0.0,3.0,0.0,97.0
CPU31,T0002,0.0,3.0,0.0,97.0
CPU32,T0002,0.0,3.0,0.0,97.0
CPU33,T0002,0.1,3.0,0.0,96.9
CPU34,T0002,0.0,3.0,0.0,97.0
CPU35,T0002,0.0,3.0,0.0,97.0
CPU36,T0002,0.0,3.0,0.0,97.0
CPU37,T0002,0.0,3.0,0.0,97.0
CPU38,T0002,0.0,3.0,0.0,97.0
CPU39,T0002,0.0,3.0,0.0,97.0
CPU40,T0002,0.0,3.0,0.0,97.0
CPU41,T0002,0.0,0.1,0.0,99.9
CPU42,T0002,0.0,3.0,0.0,97.0
CPU43,T0002,0.0,3.0,0.0,97.0
CPU44,T0002,0.0,3.0,0.0,97.0
CPU45,T0002,0.0,3.0,0.0,97.0
CPU46,T0002,0.0,3.0,0.0,97.0
CPU47,T0002,0.0,3.0,0.0,97.0
CPU48,T0002,0.0,3.0,0.0,97.0
CPU_ALL,T0002,2.6,1.2,0.2,96.0,3.8,48
LPAR,T0002,0.246,6,48,16,2.00,128,0.00,1.54,1.54,1,0,2.57,1.23,0.02,8.46,0.86,0.41,0.01,2.82,1,0
POOLS,T0002,16,16.00,10.10,0.00,0.00,0.00,0.00,0,2.00
MEM,T0002,0.2,96.4,86.5,15797.4,36864.0,16384.0
MEMNEW,T0002,76.3,8.4,15.0,0.2,20.1,81.0
MEMUSE,T0002,8.4,3.0,90.0,960,1088,8.4,90.0, 9076000.0
PAGE,T0002,1827.7,1082.1,52.8,0.0,0.0,1022.9,2594.5,0.0
MEMPAGES4KB,T0002,1573824,7508,796790,0,796790,683207,960,1088,0,0,683207,0,883109,501068,0,3655,37024,0,122504,1069101,1144416,0,146441,1070707,1439840,0,974002,47532,0,0,1021796,57526,821513,119,2335390,9,920746
MEMPAGES64KB,T0002,491460,915,0,0,0,499187,60,68,0,0,490545,0,0,87225,0,0,7072,0,49146,404235,463164,0,79516,411015,205314,0,0,0,0,0,0,0,44312,12889,0,0,0
MEMPAGES16MB,T0002,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MEMPAGES16GB,T0002,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
LARGEPAGE,T0002,0,0,0,0,16.0
PAGING,T0002,15798
PROC,T0002,4.92,0.06,2313,18824,525,163,3,2,0,0,0,0,1
FILE,T0002,0,795,0,5883500,271729,0,0,0
NET,T0002,0.6,1.2,1.2,1.2
NETPACKET,T0002,6.1,6.8,5.7,6.8
NETSIZE,T0002,106.6,181.4,212.3,181.4
NETERROR,T0002,0.0,0.0,0.0,0.0,0.0,0.0
VGBUSY,T0002,0.8,1.3,0.1,0.0
VGREAD,T0002,531.3,3756.3,13.2,0.0
VGWRITE,T0002,83.9,215.3,14.0,0.1
VGSIZE,T0002,21.2,71.2,6.1,4.0
VGXFER,T0002,29.1,55.8,4.5,0.0
IOADAPT,T0002,0.0,0.0,0.0,4300.2,313.4,89.3
JFSFILE,T0002,18.9,20.5,94.6,52.2,79.0,0.2,84.5,0.1,83.4,70.8,55.7,0.0,0.0,47.5,14.9,55.2,91.0,95.9,95.8,80.5,21.1,21.1,12.2,6.2,0.6,3.9
JFSINODE,T0002,3.4,1.4,41.7,3.9,1.2,0.0,26.3,0.0,5.9,0.0,0.0,0.0,0.0,1.7,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
DISKBUSY,T0002,0.0,0.0,0.8,4.8,0.1,0.8,0.0,1.5,2.9,0.3,0.0,0.0
DISKREAD,T0002,0.0,0.0,747.3,2358.8,13.2,639.8,2.3,8.1,399.7,131.4,0.2,0.0
DISKWRITE,T0002,4.6,7.2,165.5,7.7,14.0,21.9,0.0,13.0,36.0,42.8,0.5,0.1
DISKXFER,T0002,1.1,1.8,10.8,31.4,4.5,9.1,0.0,2.8,16.1,11.8,0.1,0.0
DISKRXFER,T0002,0.0,0.0,9.4,29.6,1.5,7.1,0.0,1.9,12.1,8.6,0.0,0.0
DISKBSIZE,T0002,4.2,4.0,84.6,75.5,6.1,72.9,228.0,7.6,27.0,14.8,8.1,4.0
DISKRIO,T0002,0.0,0.0,9.4,29.6,1.5,7.1,0.0,1.9,12.1,8.6,0.0,0.0
DISKWIO,T0002,1.1,1.8,1.4,1.7,2.9,2.0,0.0,0.9,4.0,3.2,0.1,0.0
DISKAVGRIO,T0002,4.0,0.0,79.5,79.6,8.6,89.9,228.0,4.3,33.0,15.3,12.0,0.0
DISKAVGWIO,T0002,4.2,4.0,119.3,4.5,4.8,11.2,0.0,15.2,8.9,13.4,7.1,4.0
DISKSERV,T0002,0.3,0.3,1.9,3.2,0.4,1.9,18.7,5.6,2.4,0.4,0.8,0.3
DISKREADSERV,T0002,9.5,0.0,2.0,3.3,0.4,2.3,18.7,7.9,3.0,0.4,2.6,0.0
DISKWRITESERV,T0002,0.3,0.3,0.7,0.7,0.3,0.4,0.0,0.6,0.6,0.4,0.4,0.3
DISKWAIT,T0002,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
PROCAIO,T0002,144,0,0.2
TOP,19464642,T0002,2.06,1.50,0.56,7,48308,32624,23344,510941,0,0,BESClient,Unclassified
TOP,54722870,T0002,0.19,0.17,0.02,1,421976,270976,24988,165669,1,0,oracle,Unclassified
UARG,T0002,54722870, 1,oracle , 1 ,oracle11,oinstall,oracleFIN91DMO (LOCAL=NO)
TOP,0262152,T0002,0.17,0.17,0.00,7,1024,0,1024,0,0,0,lrud,Unclassified
TOP,5374162,T0002,0.15,0.00,0.15,17,632,8,632,29,0,0,syncd,Unclassified
TOP,18153496,T0002,0.15,0.13,0.02,1,421528,270976,24540,169140,1,0,oracle,Unclassified
UARG,T0002,18153496, 1,oracle , 1 ,oracle11,oinstall,oracleFIN91DEV (LOCAL=NO)
ZZZZ,T0003,00:45:39,15-NOV-2019
CPU01,T0003,23.8,6.7,15.3,54.3
CPU02,T0003,0.7,0.3,0.1,99.0
CPU03,T0003,0.0,0.0,0.0,100.0
CPU04,T0003,0.0,0.0,0.0,100.0
CPU05,T0003,0.0,0.0,0.0,100.0

最佳答案

如果我理解正确的话,您所需要的只是以 "CPU_ALL" 开头的行。如果您可以向 CPUData 类添加一个新的构造函数,则相对简单。

首先添加一个接受字符串的构造函数:

public CPUData (String line){
String[] data = line.split(",");
this.date = LocalDate.parse(data[0]);
this.time = LocalTime.parse(data[1]);
this.value1 = Double.parseDouble(data[2]);
this.value2 = Double.parseDouble(data[3]);
....
}

然后像上面已经做的那样流式传输行并过滤以“CPU_ALL”开头的行并将它们映射到CPUData对象

List<CPUData> list = new ArrayList<>();

try (Stream<CPUData> stream = Files.lines(Paths.get(fileName))){
list = stream.filter(line -> line.startsWith("CPU_ALL"))
.map(line -> new CPUData(line))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
//do whatever with the list of CPUData´s
list.forEach(System.out::println);

关于java - java读取行间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58893759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com