16
16
17
17
package org .radarcns ;
18
18
19
+ import java .nio .file .Files ;
20
+ import java .util .Objects ;
21
+ import javax .annotation .Nonnull ;
19
22
import org .apache .avro .Schema .Field ;
20
23
import org .apache .avro .generic .GenericRecord ;
21
24
import org .apache .commons .collections .MapIterator ;
30
33
public class Frequency {
31
34
private static final Logger logger = LoggerFactory .getLogger (Frequency .class );
32
35
33
- private MultiKeyMap bins = new MultiKeyMap () ;
34
- private File binFilePath ;
36
+ private final MultiKeyMap bins ;
37
+ private final File file ;
35
38
36
- public void setBinFilePath (File binFilePath ) {
37
- this .binFilePath = binFilePath ;
39
+ public Frequency (@ Nonnull File file , @ Nonnull MultiKeyMap initialData ) {
40
+ Objects .requireNonNull (file );
41
+ Objects .requireNonNull (initialData );
42
+ this .file = file ;
43
+ this .bins = initialData ;
38
44
}
39
45
40
- public MultiKeyMap getBins () {
41
- return bins ;
46
+ public static Frequency read (File file ) {
47
+ MultiKeyMap map = new MultiKeyMap ();
48
+ try {
49
+ // Read in all lines as multikeymap (key, key, key, value)
50
+ Files .readAllLines (file .toPath ()).forEach (line -> {
51
+ String [] columns = line .split ("," );
52
+ try {
53
+ map .put (columns [0 ], columns [1 ], columns [2 ], Integer .valueOf (columns [3 ]));
54
+ } catch (ArrayIndexOutOfBoundsException ex ) {
55
+ logger .warn ("Unable to row of the bins file. Skipping." );
56
+ }
57
+ });
58
+ } catch (IOException e ) {
59
+ logger .warn ("Could not read the file with bins. Creating new file when writing." );
60
+ }
61
+ return new Frequency (file , map );
42
62
}
43
63
44
- public void addToBin (String topicName , String id , String timestamp , int countToAdd ) {
64
+ public void add (String topicName , String id , GenericRecord valueField , Field timeField ) {
65
+ String timestamp = RestructureAvroRecords .createHourTimestamp (valueField , timeField );
66
+
45
67
Integer count = (Integer ) bins .get (topicName , id , timestamp );
46
68
if (count == null ) {
47
- bins .put (topicName , id , timestamp , countToAdd );
69
+ bins .put (topicName , id , timestamp , 1 );
48
70
} else {
49
- bins .put (topicName , id , timestamp , count + countToAdd );
71
+ bins .put (topicName , id , timestamp , count + 1 );
50
72
}
51
73
}
52
74
53
- public void addToBin (String topicName , String id , GenericRecord valueField , Field timeField , int countToAdd ) {
54
- // Hour resolution
55
- String hourlyTimestamp = RestructureAvroRecords .createHourTimestamp (valueField , timeField );
56
-
57
- addToBin (topicName , id , hourlyTimestamp , countToAdd );
58
- }
59
-
60
- public void addToBin (String topicName , String id , GenericRecord valueField , Field timeField ) {
61
- addToBin (topicName , id , valueField , timeField , 1 );
62
- }
63
-
64
- public void printBins () {
75
+ public void print () {
65
76
MapIterator mapIterator = bins .mapIterator ();
66
77
67
78
while (mapIterator .hasNext ()) {
@@ -71,50 +82,24 @@ public void printBins() {
71
82
}
72
83
}
73
84
74
- public void writeBins () {
75
- // Read bins from file and add to current bins
76
- // Creates new bins if not existing yet
77
- addBinsFromFile ();
78
-
85
+ public void write () {
79
86
// Write all bins to csv
80
87
MapIterator mapIterator = bins .mapIterator ();
81
- try (FileWriter fw = new FileWriter (binFilePath , false );
82
- BufferedWriter bw = new BufferedWriter (fw );
83
- PrintWriter out = new PrintWriter (bw ))
84
- {
88
+ try (FileWriter fw = new FileWriter (file , false );
89
+ BufferedWriter bw = new BufferedWriter (fw )) {
85
90
String header = String .join ("," ,"topic" ,"device" ,"timestamp" ,"count" );
86
- out .println (header );
91
+ bw .write (header );
92
+ bw .write ('\n' );
87
93
88
94
while (mapIterator .hasNext ()) {
89
95
MultiKey key = (MultiKey ) mapIterator .next ();
90
96
Integer value = (Integer ) mapIterator .getValue ();
91
97
String data = String .join ("," , key .getKey (0 ).toString (), key .getKey (1 ).toString (), key .getKey (2 ).toString (), value .toString ());
92
- out .println (data );
93
- }
94
- } catch (IOException e ) {
95
- // TODO: exception handling
96
- e .printStackTrace ();
97
- }
98
-
99
- // Reset the map
100
- bins = new MultiKeyMap ();
101
- }
102
-
103
- private void addBinsFromFile () {
104
- try (FileReader fr = new FileReader (binFilePath );
105
- BufferedReader br = new BufferedReader (fr ))
106
- {
107
- // Read in all lines as multikeymap (key, key, key, value)
108
- String line ;
109
- br .readLine (); // Skip header
110
- while ( (line = br .readLine ()) != null ) {
111
- String [] columns = line .split ("," );
112
- this .addToBin (columns [0 ], columns [1 ], columns [2 ], Integer .valueOf (columns [3 ]));
98
+ bw .write (data );
99
+ bw .write ('\n' );
113
100
}
114
101
} catch (IOException e ) {
115
- logger .warn ("Could not read the file with bins. Creating new file when writing." );
116
- } catch (ArrayIndexOutOfBoundsException e ) {
117
- logger .warn ("Unable to parse the contents of the bins file. Skipping reading." );
102
+ logger .error ("Failed to write bins" , e );
118
103
}
119
104
}
120
105
}
0 commit comments