Search in sources :

Example 21 with FSDataInputStream

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

the class StateChangeFormat method read.

@Override
public CloseableIterator<StateChange> read(StreamStateHandle handle, long offset) throws IOException {
    FSDataInputStream stream = handle.openInputStream();
    DataInputViewStreamWrapper input = wrap(stream);
    if (stream.getPos() != offset) {
        LOG.debug("seek from {} to {}", stream.getPos(), offset);
        input.skipBytesToRead((int) offset);
    }
    return new CloseableIterator<StateChange>() {

        int numUnreadGroups = input.readInt();

        int numLeftInGroup = numUnreadGroups-- == 0 ? 0 : input.readInt();

        int keyGroup = numLeftInGroup == 0 ? 0 : input.readInt();

        @Override
        public boolean hasNext() {
            advance();
            return numLeftInGroup > 0;
        }

        private void advance() {
            if (numLeftInGroup == 0 && numUnreadGroups > 0) {
                numUnreadGroups--;
                try {
                    numLeftInGroup = input.readInt();
                    keyGroup = input.readInt();
                } catch (IOException e) {
                    ExceptionUtils.rethrow(e);
                }
            }
        }

        @Override
        public StateChange next() {
            advance();
            if (numLeftInGroup == 0) {
                throw new NoSuchElementException();
            }
            numLeftInGroup--;
            try {
                return readChange();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        private StateChange readChange() throws IOException {
            int size = input.readInt();
            byte[] bytes = new byte[size];
            IOUtils.readFully(input, bytes, 0, size);
            return new StateChange(keyGroup, bytes);
        }

        @Override
        public void close() throws Exception {
            LOG.trace("close {}", stream);
            stream.close();
        }
    };
}
Also used : CloseableIterator(org.apache.flink.util.CloseableIterator) StateChange(org.apache.flink.runtime.state.changelog.StateChange) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) IOException(java.io.IOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) NoSuchElementException(java.util.NoSuchElementException)

Example 22 with FSDataInputStream

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

the class BinaryInputFormat method createStatistics.

/**
 * Fill in the statistics. The last modification time and the total input size are prefilled.
 *
 * @param files The files that are associated with this block input format.
 * @param stats The pre-filled statistics.
 */
protected SequentialStatistics createStatistics(List<FileStatus> files, FileBaseStatistics stats) throws IOException {
    if (files.isEmpty()) {
        return null;
    }
    BlockInfo blockInfo = new BlockInfo();
    long totalCount = 0;
    for (FileStatus file : files) {
        // invalid file
        if (file.getLen() < blockInfo.getInfoSize()) {
            continue;
        }
        FileSystem fs = file.getPath().getFileSystem();
        try (FSDataInputStream fdis = fs.open(file.getPath(), blockInfo.getInfoSize())) {
            fdis.seek(file.getLen() - blockInfo.getInfoSize());
            blockInfo.read(new DataInputViewStreamWrapper(fdis));
            totalCount += blockInfo.getAccumulatedRecordCount();
        }
    }
    final float avgWidth = totalCount == 0 ? 0 : ((float) stats.getTotalInputSize() / totalCount);
    return new SequentialStatistics(stats.getLastModificationTime(), stats.getTotalInputSize(), avgWidth, totalCount);
}
Also used : FileStatus(org.apache.flink.core.fs.FileStatus) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 23 with FSDataInputStream

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

the class AvroParquetRecordFormatTest method restoreReader.

private <T> StreamFormat.Reader<T> restoreReader(AvroParquetRecordFormat<T> format, Configuration config, Path filePath, long restoredOffset, long splitOffset, long splitLength) throws IOException {
    final FileSystem fileSystem = filePath.getFileSystem();
    final FileStatus fileStatus = fileSystem.getFileStatus(filePath);
    final FSDataInputStream inputStream = fileSystem.open(filePath);
    if (format.isSplittable()) {
        inputStream.seek(splitOffset);
    } else {
        inputStream.seek(0);
        checkArgument(splitLength == fileStatus.getLen());
    }
    return format.restoreReader(config, inputStream, restoredOffset, fileStatus.getLen(), splitOffset + splitLength);
}
Also used : FileStatus(org.apache.flink.core.fs.FileStatus) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream)

Example 24 with FSDataInputStream

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

the class FullSnapshotRestoreOperation method restoreKeyGroupsInStateHandle.

private SavepointRestoreResult restoreKeyGroupsInStateHandle(@Nonnull KeyGroupsStateHandle keyedStateHandle) throws IOException, StateMigrationException {
    FSDataInputStream currentStateHandleInStream = keyedStateHandle.openInputStream();
    KeyedBackendSerializationProxy<K> serializationProxy = readMetaData(new DataInputViewStreamWrapper(currentStateHandleInStream));
    KeyGroupsIterator groupsIterator = new KeyGroupsIterator(keyGroupRange, keyedStateHandle, currentStateHandleInStream, serializationProxy.isUsingKeyGroupCompression() ? SnappyStreamCompressionDecorator.INSTANCE : UncompressedStreamCompressionDecorator.INSTANCE);
    return new SavepointRestoreResult(serializationProxy.getStateMetaInfoSnapshots(), groupsIterator);
}
Also used : FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) END_OF_KEY_GROUP_MARK(org.apache.flink.runtime.state.FullSnapshotUtil.END_OF_KEY_GROUP_MARK) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 25 with FSDataInputStream

use of org.apache.flink.core.fs.FSDataInputStream 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)

Aggregations

FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)58 Test (org.junit.Test)21 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)14 IOException (java.io.IOException)12 FileSystem (org.apache.flink.core.fs.FileSystem)12 Path (org.apache.flink.core.fs.Path)10 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 FileStatus (org.apache.flink.core.fs.FileStatus)6 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 File (java.io.File)4 DataInputView (org.apache.flink.core.memory.DataInputView)4 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)4 ObjectInputStream (java.io.ObjectInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 LocalFileSystem (org.apache.flink.core.fs.local.LocalFileSystem)2