-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Feature][Connector-V2][HdfsFile] Support true large-file split for parallel read #10332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yzeng1618
wants to merge
1
commit into
apache:dev
Choose a base branch
from
yzeng1618:dev-hdfs-split
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,138
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...onnectors/seatunnel/file/hdfs/source/split/HdfsFileAccordingToSplitSizeSplitStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.seatunnel.connectors.seatunnel.file.hdfs.source.split; | ||
|
|
||
| import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException; | ||
| import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf; | ||
| import org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorErrorCode; | ||
| import org.apache.seatunnel.connectors.seatunnel.file.hadoop.HadoopFileSystemProxy; | ||
| import org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplit; | ||
| import org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSplitStrategy; | ||
|
|
||
| import org.apache.hadoop.fs.FSDataInputStream; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class HdfsFileAccordingToSplitSizeSplitStrategy implements FileSplitStrategy, Closeable { | ||
|
|
||
| private static final int BUFFER_SIZE = 64 * 1024; | ||
|
|
||
| private final HadoopFileSystemProxy hadoopFileSystemProxy; | ||
| private final long skipHeaderRowNumber; | ||
| private final long splitSize; | ||
| private final byte[] delimiterBytes; | ||
|
|
||
| public HdfsFileAccordingToSplitSizeSplitStrategy( | ||
| HadoopConf hadoopConf, | ||
| String rowDelimiter, | ||
| long skipHeaderRowNumber, | ||
| String encodingName, | ||
| long splitSize) { | ||
| if (splitSize <= 0) { | ||
| throw new SeaTunnelRuntimeException( | ||
| FileConnectorErrorCode.FILE_SPLIT_SIZE_ILLEGAL, | ||
| "SplitSizeBytes must be greater than 0"); | ||
| } | ||
| this.hadoopFileSystemProxy = new HadoopFileSystemProxy(hadoopConf); | ||
| this.skipHeaderRowNumber = skipHeaderRowNumber; | ||
| this.splitSize = splitSize; | ||
| this.delimiterBytes = rowDelimiter.getBytes(Charset.forName(encodingName)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<FileSourceSplit> split(String tableId, String filePath) { | ||
| List<FileSourceSplit> splits = new ArrayList<>(); | ||
| long fileSize = safeGetFileSize(filePath); | ||
| if (fileSize == 0) { | ||
| return splits; | ||
| } | ||
| try (FSDataInputStream input = hadoopFileSystemProxy.getInputStream(filePath)) { | ||
| long currentStart = 0; | ||
| if (skipHeaderRowNumber > 0) { | ||
| currentStart = skipLinesUsingBuffer(input, skipHeaderRowNumber); | ||
| } | ||
| while (currentStart < fileSize) { | ||
| long tentativeEnd = currentStart + splitSize; | ||
| if (tentativeEnd >= fileSize) { | ||
| splits.add( | ||
| new FileSourceSplit( | ||
| tableId, filePath, currentStart, fileSize - currentStart)); | ||
| break; | ||
| } | ||
| long actualEnd = findNextDelimiterWithSeek(input, tentativeEnd, fileSize); | ||
| if (actualEnd <= currentStart) { | ||
| actualEnd = tentativeEnd; | ||
| } | ||
| splits.add( | ||
| new FileSourceSplit( | ||
| tableId, filePath, currentStart, actualEnd - currentStart)); | ||
| currentStart = actualEnd; | ||
| } | ||
| return splits; | ||
| } catch (IOException e) { | ||
| throw new SeaTunnelRuntimeException(FileConnectorErrorCode.FILE_READ_FAILED, e); | ||
| } | ||
| } | ||
|
|
||
| private long safeGetFileSize(String filePath) { | ||
| try { | ||
| return hadoopFileSystemProxy.getFileStatus(filePath).getLen(); | ||
| } catch (IOException e) { | ||
| throw new SeaTunnelRuntimeException(FileConnectorErrorCode.FILE_READ_FAILED, e); | ||
| } | ||
| } | ||
|
|
||
| private long skipLinesUsingBuffer(FSDataInputStream input, long skipLines) throws IOException { | ||
| input.seek(0); | ||
| byte[] buffer = new byte[BUFFER_SIZE]; | ||
| int matched = 0; | ||
| long lines = 0; | ||
| long pos = 0; | ||
| int n; | ||
| while ((n = input.read(buffer)) != -1) { | ||
| for (int i = 0; i < n; i++) { | ||
| pos++; | ||
| if (buffer[i] == delimiterBytes[matched]) { | ||
| matched++; | ||
| if (matched == delimiterBytes.length) { | ||
| matched = 0; | ||
| lines++; | ||
| if (lines >= skipLines) { | ||
| return pos; | ||
| } | ||
| } | ||
| } else { | ||
| matched = buffer[i] == delimiterBytes[0] ? 1 : 0; | ||
| } | ||
| } | ||
| } | ||
| return pos; | ||
| } | ||
|
|
||
| private long findNextDelimiterWithSeek(FSDataInputStream input, long startPos, long fileSize) | ||
| throws IOException { | ||
| long scanStart = Math.max(0, startPos - (delimiterBytes.length - 1)); | ||
| input.seek(scanStart); | ||
| byte[] buffer = new byte[BUFFER_SIZE]; | ||
| int matched = 0; | ||
| long pos = scanStart; | ||
| int n; | ||
| while ((n = input.read(buffer)) != -1) { | ||
| for (int i = 0; i < n; i++) { | ||
| pos++; | ||
| if (buffer[i] == delimiterBytes[matched]) { | ||
| matched++; | ||
| if (matched == delimiterBytes.length) { | ||
| long endPos = pos; | ||
| if (endPos >= startPos) { | ||
| return endPos; | ||
| } | ||
| matched = 0; | ||
| } | ||
| } else { | ||
| matched = buffer[i] == delimiterBytes[0] ? 1 : 0; | ||
| } | ||
| } | ||
| } | ||
| return Math.min(fileSize, pos); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| hadoopFileSystemProxy.close(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1、Modify
AccordingToSplitSizeSplitStrategyto introduceHadoopFileSystemProxy2、Delete
LocalFileAccordingToSplitSizeSplitStrategyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so you still suggest reusing the base code as much as possible? My main concern is that it might affect the logic of the localfile code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the sharding logic should be maintained uniformly in the base, and there should not be a lot of code redundancy. It is also convenient to add this function to file system connectors such as S3 and OSS.