PrimitiveInputFormat extends DelimitedInputFormat<OT> { @Override public void open(FileInputSplit split) throws IOException { super.open(split); Class<? extends FieldParser<OT>> parserType = FieldParser.getParserForType(primitiveClass); if (parserType == null) { throw new IllegalArgumentException("The type '" + primitiveClass.getName() + "' is not supported for the primitive input format."); } parser = InstantiationUtil.instantiate(parserType, FieldParser.class); } PrimitiveInputFormat(Path filePath, Class<OT> primitiveClass);  PrimitiveInputFormat(Path filePath, String delimiter, Class<OT> primitiveClass); @Override void open(FileInputSplit split); @Override OT readRecord(OT reuse, byte[] bytes, int offset, int numBytes);  }
@Test public void testStringInput() { try { final String fileContent = "abc||def||||"; final FileInputSplit split = createInputSplit(fileContent); final PrimitiveInputFormat<String> format = new PrimitiveInputFormat<String>(PATH, "||", String.class); final Configuration parameters = new Configuration(); format.configure(parameters); format.open(split); String result = null; result = format.nextRecord(result); assertEquals("abc", result); result = format.nextRecord(result); assertEquals("def", result); result = format.nextRecord(result); assertEquals("", result); result = format.nextRecord(result); assertNull(result); assertTrue(format.reachedEnd()); } catch (Exception ex) { ex.printStackTrace(); fail("Test failed due to a " + ex.getClass().getName() + ": " + ex.getMessage()); } }
@Test public void testIntegerInput() throws IOException { try { final String fileContent = "111|222|"; final FileInputSplit split = createInputSplit(fileContent); final PrimitiveInputFormat<Integer> format = new PrimitiveInputFormat<Integer>(PATH,"|", Integer.class); format.configure(new Configuration()); format.open(split); Integer result = null; result = format.nextRecord(result); assertEquals(Integer.valueOf(111), result); result = format.nextRecord(result); assertEquals(Integer.valueOf(222), result); result = format.nextRecord(result); assertNull(result); assertTrue(format.reachedEnd()); } catch (Exception ex) { fail("Test failed due to a " + ex.getClass().getName() + ": " + ex.getMessage()); } }
@Test public void testDoubleInputLinewise() throws IOException { try { final String fileContent = "1.21\n2.23\n"; final FileInputSplit split = createInputSplit(fileContent); final PrimitiveInputFormat<Double> format = new PrimitiveInputFormat<Double>(PATH, Double.class); format.configure(new Configuration()); format.open(split); Double result = null; result = format.nextRecord(result); assertEquals(Double.valueOf(1.21), result); result = format.nextRecord(result); assertEquals(Double.valueOf(2.23), result); result = format.nextRecord(result); assertNull(result); assertTrue(format.reachedEnd()); } catch (Exception ex) { fail("Test failed due to a " + ex.getClass().getName() + ": " + ex.getMessage()); } }
@Test public void testRemovingTrailingCR() { try { String first = "First line"; String second = "Second line"; String fileContent = first + "\r\n" + second + "\r\n"; final FileInputSplit split = createInputSplit(fileContent); final PrimitiveInputFormat<String> format = new PrimitiveInputFormat<String>(PATH ,String.class); format.configure(new Configuration()); format.open(split); String result = null; result = format.nextRecord(result); assertEquals(first, result); result = format.nextRecord(result); assertEquals(second, result); } catch (Exception ex) { fail("Test failed due to a " + ex.getClass().getName() + ": " + ex.getMessage()); } }
@Test(expected = IOException.class) public void testFailingInput() throws IOException { final String fileContent = "111|222|asdf|17"; final FileInputSplit split = createInputSplit(fileContent); final PrimitiveInputFormat<Integer> format = new PrimitiveInputFormat<Integer>(PATH,"|", Integer.class); format.configure(new Configuration()); format.open(split); Integer result = null; result = format.nextRecord(result); assertEquals(Integer.valueOf(111), result); result = format.nextRecord(result); assertEquals(Integer.valueOf(222), result); result = format.nextRecord(result); }