20
20
import java .io .IOException ;
21
21
import java .text .SimpleDateFormat ;
22
22
import java .util .Date ;
23
+ import org .apache .avro .Schema .Field ;
23
24
import org .apache .avro .file .DataFileReader ;
24
25
import org .apache .avro .generic .GenericDatumReader ;
25
26
import org .apache .avro .generic .GenericRecord ;
@@ -208,12 +209,16 @@ record = dataFileReader.next(record);
208
209
209
210
private void writeRecord (GenericRecord record , String topicName , FileCache cache )
210
211
throws IOException {
211
- GenericRecord keyField = (GenericRecord ) record .get ("keyField " );
212
- GenericRecord valueField = (GenericRecord ) record .get ("valueField " );
212
+ GenericRecord keyField = (GenericRecord ) record .get ("key " );
213
+ GenericRecord valueField = (GenericRecord ) record .get ("value " );
213
214
214
- // Make a timestamped filename YYYYMMDD_HH00.json
215
- String hourlyTimestamp = createHourTimestamp ( (Double ) valueField .get ("time" ));
216
- String outputFileName = hourlyTimestamp + "00." + outputFileExtension ;
215
+ if (keyField == null || valueField == null ) {
216
+ logger .error ("Failed to process {}" , record );
217
+ throw new IOException ("Failed to process " + record + "; no key or value" );
218
+ }
219
+
220
+ Field timeField = valueField .getSchema ().getField ("time" );
221
+ String outputFileName = createFilename (valueField , timeField );
217
222
218
223
// Clean user id and create final output pathname
219
224
String userId = keyField .get ("userId" ).toString ().replaceAll ("\\ W+" , "" );
@@ -225,13 +230,28 @@ private void writeRecord(GenericRecord record, String topicName, FileCache cache
225
230
cache .writeRecord (outputFile , record );
226
231
227
232
// Count data (binned and total)
228
- bins .addToBin (topicName , keyField .get ("sourceId" ).toString (), ( Double ) valueField . get ( "time" ) );
233
+ bins .addToBin (topicName , keyField .get ("sourceId" ).toString (), valueField , timeField );
229
234
processedRecordsCount ++;
230
235
}
231
236
232
- public static String createHourTimestamp (Double time ) {
237
+ private String createFilename (GenericRecord valueField , Field timeField ) {
238
+ if (timeField == null ) {
239
+ logger .warn ("Time field of record valueField " + valueField + " is not set" );
240
+ return "unknown." + outputFileExtension ;
241
+ }
242
+ // Make a timestamped filename YYYYMMDD_HH00.json
243
+ String hourlyTimestamp = createHourTimestamp (valueField , timeField );
244
+ return hourlyTimestamp + "00." + outputFileExtension ;
245
+ }
246
+
247
+ public static String createHourTimestamp (GenericRecord valueField , Field timeField ) {
248
+ if (timeField == null ) {
249
+ return "unknown" ;
250
+ }
251
+
252
+ double time = (Double ) valueField .get (timeField .pos ());
233
253
// Convert from millis to date and apply dateFormat
234
- Date date = new Date ( time . longValue () * 1000 );
254
+ Date date = new Date (( long ) ( time * 1000d ) );
235
255
return FILE_DATE_FORMAT .format (date );
236
256
}
237
257
}
0 commit comments