hailin0 commented on code in PR #10142:
URL: https://github.com/apache/seatunnel/pull/10142#discussion_r2610712093
##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/AbstractReadStrategy.java:
##########
@@ -94,6 +97,8 @@ public abstract class AbstractReadStrategy implements
ReadStrategy {
protected Date fileModifiedEndDate;
protected String fileBasePath;
+ protected boolean enableSplitFile;
Review Comment:
```suggestion
protected boolean enableFileSplit;
```
##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/config/FileBaseOptions.java:
##########
@@ -150,4 +150,17 @@ public class FileBaseOptions extends
ConnectorCommonOptions {
.enumType(ArchiveCompressFormat.class)
.defaultValue(ArchiveCompressFormat.NONE)
.withDescription("Archive compression codec");
+
+ public static final Option<Boolean> ENABLE_SPLIT_FILE =
+ Options.key("enable_split_file")
+ .booleanType()
+ .defaultValue(false)
+ .withDescription("Turn on the file splitting function, the
default is false");
+
+ public static final Option<Long> SPLIT_SIZE =
+ Options.key("split_size")
Review Comment:
```suggestion
public static final Option<Long> FILE_SPLIT_SIZE =
Options.key("file_split_size")
```
##########
seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/LocalFileSource.java:
##########
@@ -18,14 +18,32 @@
package org.apache.seatunnel.connectors.seatunnel.file.local.source;
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import
org.apache.seatunnel.connectors.seatunnel.file.config.FileBaseSourceOptions;
import org.apache.seatunnel.connectors.seatunnel.file.config.FileSystemType;
import
org.apache.seatunnel.connectors.seatunnel.file.local.source.config.MultipleTableLocalFileSourceConfig;
+import
org.apache.seatunnel.connectors.seatunnel.file.local.source.split.LocalFileAccordingToSplitSizeSplitStrategy;
import
org.apache.seatunnel.connectors.seatunnel.file.source.BaseMultipleTableFileSource;
+import
org.apache.seatunnel.connectors.seatunnel.file.source.split.DefaultFileSplitStrategy;
+
+import static
org.apache.seatunnel.connectors.seatunnel.file.config.FileBaseSourceOptions.DEFAULT_ROW_DELIMITER;
public class LocalFileSource extends BaseMultipleTableFileSource {
public LocalFileSource(ReadonlyConfig readonlyConfig) {
- super(new MultipleTableLocalFileSourceConfig(readonlyConfig));
+ super(
+ new MultipleTableLocalFileSourceConfig(readonlyConfig),
+ readonlyConfig.get(FileBaseSourceOptions.ENABLE_SPLIT_FILE)
+ ? new LocalFileAccordingToSplitSizeSplitStrategy(
+
readonlyConfig.get(FileBaseSourceOptions.ROW_DELIMITER) == null
+ ? DEFAULT_ROW_DELIMITER
+ :
readonlyConfig.get(FileBaseSourceOptions.ROW_DELIMITER),
+
readonlyConfig.get(FileBaseSourceOptions.CSV_USE_HEADER_LINE)
+ ? 1L
+ : readonlyConfig.get(
+
FileBaseSourceOptions.SKIP_HEADER_ROW_NUMBER),
+
readonlyConfig.get(FileBaseSourceOptions.ENCODING),
+
readonlyConfig.get(FileBaseSourceOptions.SPLIT_SIZE))
+ : new DefaultFileSplitStrategy());
Review Comment:
Extract to util methods
##########
seatunnel-connectors-v2/connector-file/connector-file-local/src/test/java/org/apache/seatunnel/connectors/seatunnel/file/local/SplitFileStrategyTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.local;
+
+import
org.apache.seatunnel.connectors.seatunnel.file.local.source.split.LocalFileAccordingToSplitSizeSplitStrategy;
+import
org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import lombok.SneakyThrows;
+
+import java.net.URL;
+import java.nio.file.Paths;
+import java.util.List;
+
+public class SplitFileStrategyTest {
Review Comment:
Add escape characters to test: when the field content contains a newline
character, the line is not truncated.
e.g(csv):
```csv
1,t1,"a
b"
2,t2,"c
d"
```
##########
seatunnel-connectors-v2/connector-file/connector-file-local/src/test/java/org/apache/seatunnel/connectors/seatunnel/file/local/SplitFileStrategyTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.local;
+
+import
org.apache.seatunnel.connectors.seatunnel.file.local.source.split.LocalFileAccordingToSplitSizeSplitStrategy;
+import
org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import lombok.SneakyThrows;
+
+import java.net.URL;
+import java.nio.file.Paths;
+import java.util.List;
+
+public class SplitFileStrategyTest {
+
+ @DisabledOnOs(
+ value = OS.WINDOWS,
+ disabledReason =
+ "In the Windows environment, the newline character of the
text file is '\\r\\n', and the byte length and newline character are
inconsistent, which will cause the test case to fail.")
+ @SneakyThrows
+ @Test
+ public void testSplitNoSkipHeader() {
+ final LocalFileAccordingToSplitSizeSplitStrategy
localFileSplitStrategy =
+ new LocalFileAccordingToSplitSizeSplitStrategy("\n", 0L,
"utf-8", 100L);
+ URL url =
getClass().getClassLoader().getResource("test_split_csv_data.csv");
+ String realPath = Paths.get(url.toURI()).toString();
+ final List<FileSourceSplit> splits =
localFileSplitStrategy.split("test.table", realPath);
+ Assertions.assertEquals(2, splits.size());
Review Comment:
add line count check
##########
seatunnel-connectors-v2/connector-file/connector-file-local/src/test/java/org/apache/seatunnel/connectors/seatunnel/file/local/SplitFileStrategyTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.local;
+
+import
org.apache.seatunnel.connectors.seatunnel.file.local.source.split.LocalFileAccordingToSplitSizeSplitStrategy;
+import
org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplit;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import lombok.SneakyThrows;
+
+import java.net.URL;
+import java.nio.file.Paths;
+import java.util.List;
+
+public class SplitFileStrategyTest {
+
+ @DisabledOnOs(
+ value = OS.WINDOWS,
+ disabledReason =
+ "In the Windows environment, the newline character of the
text file is '\\r\\n', and the byte length and newline character are
inconsistent, which will cause the test case to fail.")
+ @SneakyThrows
+ @Test
+ public void testSplitNoSkipHeader() {
+ final LocalFileAccordingToSplitSizeSplitStrategy
localFileSplitStrategy =
+ new LocalFileAccordingToSplitSizeSplitStrategy("\n", 0L,
"utf-8", 100L);
+ URL url =
getClass().getClassLoader().getResource("test_split_csv_data.csv");
+ String realPath = Paths.get(url.toURI()).toString();
+ final List<FileSourceSplit> splits =
localFileSplitStrategy.split("test.table", realPath);
+ Assertions.assertEquals(2, splits.size());
+ // check split-1
+ Assertions.assertEquals(0, splits.get(0).getStart());
+ Assertions.assertEquals(105, splits.get(0).getLength());
+ // check split-2
+ Assertions.assertEquals(105, splits.get(1).getStart());
+ Assertions.assertEquals(85, splits.get(1).getLength());
+ }
+
+ @DisabledOnOs(
+ value = OS.WINDOWS,
+ disabledReason =
+ "In the Windows environment, the newline character of the
text file is '\\r\\n', and the byte length and newline character are
inconsistent, which will cause the test case to fail.")
+ @SneakyThrows
+ @Test
+ public void testSplitSkipHeader() {
+ final LocalFileAccordingToSplitSizeSplitStrategy
localFileSplitStrategy =
+ new LocalFileAccordingToSplitSizeSplitStrategy("\n", 1L,
"utf-8", 30L);
+ URL url =
getClass().getClassLoader().getResource("test_split_csv_data.csv");
+ String realPath = Paths.get(url.toURI()).toString();
+ final List<FileSourceSplit> splits =
localFileSplitStrategy.split("test.table", realPath);
+ Assertions.assertEquals(4, splits.size());
Review Comment:
add line count check
##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/config/FileBaseOptions.java:
##########
@@ -150,4 +150,17 @@ public class FileBaseOptions extends
ConnectorCommonOptions {
.enumType(ArchiveCompressFormat.class)
.defaultValue(ArchiveCompressFormat.NONE)
.withDescription("Archive compression codec");
+
+ public static final Option<Boolean> ENABLE_SPLIT_FILE =
+ Options.key("enable_split_file")
Review Comment:
```suggestion
public static final Option<Boolean> ENABLE_FILE_SPLIT =
Options.key("enable_file_split")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]