@@ -240,7 +240,7 @@ private void processFile(Path filePath, String topicName, FileCacheStore cache,
240
240
record = dataFileReader .next (record );
241
241
242
242
// Get the fields
243
- this .writeRecord (record , topicName , cache );
243
+ this .writeRecord (record , topicName , cache , 0 );
244
244
}
245
245
246
246
// Write which file has been processed and update bins
@@ -253,7 +253,7 @@ record = dataFileReader.next(record);
253
253
}
254
254
}
255
255
256
- private void writeRecord (GenericRecord record , String topicName , FileCacheStore cache )
256
+ private void writeRecord (GenericRecord record , String topicName , FileCacheStore cache , int suffix )
257
257
throws IOException {
258
258
GenericRecord keyField = (GenericRecord ) record .get ("key" );
259
259
GenericRecord valueField = (GenericRecord ) record .get ("value" );
@@ -264,7 +264,7 @@ private void writeRecord(GenericRecord record, String topicName, FileCacheStore
264
264
}
265
265
266
266
Date time = getDate (keyField , valueField );
267
- java .nio .file .Path outputFileName = createFilename (time );
267
+ java .nio .file .Path outputFileName = createFilename (time , suffix );
268
268
269
269
String projectId ;
270
270
@@ -284,28 +284,43 @@ private void writeRecord(GenericRecord record, String topicName, FileCacheStore
284
284
java .nio .file .Path outputPath = userTopicDir .resolve (outputFileName );
285
285
286
286
// Write data
287
- cache .writeRecord (outputPath , record );
287
+ int response = cache .writeRecord (outputPath , record );
288
288
289
- java .nio .file .Path schemaPath = userTopicDir .resolve (SCHEMA_OUTPUT_FILE_NAME );
290
- if (!Files .exists (schemaPath )) {
291
- try (Writer writer = Files .newBufferedWriter (schemaPath )) {
292
- writer .write (record .getSchema ().toString (true ));
289
+ if (response == FileCacheStore .CACHE_AND_NO_WRITE ) {
290
+ // Write was unsuccessful due to different number of columns,
291
+ // try again with new file name
292
+ writeRecord (record , topicName , cache , ++suffix );
293
+ } else {
294
+ // Write was successful, finalize the write
295
+ java .nio .file .Path schemaPath = userTopicDir .resolve (SCHEMA_OUTPUT_FILE_NAME );
296
+ if (!Files .exists (schemaPath )) {
297
+ try (Writer writer = Files .newBufferedWriter (schemaPath )) {
298
+ writer .write (record .getSchema ().toString (true ));
299
+ }
293
300
}
294
- }
295
301
296
- // Count data (binned and total)
297
- bins .add (topicName , keyField .get ("sourceId" ).toString (), time );
298
- processedRecordsCount ++;
302
+ // Count data (binned and total)
303
+ bins .add (topicName , keyField .get ("sourceId" ).toString (), time );
304
+ processedRecordsCount ++;
305
+ }
299
306
}
300
307
301
- private java .nio .file .Path createFilename (Date date ) {
308
+ private java .nio .file .Path createFilename (Date date , int suffix ) {
302
309
if (date == null ) {
303
310
logger .warn ("Time field of record valueField is not set" );
304
311
return Paths .get ("unknown_date." + outputFileExtension );
305
312
}
313
+
314
+ String finalSuffix ;
315
+ if (suffix == 0 ) {
316
+ finalSuffix = "" ;
317
+ } else {
318
+ finalSuffix = "_" + suffix ;
319
+ }
320
+
306
321
// Make a timestamped filename YYYYMMDD_HH00.json
307
322
String hourlyTimestamp = createHourTimestamp (date );
308
- return Paths .get (hourlyTimestamp + "00." + outputFileExtension );
323
+ return Paths .get (hourlyTimestamp + "00" + finalSuffix + " ." + outputFileExtension );
309
324
}
310
325
311
326
public static String createHourTimestamp (Date date ) {
0 commit comments