Search in sources :

Example 31 with FileInputSplit

use of org.apache.flink.core.fs.FileInputSplit in project flink by apache.

the class PrimitiveInputFormatTest method testIntegerInput.

@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());
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) IOException(java.io.IOException) Test(org.junit.Test)

Example 32 with FileInputSplit

use of org.apache.flink.core.fs.FileInputSplit in project flink by apache.

the class PrimitiveInputFormatTest method testDoubleInputLinewise.

@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());
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with FileInputSplit

use of org.apache.flink.core.fs.FileInputSplit in project flink by apache.

the class PrimitiveInputFormatTest method testRemovingTrailingCR.

@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());
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) IOException(java.io.IOException) Test(org.junit.Test)

Example 34 with FileInputSplit

use of org.apache.flink.core.fs.FileInputSplit in project flink by apache.

the class PrimitiveInputFormatTest method testFailingInput.

@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);
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) Test(org.junit.Test)

Example 35 with FileInputSplit

use of org.apache.flink.core.fs.FileInputSplit in project flink by apache.

the class PrimitiveInputFormatTest method testStringInput.

@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());
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Configuration(org.apache.flink.configuration.Configuration) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

FileInputSplit (org.apache.flink.core.fs.FileInputSplit)140 Test (org.junit.Test)119 Configuration (org.apache.flink.configuration.Configuration)93 Path (org.apache.flink.core.fs.Path)59 IOException (java.io.IOException)45 File (java.io.File)36 FileOutputStream (java.io.FileOutputStream)23 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)20 Row (org.apache.flink.types.Row)20 OutputStreamWriter (java.io.OutputStreamWriter)18 ParseException (org.apache.flink.api.common.io.ParseException)17 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)17 DoubleValue (org.apache.flink.types.DoubleValue)17 IntValue (org.apache.flink.types.IntValue)17 LongValue (org.apache.flink.types.LongValue)17 StringValue (org.apache.flink.types.StringValue)17 Value (org.apache.flink.types.Value)17 Plan (org.apache.flink.api.common.Plan)12 ReplicatingInputFormat (org.apache.flink.api.common.io.ReplicatingInputFormat)12 Tuple1 (org.apache.flink.api.java.tuple.Tuple1)12