Search in sources :

Example 16 with FSDataOutputStream

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

the class SavepointStoreTest method testUnexpectedSavepoint.

/**
	 * Tests loading with unexpected magic number.
	 */
@Test
public void testUnexpectedSavepoint() throws Exception {
    // Random file
    Path filePath = new Path(tmp.getRoot().getPath(), UUID.randomUUID().toString());
    FSDataOutputStream fdos = FileSystem.get(filePath.toUri()).create(filePath, false);
    DataOutputStream dos = new DataOutputStream(fdos);
    for (int i = 0; i < 10; i++) {
        dos.writeLong(ThreadLocalRandom.current().nextLong());
    }
    try {
        SavepointStore.loadSavepoint(filePath.toString(), Thread.currentThread().getContextClassLoader());
        fail("Did not throw expected Exception");
    } catch (RuntimeException e) {
        assertTrue(e.getMessage().contains("Flink 1.0") && e.getMessage().contains("Unexpected magic number"));
    }
}
Also used : Path(org.apache.flink.core.fs.Path) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) DataOutputStream(java.io.DataOutputStream) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 17 with FSDataOutputStream

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

the class DistCp method main.

public static void main(String[] args) throws Exception {
    // set up the execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    ParameterTool params = ParameterTool.fromArgs(args);
    if (!params.has("input") || !params.has("output")) {
        System.err.println("Usage: --input <path> --output <path> [--parallelism <n>]");
        return;
    }
    final Path sourcePath = new Path(params.get("input"));
    final Path targetPath = new Path(params.get("output"));
    if (!isLocal(env) && !(isOnDistributedFS(sourcePath) && isOnDistributedFS(targetPath))) {
        System.out.println("In a distributed mode only HDFS input/output paths are supported");
        return;
    }
    final int parallelism = params.getInt("parallelism", 10);
    if (parallelism <= 0) {
        System.err.println("Parallelism should be greater than 0");
        return;
    }
    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(params);
    env.setParallelism(parallelism);
    long startTime = System.currentTimeMillis();
    LOGGER.info("Initializing copy tasks");
    List<FileCopyTask> tasks = getCopyTasks(sourcePath);
    LOGGER.info("Copy task initialization took " + (System.currentTimeMillis() - startTime) + "ms");
    DataSet<FileCopyTask> inputTasks = new DataSource<>(env, new FileCopyTaskInputFormat(tasks), new GenericTypeInfo<>(FileCopyTask.class), "fileCopyTasks");
    FlatMapOperator<FileCopyTask, Object> res = inputTasks.flatMap(new RichFlatMapFunction<FileCopyTask, Object>() {

        private static final long serialVersionUID = 1109254230243989929L;

        private LongCounter fileCounter;

        private LongCounter bytesCounter;

        @Override
        public void open(Configuration parameters) throws Exception {
            bytesCounter = getRuntimeContext().getLongCounter(BYTES_COPIED_CNT_NAME);
            fileCounter = getRuntimeContext().getLongCounter(FILES_COPIED_CNT_NAME);
        }

        @Override
        public void flatMap(FileCopyTask task, Collector<Object> out) throws Exception {
            LOGGER.info("Processing task: " + task);
            Path outPath = new Path(targetPath, task.getRelativePath());
            FileSystem targetFs = targetPath.getFileSystem();
            // creating parent folders in case of a local FS
            if (!targetFs.isDistributedFS()) {
                // dealing with cases like file:///tmp or just /tmp
                File outFile = outPath.toUri().isAbsolute() ? new File(outPath.toUri()) : new File(outPath.toString());
                File parentFile = outFile.getParentFile();
                if (!parentFile.mkdirs() && !parentFile.exists()) {
                    throw new RuntimeException("Cannot create local file system directories: " + parentFile);
                }
            }
            FSDataOutputStream outputStream = null;
            FSDataInputStream inputStream = null;
            try {
                outputStream = targetFs.create(outPath, FileSystem.WriteMode.OVERWRITE);
                inputStream = task.getPath().getFileSystem().open(task.getPath());
                int bytes = IOUtils.copy(inputStream, outputStream);
                bytesCounter.add(bytes);
            } finally {
                IOUtils.closeQuietly(inputStream);
                IOUtils.closeQuietly(outputStream);
            }
            fileCounter.add(1L);
        }
    });
    // no data sinks are needed, therefore just printing an empty result
    res.print();
    Map<String, Object> accumulators = env.getLastJobExecutionResult().getAllAccumulatorResults();
    LOGGER.info("== COUNTERS ==");
    for (Map.Entry<String, Object> e : accumulators.entrySet()) {
        LOGGER.info(e.getKey() + ": " + e.getValue());
    }
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Configuration(org.apache.flink.configuration.Configuration) LongCounter(org.apache.flink.api.common.accumulators.LongCounter) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Path(org.apache.flink.core.fs.Path) IOException(java.io.IOException) DataSource(org.apache.flink.api.java.operators.DataSource) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) File(java.io.File) Map(java.util.Map)

