Search in sources :

Example 6 with FSDataOutputStream

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

the class AzureFileSystemBehaviorITCase method testDirectoryListing.

@Test
public void testDirectoryListing() throws Exception {
    // 30 secs
    final long deadline = System.nanoTime() + 30_000_000_000L;
    final Path directory = new Path(getBasePath() + "/testdir/");
    final FileSystem fs = directory.getFileSystem();
    // directory must not yet exist
    assertFalse(fs.exists(directory));
    try {
        // create directory
        assertTrue(fs.mkdirs(directory));
        checkPathEventualExistence(fs, directory, true, deadline);
        // directory empty
        assertEquals(0, fs.listStatus(directory).length);
        // create some files
        final int numFiles = 3;
        for (int i = 0; i < numFiles; i++) {
            Path file = new Path(directory, "/file-" + i);
            try (FSDataOutputStream out = fs.create(file, FileSystem.WriteMode.OVERWRITE);
                OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
                writer.write("hello-" + i + "\n");
            }
            // just in case, wait for the file to exist (should then also be reflected in the
            // directory's file list below)
            checkPathEventualExistence(fs, file, true, deadline);
        }
        FileStatus[] files = fs.listStatus(directory);
        assertNotNull(files);
        assertEquals(3, files.length);
        for (FileStatus status : files) {
            assertFalse(status.isDir());
        }
        // now that there are files, the directory must exist
        assertTrue(fs.exists(directory));
    } finally {
        // clean up
        fs.delete(directory, true);
    }
    // now directory must be gone
    checkPathEventualExistence(fs, directory, false, deadline);
}
Also used : Path(org.apache.flink.core.fs.Path) FileStatus(org.apache.flink.core.fs.FileStatus) FileSystem(org.apache.flink.core.fs.FileSystem) OutputStreamWriter(java.io.OutputStreamWriter) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 7 with FSDataOutputStream

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

the class AzureFileSystemBehaviorITCase method testSimpleFileWriteAndRead.

@Test
public void testSimpleFileWriteAndRead() throws Exception {
    // 30 secs
    final long deadline = System.nanoTime() + 30_000_000_000L;
    final String testLine = "Hello Upload!";
    final Path path = new Path(getBasePath() + "/test.txt");
    final FileSystem fs = path.getFileSystem();
    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
        checkPathEventualExistence(fs, path, true, deadline);
        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);
    }
    // now file must be gone
    checkPathEventualExistence(fs, path, false, deadline);
}
Also used : Path(org.apache.flink.core.fs.Path) InputStreamReader(java.io.InputStreamReader) FileSystem(org.apache.flink.core.fs.FileSystem) 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 8 with FSDataOutputStream

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

the class AbstractHadoopFileSystemITTest method testDirectoryListing.

@Test
public void testDirectoryListing() throws Exception {
    final Path directory = new Path(basePath, "testdir/");
    // directory must not yet exist
    assertFalse(fs.exists(directory));
    try {
        // create directory
        assertTrue(fs.mkdirs(directory));
        checkEmptyDirectory(directory);
        // directory empty
        assertEquals(0, fs.listStatus(directory).length);
        // create some files
        final int numFiles = 3;
        for (int i = 0; i < numFiles; i++) {
            Path file = new Path(directory, "/file-" + i);
            try (FSDataOutputStream out = fs.create(file, FileSystem.WriteMode.OVERWRITE);
                OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
                writer.write("hello-" + i + "\n");
            }
            // just in case, wait for the file to exist (should then also be reflected in the
            // directory's file list below)
            checkPathExistence(file, true, consistencyToleranceNS);
        }
        FileStatus[] files = fs.listStatus(directory);
        assertNotNull(files);
        assertEquals(3, files.length);
        for (FileStatus status : files) {
            assertFalse(status.isDir());
        }
        // now that there are files, the directory must exist
        assertTrue(fs.exists(directory));
    } finally {
        // clean up
        cleanupDirectoryWithRetry(fs, directory, consistencyToleranceNS);
    }
}
Also used : Path(org.apache.flink.core.fs.Path) FileStatus(org.apache.flink.core.fs.FileStatus) OutputStreamWriter(java.io.OutputStreamWriter) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 9 with FSDataOutputStream

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

the class TestManagedSinkCommitter method commitAdd.

private void commitAdd(Map<CatalogPartitionSpec, List<RowData>> toAdd, Map<CatalogPartitionSpec, List<Path>> managedTableFileEntries) throws IOException {
    Map<CatalogPartitionSpec, String> processedPartitions = new HashMap<>();
    for (Map.Entry<CatalogPartitionSpec, List<RowData>> entry : toAdd.entrySet()) {
        CatalogPartitionSpec partitionSpec = entry.getKey();
        String partition = processedPartitions.computeIfAbsent(partitionSpec, (spec) -> PartitionPathUtils.generatePartitionPath(new LinkedHashMap<>(spec.getPartitionSpec())));
        List<RowData> elements = entry.getValue();
        Path compactFilePath = new Path(basePath, new Path(String.format("%scompact-%s-file-0", partition, UUID.randomUUID())));
        FSDataOutputStream outputStream = compactFilePath.getFileSystem().create(compactFilePath, FileSystem.WriteMode.NO_OVERWRITE);
        for (RowData element : elements) {
            encoder.encode(element, outputStream);
        }
        outputStream.flush();
        outputStream.close();
        List<Path> fileEntries = managedTableFileEntries.get(partitionSpec);
        fileEntries.add(compactFilePath);
        managedTableFileEntries.put(partitionSpec, fileEntries);
    }
}
Also used : Path(org.apache.flink.core.fs.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) RowData(org.apache.flink.table.data.RowData) List(java.util.List) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec)

Example 10 with FSDataOutputStream

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

the class CheckpointStateOutputStreamTest method testCleanupWhenFailingCloseAndGetHandle.

/**
 * Tests that the underlying stream file is deleted if the closeAndGetHandle method fails.
 */
@Test
public void testCleanupWhenFailingCloseAndGetHandle() throws IOException {
    final Path folder = new Path(tmp.newFolder().toURI());
    final String fileName = "test_name";
    final Path filePath = new Path(folder, fileName);
    final FileSystem fs = spy(new FsWithoutRecoverableWriter((path) -> new FailingCloseStream(new File(path.getPath()))));
    FSDataOutputStream stream = createTestStream(fs, folder, fileName);
    stream.write(new byte[] { 1, 2, 3, 4, 5 });
    try {
        closeAndGetResult(stream);
        fail("Expected IOException");
    } catch (IOException ignored) {
    // expected exception
    }
    verify(fs).delete(filePath, false);
}
Also used : Path(org.apache.flink.core.fs.Path) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Arrays(java.util.Arrays) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Mockito.spy(org.mockito.Mockito.spy) CheckedThread(org.apache.flink.core.testutils.CheckedThread) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Path(org.apache.flink.core.fs.Path) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestLogger(org.apache.flink.util.TestLogger) FunctionWithException(org.apache.flink.util.function.FunctionWithException) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) LocalRecoverableWriter(org.apache.flink.core.fs.local.LocalRecoverableWriter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) LocalDataOutputStream(org.apache.flink.core.fs.local.LocalDataOutputStream) EOFException(java.io.EOFException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) FileSystem(org.apache.flink.core.fs.FileSystem) Assert.assertFalse(org.junit.Assert.assertFalse) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

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