RecordInputFormat extends DelimitedInputFormat implements OutputSchemaProvider { @Override public int[] getOutputSchema() { if (this.recordPositions == null) throw new RuntimeException("RecordInputFormat must be configured before output schema is available"); final int[] outputSchema = new int[this.numFields]; int j = 0; for(int i = 0; i < this.recordPositions.length; i++) { if (this.recordPositions[i] > 0) { outputSchema[j++] = this.recordPositions[i]; } } Arrays.sort(outputSchema); return outputSchema; }  @Override void configure(Configuration config); @SuppressWarnings("unchecked") @Override boolean readRecord(PactRecord target, byte[] bytes, int offset, int numBytes); @Override int[] getOutputSchema(); static ConfigBuilder configureRecordFormat(FileDataSource target); static final String FIELD_DELIMITER_PARAMETER; static final String NUM_FIELDS_PARAMETER; static final String FIELD_PARSER_PARAMETER_PREFIX; static final String TEXT_POSITION_PARAMETER_PREFIX; static final String RECORD_POSITION_PARAMETER_PREFIX; static final String RECORD_DELIMITER_PARAMETER; }
@Test public void testGetOutputSchema() { try { final Configuration parameters = new Configuration(); parameters.setString(RecordInputFormat.FILE_PARAMETER_KEY, "file: parameters.setInteger(RecordInputFormat.NUM_FIELDS_PARAMETER, 5); parameters.setString(RecordInputFormat.RECORD_DELIMITER_PARAMETER, "\n"); parameters.setString(RecordInputFormat.FIELD_DELIMITER_PARAMETER, "|"); parameters.setClass(RecordInputFormat.FIELD_PARSER_PARAMETER_PREFIX + 0, DecimalTextIntParser.class); parameters.setInteger(RecordInputFormat.RECORD_POSITION_PARAMETER_PREFIX + 0, 2); parameters.setClass(RecordInputFormat.FIELD_PARSER_PARAMETER_PREFIX + 1, DecimalTextIntParser.class); parameters.setInteger(RecordInputFormat.RECORD_POSITION_PARAMETER_PREFIX + 1, 7); parameters.setClass(RecordInputFormat.FIELD_PARSER_PARAMETER_PREFIX + 2, DecimalTextIntParser.class); parameters.setInteger(RecordInputFormat.RECORD_POSITION_PARAMETER_PREFIX + 2, 1); parameters.setClass(RecordInputFormat.FIELD_PARSER_PARAMETER_PREFIX + 3, DecimalTextIntParser.class); parameters.setInteger(RecordInputFormat.RECORD_POSITION_PARAMETER_PREFIX + 3, 3); parameters.setClass(RecordInputFormat.FIELD_PARSER_PARAMETER_PREFIX + 4, DecimalTextIntParser.class); parameters.setInteger(RecordInputFormat.RECORD_POSITION_PARAMETER_PREFIX + 4, 6); format.configure(parameters); assertTrue(Arrays.equals(format.getOutputSchema(), new int[]{1,2,3,6,7})); } catch (Exception ex) { Assert.fail("Test failed due to a " + ex.getClass().getSimpleName() + ": " + ex.getMessage()); } }