Example 18 with FSDataOutputStream

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

the class FileUtilsTest method generateTestFile.

/**
 * Generates a random content file.
 *
 * @param outputFile the path of the output file
 * @param length the size of content to generate
 * @return MD5 of the output file
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
private static String generateTestFile(String outputFile, int length) throws IOException, NoSuchAlgorithmException {
    Path outputFilePath = new Path(outputFile);
    final FileSystem fileSystem = outputFilePath.getFileSystem();
    try (final FSDataOutputStream fsDataOutputStream = fileSystem.create(outputFilePath, FileSystem.WriteMode.OVERWRITE)) {
        return writeRandomContent(fsDataOutputStream, length);
    }
}
Also used : Path(org.apache.flink.core.fs.Path) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream)

Example 19 with FSDataOutputStream

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

the class AbstractHadoopFileSystemITTest method testSimpleFileWriteAndRead.

@Test
public void testSimpleFileWriteAndRead() throws Exception {
    final String testLine = "Hello Upload!";
    final Path path = new Path(basePath, "test.txt");
    try {
        try (FSDataOutputStream out = fs.create(path, FileSystem.WriteMode.OVERWRITE);
            OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
            writer.write(testLine);
        }
        // just in case, wait for the path to exist
        checkPathExistence(path, true, consistencyToleranceNS);
        try (FSDataInputStream in = fs.open(path);
            InputStreamReader ir = new InputStreamReader(in, StandardCharsets.UTF_8);
            BufferedReader reader = new BufferedReader(ir)) {
            String line = reader.readLine();
            assertEquals(testLine, line);
        }
    } finally {
        fs.delete(path, false);
    }
    checkPathExistence(path, false, consistencyToleranceNS);
}
Also used : Path(org.apache.flink.core.fs.Path) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) OutputStreamWriter(java.io.OutputStreamWriter) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 20 with FSDataOutputStream

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

the class StateChangeFsUploader method wrap.

private OutputStreamWithPos wrap(FSDataOutputStream fsStream) throws IOException {
    StreamCompressionDecorator instance = compression ? SnappyStreamCompressionDecorator.INSTANCE : UncompressedStreamCompressionDecorator.INSTANCE;
    OutputStream compressed = compression ? instance.decorateWithCompression(fsStream) : fsStream;
    return new OutputStreamWithPos(new BufferedOutputStream(compressed, bufferSize));
}
Also used : UncompressedStreamCompressionDecorator(org.apache.flink.runtime.state.UncompressedStreamCompressionDecorator) SnappyStreamCompressionDecorator(org.apache.flink.runtime.state.SnappyStreamCompressionDecorator) StreamCompressionDecorator(org.apache.flink.runtime.state.StreamCompressionDecorator) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)31 Path (org.apache.flink.core.fs.Path)24 FileSystem (org.apache.flink.core.fs.FileSystem)17 Test (org.junit.Test)16 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)10 IOException (java.io.IOException)8 File (java.io.File)7 Random (java.util.Random)5 LocalFileSystem (org.apache.flink.core.fs.local.LocalFileSystem)5 InputStream (java.io.InputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 FileStatus (org.apache.flink.core.fs.FileStatus)4 DataOutputStream (java.io.DataOutputStream)3 Map (java.util.Map)3 LocalDataOutputStream (org.apache.flink.core.fs.local.LocalDataOutputStream)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Arrays (java.util.Arrays)2