L2FormattingMapper extends Mapper<NullWritable, NullWritable, NullWritable, NullWritable> { public static String getProductName(Configuration jobConfig, String fileName) { String regex = jobConfig.get(JobConfigNames.CALVALUS_OUTPUT_REGEX, null); String replacement = jobConfig.get(JobConfigNames.CALVALUS_OUTPUT_REPLACEMENT, null); String dateElement = jobConfig.get(JobConfigNames.CALVALUS_OUTPUT_DATE_ELEMENT, null); String dateFormat = jobConfig.get(JobConfigNames.CALVALUS_OUTPUT_DATE_FORMAT, null); try { String newProductName = FileUtils.getFilenameWithoutExtension(fileName); if (regex != null && replacement != null) { Matcher m = Pattern.compile(regex).matcher(newProductName); newProductName = m.replaceAll(replacement); if (dateElement != null && dateFormat != null) { final String dateString = m.replaceAll(dateElement); final DateFormat df1 = DateUtils.createDateFormat(dateFormat); final DateFormat df2 = DateUtils.createDateFormat(newProductName); final Date date = df1.parse(dateString); newProductName = df2.format(date); } } return newProductName; } catch (Exception e) { if (dateElement == null || dateFormat == null) { throw new RuntimeException("failed to convert name " + fileName + " matching regex " + regex + " by " + replacement, e); } else { throw new RuntimeException("failed to convert name " + fileName + " matching regex " + regex + " date ele " + dateElement + " date format " + dateFormat + " by " + replacement, e); } } }  @Override void run(Mapper.Context context); static String getProductName(Configuration jobConfig, String fileName);  }
@Test public void testGetProductName() throws Exception { JobConf jobConfig = new JobConf(); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_REGEX, "MER_RR__1P(.*)"); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_REPLACEMENT, "MER_RR__2P$1"); String input = "MER_RR__1PRLRA20120408_022952_000026243113_00262_52862_0643.N1"; String output = L2FormattingMapper.getProductName(jobConfig, input); assertEquals("MER_RR__2PRLRA20120408_022952_000026243113_00262_52862_0643", output); }
@Test public void testGetProductNameWithDate() throws Exception { JobConf jobConfig = new JobConf(); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_REGEX, "LC8(......)(.......).*"); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_REPLACEMENT, "yyyyMMdd'-L8-$1-Elbe_Tideelbe'"); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_DATE_ELEMENT, "$2"); jobConfig.set(JobConfigNames.CALVALUS_OUTPUT_DATE_FORMAT, "yyyyDDD"); String input = "LC81370352016335LGN00.tgz"; String output = L2FormattingMapper.getProductName(jobConfig, input); assertEquals("20161130-L8-137035-Elbe_Tideelbe", output); }