Search in sources :

Example 6 with FileReader

use of java.io.FileReader in project flink by apache.

the class DataSinkTaskTest method testSortingDataSinkTask.

@Test
@SuppressWarnings("unchecked")
public void testSortingDataSinkTask() {
    int keyCnt = 100;
    int valCnt = 20;
    double memoryFraction = 1.0;
    super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE);
    super.addInput(new UniformRecordGenerator(keyCnt, valCnt, true), 0);
    DataSinkTask<Record> testTask = new DataSinkTask<>();
    // set sorting
    super.getTaskConfig().setInputLocalStrategy(0, LocalStrategy.SORT);
    super.getTaskConfig().setInputComparator(new RecordComparatorFactory(new int[] { 1 }, (new Class[] { IntValue.class })), 0);
    super.getTaskConfig().setRelativeMemoryInput(0, memoryFraction);
    super.getTaskConfig().setFilehandlesInput(0, 8);
    super.getTaskConfig().setSpillingThresholdInput(0, 0.8f);
    super.registerFileOutputTask(testTask, MockOutputFormat.class, new File(tempTestPath).toURI().toString());
    try {
        testTask.invoke();
    } catch (Exception e) {
        LOG.debug("Exception while invoking the test task.", e);
        Assert.fail("Invoke method caused exception.");
    }
    File tempTestFile = new File(this.tempTestPath);
    Assert.assertTrue("Temp output file does not exist", tempTestFile.exists());
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader(tempTestFile);
        br = new BufferedReader(fr);
        Set<Integer> keys = new HashSet<>();
        int curVal = -1;
        while (br.ready()) {
            String line = br.readLine();
            Integer key = Integer.parseInt(line.substring(0, line.indexOf("_")));
            Integer val = Integer.parseInt(line.substring(line.indexOf("_") + 1, line.length()));
            // check that values are in correct order
            Assert.assertTrue("Values not in ascending order", val >= curVal);
            // next value hit
            if (val > curVal) {
                if (curVal != -1) {
                    // check that we saw 100 distinct keys for this values
                    Assert.assertTrue("Keys missing for value", keys.size() == 100);
                }
                // empty keys set
                keys.clear();
                // update current value
                curVal = val;
            }
            Assert.assertTrue("Duplicate key for value", keys.add(key));
        }
    } catch (FileNotFoundException e) {
        Assert.fail("Out file got lost...");
    } catch (IOException ioe) {
        Assert.fail("Caught IOE while reading out file");
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (Throwable t) {
            }
        }
        if (fr != null) {
            try {
                fr.close();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : RecordComparatorFactory(org.apache.flink.runtime.testutils.recordutils.RecordComparatorFactory) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) Record(org.apache.flink.types.Record) FileReader(java.io.FileReader) UniformRecordGenerator(org.apache.flink.runtime.operators.testutils.UniformRecordGenerator) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with FileReader

use of java.io.FileReader in project flink by apache.

the class DataSinkTaskTest method testUnionDataSinkTask.

@Test
public void testUnionDataSinkTask() {
    int keyCnt = 10;
    int valCnt = 20;
    super.initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE);
    final IteratorWrappingTestSingleInputGate<?>[] readers = new IteratorWrappingTestSingleInputGate[4];
    readers[0] = super.addInput(new UniformRecordGenerator(keyCnt, valCnt, 0, 0, false), 0, false);
    readers[1] = super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt, 0, false), 0, false);
    readers[2] = super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt * 2, 0, false), 0, false);
    readers[3] = super.addInput(new UniformRecordGenerator(keyCnt, valCnt, keyCnt * 3, 0, false), 0, false);
    DataSinkTask<Record> testTask = new DataSinkTask<>();
    super.registerFileOutputTask(testTask, MockOutputFormat.class, new File(tempTestPath).toURI().toString());
    try {
        // which checks forwards existing notifications on registerListener calls.
        for (IteratorWrappingTestSingleInputGate<?> reader : readers) {
            reader.notifyNonEmpty();
        }
        testTask.invoke();
    } catch (Exception e) {
        LOG.debug("Exception while invoking the test task.", e);
        Assert.fail("Invoke method caused exception.");
    }
    File tempTestFile = new File(this.tempTestPath);
    Assert.assertTrue("Temp output file does not exist", tempTestFile.exists());
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader(tempTestFile);
        br = new BufferedReader(fr);
        HashMap<Integer, HashSet<Integer>> keyValueCountMap = new HashMap<>(keyCnt);
        while (br.ready()) {
            String line = br.readLine();
            Integer key = Integer.parseInt(line.substring(0, line.indexOf("_")));
            Integer val = Integer.parseInt(line.substring(line.indexOf("_") + 1, line.length()));
            if (!keyValueCountMap.containsKey(key)) {
                keyValueCountMap.put(key, new HashSet<Integer>());
            }
            keyValueCountMap.get(key).add(val);
        }
        Assert.assertTrue("Invalid key count in out file. Expected: " + keyCnt + " Actual: " + keyValueCountMap.keySet().size(), keyValueCountMap.keySet().size() == keyCnt * 4);
        for (Integer key : keyValueCountMap.keySet()) {
            Assert.assertTrue("Invalid value count for key: " + key + ". Expected: " + valCnt + " Actual: " + keyValueCountMap.get(key).size(), keyValueCountMap.get(key).size() == valCnt);
        }
    } catch (FileNotFoundException e) {
        Assert.fail("Out file got lost...");
    } catch (IOException ioe) {
        Assert.fail("Caught IOE while reading out file");
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (Throwable t) {
            }
        }
        if (fr != null) {
            try {
                fr.close();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IteratorWrappingTestSingleInputGate(org.apache.flink.runtime.io.network.partition.consumer.IteratorWrappingTestSingleInputGate) BufferedReader(java.io.BufferedReader) Record(org.apache.flink.types.Record) FileReader(java.io.FileReader) UniformRecordGenerator(org.apache.flink.runtime.operators.testutils.UniformRecordGenerator) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with FileReader

use of java.io.FileReader in project flink by apache.

the class TestBaseUtils method getResultReader.

public static BufferedReader[] getResultReader(String resultPath, String[] excludePrefixes, boolean inOrderOfFiles) throws IOException {
    File[] files = getAllInvolvedFiles(resultPath, excludePrefixes);
    if (inOrderOfFiles) {
        // sort the files after their name (1, 2, 3, 4)...
        // we cannot sort by path, because strings sort by prefix
        Arrays.sort(files, new Comparator<File>() {

            @Override
            public int compare(File o1, File o2) {
                try {
                    int f1 = Integer.parseInt(o1.getName());
                    int f2 = Integer.parseInt(o2.getName());
                    return f1 < f2 ? -1 : (f1 > f2 ? 1 : 0);
                } catch (NumberFormatException e) {
                    throw new RuntimeException("The file names are no numbers and cannot be ordered: " + o1.getName() + "/" + o2.getName());
                }
            }
        });
    }
    BufferedReader[] readers = new BufferedReader[files.length];
    for (int i = 0; i < files.length; i++) {
        readers[i] = new BufferedReader(new FileReader(files[i]));
    }
    return readers;
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 9 with FileReader

use of java.io.FileReader in project flink by apache.

the class AggregatorsITCase method testDistributedCacheWithIterations.

@Test
public void testDistributedCacheWithIterations() throws Exception {
    File tempFile = new File(testPath);
    try (FileWriter writer = new FileWriter(tempFile)) {
        writer.write(testString);
    }
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.registerCachedFile(resultPath, testName);
    IterativeDataSet<Long> solution = env.fromElements(1L).iterate(2);
    solution.closeWith(env.generateSequence(1, 2).filter(new RichFilterFunction<Long>() {

        @Override
        public void open(Configuration parameters) throws Exception {
            File file = getRuntimeContext().getDistributedCache().getFile(testName);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String output = reader.readLine();
            reader.close();
            assertEquals(output, testString);
        }

        @Override
        public boolean filter(Long value) throws Exception {
            return false;
        }
    }).withBroadcastSet(solution, "SOLUTION")).output(new DiscardingOutputFormat<Long>());
    env.execute();
    // this will be a useless verification now.
    expected = testString;
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Configuration(org.apache.flink.configuration.Configuration) FileWriter(java.io.FileWriter) RichFilterFunction(org.apache.flink.api.common.functions.RichFilterFunction) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 10 with FileReader

use of java.io.FileReader in project hadoop by apache.

the class DataGenerator method genFiles.

/** Read file structure file under the input directory.
   * Create each file under the specified root.
   * The file names are relative to the root.
   */
private void genFiles() throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(new File(inDir, StructureGenerator.FILE_STRUCTURE_FILE_NAME)));
    String line;
    while ((line = in.readLine()) != null) {
        String[] tokens = line.split(" ");
        if (tokens.length != 2) {
            throw new IOException("Expect at most 2 tokens per line: " + line);
        }
        String fileName = root + tokens[0];
        long fileSize = (long) (BLOCK_SIZE * Double.parseDouble(tokens[1]));
        genFile(new Path(fileName), fileSize);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileReader (java.io.FileReader)1602 BufferedReader (java.io.BufferedReader)1114 IOException (java.io.IOException)762 File (java.io.File)701 FileNotFoundException (java.io.FileNotFoundException)264 ArrayList (java.util.ArrayList)250 Test (org.junit.Test)154 FileWriter (java.io.FileWriter)121 HashMap (java.util.HashMap)103 Reader (java.io.Reader)86 BufferedWriter (java.io.BufferedWriter)78 Properties (java.util.Properties)57 InputStreamReader (java.io.InputStreamReader)54 Map (java.util.Map)54 List (java.util.List)51 Matcher (java.util.regex.Matcher)49 LineNumberReader (java.io.LineNumberReader)46 HashSet (java.util.HashSet)43 FileInputStream (java.io.FileInputStream)41 PrintWriter (java.io.PrintWriter)40