SetDateBolt extends AbstractProcessingBolt { @Override protected void process(LogRecord record) { SimpleDateFormat sourceSdf = new SimpleDateFormat(dateFormat); String dateValue = null; for (int i = 0; i < dateFields.size(); i++) { dateValue = record.getValue(dateFields.get(i)); if (StringUtils.isNotBlank(dateValue)) { if (_updateIndexField && i != 0) { record.setValue(dateFields.get(0), dateValue); } break; } } try { Date date = sourceSdf.parse(dateValue); record.setDate(date); } catch (ParseException e) { logger.error("Failed to parse timestamp: " + dateValue); } }  @Override void configure(Configuration conf); static final String DATE_FIELD; static final String DATE_FORMAT; }
@Test public void testEnrichDataswift(@Mocked final LogRecord record) throws Exception { final Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2014, Calendar.JUNE, 10, 20, 22, 4); bolt.dateFields = Arrays.asList("interaction.created_at"); bolt.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; new Expectations() {{ record.getValue(bolt.dateFields.get(0)); result = "Tue, 10 Jun 2014 20:22:04 +0000"; record.setDate(calendar.getTime()); }}; bolt.process(record); }
@Test public void testEnrichGnip(@Mocked final LogRecord record) throws Exception { final Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2015, Calendar.JULY, 12, 0, 0, 52); bolt.dateFields = Arrays.asList("postedTime"); bolt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"; new Expectations() {{ record.getValue(bolt.dateFields.get(0)); result = "2015-07-12T00:00:52.000Z"; record.setDate(calendar.getTime()); }}; bolt.process(record); }