@Test public void testFixedLengthWidthPadding() throws Exception { String[] directives = new String[] { "parse-as-fixed-length :body 4,4,4,4,4,4 '_'" , }; List<Row> rows = Arrays.asList( new Row("body", "AA__BB__C___D___EEE_FFFF") ); rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 1); Assert.assertEquals("AA", rows.get(0).getValue("body_1")); Assert.assertEquals("BB", rows.get(0).getValue("body_2")); Assert.assertEquals("C", rows.get(0).getValue("body_3")); Assert.assertEquals("D", rows.get(0).getValue("body_4")); Assert.assertEquals("EEE", rows.get(0).getValue("body_5")); Assert.assertEquals("FFFF", rows.get(0).getValue("body_6")); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String'.", col)); } if (object instanceof String) { String data = (String) object; int length = data.length(); if (length < recordLength) { throw new ErrorRowException( NAME, String.format("Column '%s' contains a value with fewer characters than the specified length " + "of row. Expected at least %d characters but found %s characters.", col, recordLength, length), 2); } int index = 1; while ((index + recordLength - 1) <= length) { Row newRow = new Row(row); int recPosition = index; int colid = 1; for (int width : widths) { String val = data.substring(recPosition - 1, recPosition + width - 1); if (padding != null) { val = val.replaceAll(padding, ""); } newRow.add(String.format("%s_%d", col, colid), val); recPosition += width; colid += 1; } results.add(newRow); index = (index + recordLength); } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String'.", col, object.getClass().getSimpleName())); } } } return results; }
FixedLengthParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String'.", col)); } if (object instanceof String) { String data = (String) object; int length = data.length(); if (length < recordLength) { throw new ErrorRowException( NAME, String.format("Column '%s' contains a value with fewer characters than the specified length " + "of row. Expected at least %d characters but found %s characters.", col, recordLength, length), 2); } int index = 1; while ((index + recordLength - 1) <= length) { Row newRow = new Row(row); int recPosition = index; int colid = 1; for (int width : widths) { String val = data.substring(recPosition - 1, recPosition + width - 1); if (padding != null) { val = val.replaceAll(padding, ""); } newRow.add(String.format("%s_%d", col, colid), val); recPosition += width; colid += 1; } results.add(newRow); index = (index + recordLength); } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String'.", col, object.getClass().getSimpleName())); } } } return results; } }
FixedLengthParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String'.", col)); } if (object instanceof String) { String data = (String) object; int length = data.length(); if (length < recordLength) { throw new ErrorRowException( NAME, String.format("Column '%s' contains a value with fewer characters than the specified length " + "of row. Expected at least %d characters but found %s characters.", col, recordLength, length), 2); } int index = 1; while ((index + recordLength - 1) <= length) { Row newRow = new Row(row); int recPosition = index; int colid = 1; for (int width : widths) { String val = data.substring(recPosition - 1, recPosition + width - 1); if (padding != null) { val = val.replaceAll(padding, ""); } newRow.add(String.format("%s_%d", col, colid), val); recPosition += width; colid += 1; } results.add(newRow); index = (index + recordLength); } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String'.", col, object.getClass().getSimpleName())); } } } return results; }  }
FixedLengthParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String'.", col)); } if (object instanceof String) { String data = (String) object; int length = data.length(); if (length < recordLength) { throw new ErrorRowException( NAME, String.format("Column '%s' contains a value with fewer characters than the specified length " + "of row. Expected at least %d characters but found %s characters.", col, recordLength, length), 2); } int index = 1; while ((index + recordLength - 1) <= length) { Row newRow = new Row(row); int recPosition = index; int colid = 1; for (int width : widths) { String val = data.substring(recPosition - 1, recPosition + width - 1); if (padding != null) { val = val.replaceAll(padding, ""); } newRow.add(String.format("%s_%d", col, colid), val); recPosition += width; colid += 1; } results.add(newRow); index = (index + recordLength); } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String'.", col, object.getClass().getSimpleName())); } } } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
FixedLengthParser implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException, ErrorRowException { List<Row> results = new ArrayList<>(); for (Row row : rows) { int idx = row.find(col); if (idx != -1) { Object object = row.getValue(idx); if (object == null) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has null value. It should be a non-null 'String'.", col)); } if (object instanceof String) { String data = (String) object; int length = data.length(); if (length < recordLength) { throw new ErrorRowException( NAME, String.format("Column '%s' contains a value with fewer characters than the specified length " + "of row. Expected at least %d characters but found %s characters.", col, recordLength, length), 2); } int index = 1; while ((index + recordLength - 1) <= length) { Row newRow = new Row(row); int recPosition = index; int colid = 1; for (int width : widths) { String val = data.substring(recPosition - 1, recPosition + width - 1); if (padding != null) { val = val.replaceAll(padding, ""); } newRow.add(String.format("%s_%d", col, colid), val); recPosition += width; colid += 1; } results.add(newRow); index = (index + recordLength); } } else { throw new DirectiveExecutionException( NAME, String.format("Column '%s' has invalid type '%s'. It should be of type 'String'.", col, object.getClass().getSimpleName())); } } } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }