Search in sources :

Example 21 with Path

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

the class FileStateBackendTest method testStateOutputStream.

@Test
public void testStateOutputStream() {
    URI basePath = randomHdfsFileUri();
    try {
        FsStateBackend backend = CommonTestUtils.createCopySerializable(new FsStateBackend(basePath, 15));
        JobID jobId = new JobID();
        CheckpointStreamFactory streamFactory = backend.createStreamFactory(jobId, "test_op");
        // we know how FsCheckpointStreamFactory is implemented so we know where it
        // will store checkpoints
        Path checkpointPath = new Path(new Path(basePath), jobId.toString());
        byte[] state1 = new byte[1274673];
        byte[] state2 = new byte[1];
        byte[] state3 = new byte[0];
        byte[] state4 = new byte[177];
        Random rnd = new Random();
        rnd.nextBytes(state1);
        rnd.nextBytes(state2);
        rnd.nextBytes(state3);
        rnd.nextBytes(state4);
        long checkpointId = 97231523452L;
        CheckpointStreamFactory.CheckpointStateOutputStream stream1 = streamFactory.createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());
        CheckpointStreamFactory.CheckpointStateOutputStream stream2 = streamFactory.createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());
        CheckpointStreamFactory.CheckpointStateOutputStream stream3 = streamFactory.createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());
        stream1.write(state1);
        stream2.write(state2);
        stream3.write(state3);
        FileStateHandle handle1 = (FileStateHandle) stream1.closeAndGetHandle();
        ByteStreamStateHandle handle2 = (ByteStreamStateHandle) stream2.closeAndGetHandle();
        ByteStreamStateHandle handle3 = (ByteStreamStateHandle) stream3.closeAndGetHandle();
        // use with try-with-resources
        FileStateHandle handle4;
        try (CheckpointStreamFactory.CheckpointStateOutputStream stream4 = streamFactory.createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis())) {
            stream4.write(state4);
            handle4 = (FileStateHandle) stream4.closeAndGetHandle();
        }
        // close before accessing handle
        CheckpointStreamFactory.CheckpointStateOutputStream stream5 = streamFactory.createCheckpointStateOutputStream(checkpointId, System.currentTimeMillis());
        stream5.write(state4);
        stream5.close();
        try {
            stream5.closeAndGetHandle();
            fail();
        } catch (IOException e) {
        // uh-huh
        }
        validateBytesInStream(handle1.openInputStream(), state1);
        handle1.discardState();
        assertFalse(isDirectoryEmpty(checkpointPath));
        ensureFileDeleted(handle1.getFilePath());
        validateBytesInStream(handle2.openInputStream(), state2);
        handle2.discardState();
        // stream 3 has zero bytes, so it should not return anything
        assertNull(handle3);
        validateBytesInStream(handle4.openInputStream(), state4);
        handle4.discardState();
        assertTrue(isDirectoryEmpty(checkpointPath));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Path(org.apache.flink.core.fs.Path) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) FileStateHandle(org.apache.flink.runtime.state.filesystem.FileStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Random(java.util.Random) FsStateBackend(org.apache.flink.runtime.state.filesystem.FsStateBackend) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 22 with Path

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

the class FsNegativeRunningJobsRegistryTest method createHDFS.

// ------------------------------------------------------------------------
//  startup / shutdown
// ------------------------------------------------------------------------
@BeforeClass
public static void createHDFS() throws Exception {
    final File tempDir = TEMP_DIR.newFolder();
    Configuration hdConf = new Configuration();
    hdConf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, tempDir.getAbsolutePath());
    MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(hdConf);
    HDFS_CLUSTER = builder.build();
    HDFS_ROOT_PATH = new Path("hdfs://" + HDFS_CLUSTER.getURI().getHost() + ":" + HDFS_CLUSTER.getNameNodePort() + "/");
}
Also used : Path(org.apache.flink.core.fs.Path) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 23 with Path

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

the class HDFSTest method testAvroOut.

@Test
public void testAvroOut() {
    String type = "one";
    AvroOutputFormat<String> avroOut = new AvroOutputFormat<String>(String.class);
    org.apache.hadoop.fs.Path result = new org.apache.hadoop.fs.Path(hdfsURI + "/avroTest");
    avroOut.setOutputFilePath(new Path(result.toString()));
    avroOut.setWriteMode(FileSystem.WriteMode.NO_OVERWRITE);
    avroOut.setOutputDirectoryMode(FileOutputFormat.OutputDirectoryMode.ALWAYS);
    try {
        avroOut.open(0, 2);
        avroOut.writeRecord(type);
        avroOut.close();
        avroOut.open(1, 2);
        avroOut.writeRecord(type);
        avroOut.close();
        assertTrue("No result file present", hdfs.exists(result));
        FileStatus[] files = hdfs.listStatus(result);
        Assert.assertEquals(2, files.length);
        for (FileStatus file : files) {
            assertTrue("1.avro".equals(file.getPath().getName()) || "2.avro".equals(file.getPath().getName()));
        }
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Path(org.apache.flink.core.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) AvroOutputFormat(org.apache.flink.api.java.io.AvroOutputFormat) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with Path

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

the class CliFrontend method disposeSavepoint.

/**
	 * Sends a {@link org.apache.flink.runtime.messages.JobManagerMessages.DisposeSavepoint}
	 * message to the job manager.
	 */
private int disposeSavepoint(SavepointOptions options) {
    try {
        String savepointPath = options.getSavepointPath();
        if (savepointPath == null) {
            throw new IllegalArgumentException("Missing required argument: savepoint path. " + "Usage: bin/flink savepoint -d <savepoint-path>");
        }
        String jarFile = options.getJarFilePath();
        ActorGateway jobManager = getJobManagerGateway(options);
        List<BlobKey> blobKeys = null;
        if (jarFile != null) {
            logAndSysout("Disposing savepoint '" + savepointPath + "' with JAR " + jarFile + ".");
            List<File> libs = null;
            try {
                libs = PackagedProgram.extractContainedLibraries(new File(jarFile).toURI().toURL());
                if (!libs.isEmpty()) {
                    List<Path> libPaths = new ArrayList<>(libs.size());
                    for (File f : libs) {
                        libPaths.add(new Path(f.toURI()));
                    }
                    logAndSysout("Uploading JAR files.");
                    LOG.debug("JAR files: " + libPaths);
                    blobKeys = BlobClient.uploadJarFiles(jobManager, clientTimeout, config, libPaths);
                    LOG.debug("Blob keys: " + blobKeys.toString());
                }
            } finally {
                if (libs != null) {
                    PackagedProgram.deleteExtractedLibraries(libs);
                }
            }
        } else {
            logAndSysout("Disposing savepoint '" + savepointPath + "'.");
        }
        Object msg = new DisposeSavepoint(savepointPath);
        Future<Object> response = jobManager.ask(msg, clientTimeout);
        Object result;
        try {
            logAndSysout("Waiting for response...");
            result = Await.result(response, clientTimeout);
        } catch (Exception e) {
            throw new Exception("Disposing the savepoint with path" + savepointPath + " failed.", e);
        }
        if (result.getClass() == JobManagerMessages.getDisposeSavepointSuccess().getClass()) {
            logAndSysout("Savepoint '" + savepointPath + "' disposed.");
            return 0;
        } else if (result instanceof DisposeSavepointFailure) {
            DisposeSavepointFailure failure = (DisposeSavepointFailure) result;
            if (failure.cause() instanceof ClassNotFoundException) {
                throw new ClassNotFoundException("Savepoint disposal failed, because of a " + "missing class. This is most likely caused by a custom state " + "instance, which cannot be disposed without the user code class " + "loader. Please provide the program jar with which you have created " + "the savepoint via -j <JAR> for disposal.", failure.cause().getCause());
            } else {
                throw failure.cause();
            }
        } else {
            throw new IllegalStateException("Unknown JobManager response of type " + result.getClass());
        }
    } catch (Throwable t) {
        return handleError(t);
    }
}
Also used : Path(org.apache.flink.core.fs.Path) ArrayList(java.util.ArrayList) ProgramInvocationException(org.apache.flink.client.program.ProgramInvocationException) ProgramMissingJobException(org.apache.flink.client.program.ProgramMissingJobException) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) ProgramParametrizationException(org.apache.flink.client.program.ProgramParametrizationException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) CliArgsException(org.apache.flink.client.cli.CliArgsException) IOException(java.io.IOException) DisposeSavepointFailure(org.apache.flink.runtime.messages.JobManagerMessages.DisposeSavepointFailure) BlobKey(org.apache.flink.runtime.blob.BlobKey) DisposeSavepoint(org.apache.flink.runtime.messages.JobManagerMessages.DisposeSavepoint) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) File(java.io.File)

Example 25 with Path

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

the class AvroExternalJarProgram method main.

//	public static void main(String[] args) throws Exception {
//		String testDataFile = new File("src/test/resources/testdata.avro").getAbsolutePath();
//		writeTestData(new File(testDataFile), 50);
//	}
public static void main(String[] args) throws Exception {
    String inputPath = args[0];
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    DataSet<MyUser> input = env.createInput(new AvroInputFormat<MyUser>(new Path(inputPath), MyUser.class));
    DataSet<Tuple2<String, MyUser>> result = input.map(new NameExtractor()).groupBy(0).reduce(new NameGrouper());
    result.output(new DiscardingOutputFormat<Tuple2<String, MyUser>>());
    env.execute();
}
Also used : Path(org.apache.flink.core.fs.Path) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Tuple2(org.apache.flink.api.java.tuple.Tuple2)

Aggregations

Path (org.apache.flink.core.fs.Path)590 Test (org.junit.Test)320 File (java.io.File)178 Configuration (org.apache.flink.configuration.Configuration)101 IOException (java.io.IOException)91 FileSystem (org.apache.flink.core.fs.FileSystem)87 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)72 ArrayList (java.util.ArrayList)64 HashMap (java.util.HashMap)40 FileOutputStream (java.io.FileOutputStream)38 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)36 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)35 List (java.util.List)33 Map (java.util.Map)29 JobID (org.apache.flink.api.common.JobID)29 OutputStreamWriter (java.io.OutputStreamWriter)27 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)27 FileStatus (org.apache.flink.core.fs.FileStatus)26 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)25 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)21