16
16
17
17
package org .radarcns ;
18
18
19
- import java .io .File ;
20
- import java .io .FileWriter ;
21
- import java .io .IOException ;
22
- import java .nio .file .Files ;
23
- import java .text .SimpleDateFormat ;
24
- import java .util .ArrayList ;
25
- import java .util .Date ;
26
- import java .util .HashMap ;
27
- import java .util .List ;
28
- import java .util .Map ;
29
- import java .util .TimeZone ;
30
19
import org .apache .avro .Schema .Field ;
31
20
import org .apache .avro .file .DataFileReader ;
32
21
import org .apache .avro .generic .GenericDatumReader ;
45
34
import org .slf4j .Logger ;
46
35
import org .slf4j .LoggerFactory ;
47
36
48
- import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
37
+ import java .io .File ;
38
+ import java .io .FileWriter ;
39
+ import java .io .IOException ;
40
+ import java .text .SimpleDateFormat ;
41
+ import java .util .ArrayList ;
42
+ import java .util .Date ;
43
+ import java .util .HashMap ;
44
+ import java .util .List ;
45
+ import java .util .Map ;
46
+ import java .util .TimeZone ;
49
47
50
48
public class RestructureAvroRecords {
51
49
private static final Logger logger = LoggerFactory .getLogger (RestructureAvroRecords .class );
@@ -255,8 +253,8 @@ private void writeRecord(GenericRecord record, String topicName, FileCacheStore
255
253
throw new IOException ("Failed to process " + record + "; no key or value" );
256
254
}
257
255
258
- Field timeField = valueField . getSchema (). getField ( "time" );
259
- String outputFileName = createFilename (valueField , timeField );
256
+ Date time = getDate ( keyField , valueField );
257
+ String outputFileName = createFilename (time );
260
258
261
259
// Clean user id and create final output pathname
262
260
String userId = keyField .get ("userId" ).toString ().replaceAll ("[^a-zA-Z0-9_-]+" , "" );
@@ -275,29 +273,42 @@ private void writeRecord(GenericRecord record, String topicName, FileCacheStore
275
273
}
276
274
277
275
// Count data (binned and total)
278
- bins .add (topicName , keyField .get ("sourceId" ).toString (), valueField , timeField );
276
+ bins .add (topicName , keyField .get ("sourceId" ).toString (), time );
279
277
processedRecordsCount ++;
280
278
}
281
279
282
- private String createFilename (GenericRecord valueField , Field timeField ) {
283
- if (timeField == null ) {
284
- logger .warn ("Time field of record valueField " + valueField + " is not set" );
285
- return "unknown ." + outputFileExtension ;
280
+ private String createFilename (Date date ) {
281
+ if (date == null ) {
282
+ logger .warn ("Time field of record valueField is not set" );
283
+ return "unknown_date ." + outputFileExtension ;
286
284
}
287
285
// Make a timestamped filename YYYYMMDD_HH00.json
288
- String hourlyTimestamp = createHourTimestamp (valueField , timeField );
286
+ String hourlyTimestamp = createHourTimestamp (date );
289
287
return hourlyTimestamp + "00." + outputFileExtension ;
290
288
}
291
289
292
- public static String createHourTimestamp (GenericRecord valueField , Field timeField ) {
293
- if (timeField == null ) {
294
- return "unknown " ;
290
+ public static String createHourTimestamp (Date date ) {
291
+ if (date == null ) {
292
+ return "unknown_date " ;
295
293
}
296
294
297
- double time = (Double ) valueField .get (timeField .pos ());
298
- // Convert from millis to date and apply dateFormat
299
- Date date = new Date ((long ) (time * 1000d ));
300
295
return FILE_DATE_FORMAT .format (date );
301
296
}
302
297
298
+ public static Date getDate (GenericRecord keyField , GenericRecord valueField ) {
299
+ Field timeField = valueField .getSchema ().getField ("time" );
300
+ if (timeField != null ) {
301
+ double time = (Double ) valueField .get (timeField .pos ());
302
+ // Convert from millis to date and apply dateFormat
303
+ return new Date ((long ) (time * 1000d ));
304
+ }
305
+
306
+ // WindowedKey
307
+ timeField = keyField .getSchema ().getField ("start" );
308
+ if (timeField == null ) {
309
+ return null ;
310
+ }
311
+ long time = (Long ) keyField .get ("start" );
312
+ return new Date (time );
313
+ }
303
314
}
0 commit comments