[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-20 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1236234583


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/BlockSplittingRecursiveAllDirEnumerator.java:
##
@@ -0,0 +1,165 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.connector.file.src.compression.StandardDeCompressors;
+import org.apache.flink.core.fs.BlockLocation;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively except the
+ * hidden directories, and creates a separate split for each file block.
+ *
+ * Please note that file blocks are only exposed by some file systems, such 
as HDFS. File systems
+ * that do not expose block information will not create multiple file splits 
per file, but keep the
+ * files as one source split.
+ *
+ * Files with suffixes corresponding to known compression formats (for 
example '.gzip', '.bz2',
+ * ...) will not be split. See {@link StandardDeCompressors} for a list of 
known formats and
+ * suffixes.
+ *
+ * Compared to {@link BlockSplittingRecursiveEnumerator}, this enumerator 
will enumerate all
+ * files even through its parent directory is filtered out by the file filter.
+ */
+public class BlockSplittingRecursiveAllDirEnumerator extends 
NonSplittingRecursiveAllDirEnumerator {

Review Comment:
   `BlockSplittingRecursiveAllDirEnumerator` need to reuse the method 
`addSplitsForPath` in `NonSplittingRecursiveAllDirEnumerator`, not 
`BlockSplittingRecursiveEnumerator`.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-09 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1223913567


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,102 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * The default instantiation of this enumerator filters files with the 
common hidden file
+ * prefixes '.' and '_'. A custom file filter can be specified.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+/** The custom filter predicate to filter out unwanted files. */
+private final Predicate fileFilter;
+
+private final RegexFileFilter regexFileFilter;

Review Comment:
   I think it is not suitable when the regex is complex. I think I should 
remove the `connectBasePathAndRegex ` and use the actual value passed by 
`source.regex-pattern`.
   
   But I still think we should keep the table path as the base dir to start to 
search files.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-09 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1223912208


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,102 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * The default instantiation of this enumerator filters files with the 
common hidden file
+ * prefixes '.' and '_'. A custom file filter can be specified.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+/** The custom filter predicate to filter out unwanted files. */
+private final Predicate fileFilter;
+
+private final RegexFileFilter regexFileFilter;

Review Comment:
   Because it does not satisfy some situations. 
   For example, we have files as follows.
   ```
   /
|- dir
|- test
  |- test.java
  |- innerDir
|- test.java
   ```
   We want to read these two `test.java` under the directory `/dir`. Then we 
will use the final regex `/dir/.*/test.java`.
   If we reuse `NonSplittingRecursiveEnumerator ` + `fileFilter`, it will skip 
the directory `/dir/test` because its path does not match the regex 
`/dir/.*/test.java`. And it will lose the files.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-09 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1223886235


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/table/FileSystemTableSource.java:
##
@@ -269,10 +271,35 @@ private SourceProvider 
createSourceProvider(BulkFormat
 tableOptions
 
.getOptional(FileSystemConnectorOptions.SOURCE_MONITOR_INTERVAL)
 .ifPresent(fileSourceBuilder::monitorContinuously);
+tableOptions
+.getOptional(FileSystemConnectorOptions.SOURCE_REGEX_PATTERN)
+.ifPresent(
+s -> {
+String regexPath = 
connectBasePathAndRegex(path.getPath(), s);
+fileSourceBuilder.setFileEnumerator(
+bulkFormat.isSplittable()
+? () -> new 
BlockSplittingRegexEnumerator(regexPath)
+: () -> new 
NonSplittingRegexEnumerator(regexPath));
+});
 
 return SourceProvider.of(fileSourceBuilder.build());
 }
 
+private String connectBasePathAndRegex(String basePath, String regex) {
+StringBuilder result = new StringBuilder();
+result.append(basePath);
+if (!basePath.endsWith(Path.SEPARATOR)) {
+result.append(Path.SEPARATOR);
+}
+int startIndex = 0;
+while (startIndex < regex.length()

Review Comment:
   @luoyuxia Using a base path will help to reduce the search time. 
   If we only provide `source.regex-pattern` , we have to search files from 
root path `/` when `source.regex-pattern` = `'/dir/t/.*/inner/.*'`.
   If we provide base dir and `source.regex-pattern`, we will start to search 
files under the base dir `/dir/t/` when base dir = `'/dir/t'` and 
`source.regex-pattern` = `'.*/inner/.*'`.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222677339


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * This enumerator filters out files with the common hidden file prefixes 
'.' and '_'.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+private final Predicate fileFilter;

Review Comment:
   I think this custom filter may not be exposed to the table API. We can only 
use it by the DataStream API.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222654083


##
flink-connectors/flink-connector-files/src/test/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumeratorTest.java:
##
@@ -0,0 +1,247 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.connector.file.src.testutils.TestingFileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.assertSplitsEqual;
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.toPaths;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Unit tests for the {@link NonSplittingRegexEnumerator}. */
+public class NonSplittingRegexEnumeratorTest {
+/**
+ * Testing file system reference, to be cleaned up in an @After method. 
That way it also gets
+ * cleaned up on a test failure, without needing finally clauses in every 
test.
+ */
+protected TestingFileSystem testFs;
+
+@AfterEach
+void unregisterTestFs() throws Exception {
+if (testFs != null) {
+testFs.unregister();
+}
+}
+
+// 
+
+@Test
+void testIncludeSingleFile() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherfile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nested/file.out");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits)).containsExactlyInAnyOrder(testPaths[1]);
+}
+
+@Test
+void testIncludeFilesFromRegexDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nest.[a-z]");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits))
+.containsExactlyInAnyOrder(Arrays.copyOfRange(testPaths, 1, 
testPaths.length));
+}
+
+@Test
+void testIncludeSingleFileFromMultiDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt"),
+new Path("testfs:///dir/anotherNested/file.out"),
+new Path("testfs:///dir/anotherNested/nested/file.out"),
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+

[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222654083


##
flink-connectors/flink-connector-files/src/test/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumeratorTest.java:
##
@@ -0,0 +1,247 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.connector.file.src.testutils.TestingFileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.assertSplitsEqual;
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.toPaths;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Unit tests for the {@link NonSplittingRegexEnumerator}. */
+public class NonSplittingRegexEnumeratorTest {
+/**
+ * Testing file system reference, to be cleaned up in an @After method. 
That way it also gets
+ * cleaned up on a test failure, without needing finally clauses in every 
test.
+ */
+protected TestingFileSystem testFs;
+
+@AfterEach
+void unregisterTestFs() throws Exception {
+if (testFs != null) {
+testFs.unregister();
+}
+}
+
+// 
+
+@Test
+void testIncludeSingleFile() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherfile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nested/file.out");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits)).containsExactlyInAnyOrder(testPaths[1]);
+}
+
+@Test
+void testIncludeFilesFromRegexDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nest.[a-z]");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits))
+.containsExactlyInAnyOrder(Arrays.copyOfRange(testPaths, 1, 
testPaths.length));
+}
+
+@Test
+void testIncludeSingleFileFromMultiDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt"),
+new Path("testfs:///dir/anotherNested/file.out"),
+new Path("testfs:///dir/anotherNested/nested/file.out"),
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+

[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222654083


##
flink-connectors/flink-connector-files/src/test/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumeratorTest.java:
##
@@ -0,0 +1,247 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.connector.file.src.testutils.TestingFileSystem;
+import org.apache.flink.core.fs.Path;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.assertSplitsEqual;
+import static 
org.apache.flink.connector.file.src.enumerate.NonSplittingRecursiveEnumeratorTest.toPaths;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Unit tests for the {@link NonSplittingRegexEnumerator}. */
+public class NonSplittingRegexEnumeratorTest {
+/**
+ * Testing file system reference, to be cleaned up in an @After method. 
That way it also gets
+ * cleaned up on a test failure, without needing finally clauses in every 
test.
+ */
+protected TestingFileSystem testFs;
+
+@AfterEach
+void unregisterTestFs() throws Exception {
+if (testFs != null) {
+testFs.unregister();
+}
+}
+
+// 
+
+@Test
+void testIncludeSingleFile() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherfile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nested/file.out");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits)).containsExactlyInAnyOrder(testPaths[1]);
+}
+
+@Test
+void testIncludeFilesFromRegexDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt")
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+createEnumerator(baseDIr.getPath() + "/nest.[a-z]");
+final Collection splits =
+enumerator.enumerateSplits(new Path[] {baseDIr}, 1);
+
+assertThat(toPaths(splits))
+.containsExactlyInAnyOrder(Arrays.copyOfRange(testPaths, 1, 
testPaths.length));
+}
+
+@Test
+void testIncludeSingleFileFromMultiDirectory() throws Exception {
+final Path[] testPaths =
+new Path[] {
+new Path("testfs:///dir/file1"),
+new Path("testfs:///dir/nested/file.out"),
+new Path("testfs:///dir/nested/anotherFile.txt"),
+new 
Path("testfs:///dir/nested/nested/doubleNestedFile.txt"),
+new Path("testfs:///dir/anotherNested/file.out"),
+new Path("testfs:///dir/anotherNested/nested/file.out"),
+};
+testFs = TestingFileSystem.createWithFiles("testfs", testPaths);
+testFs.register();
+
+Path baseDIr = new Path("testfs:///dir");
+final NonSplittingRegexEnumerator enumerator =
+

[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222617285


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * This enumerator filters out files with the common hidden file prefixes 
'.' and '_'.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+private final Predicate fileFilter;

Review Comment:
   I will add some comments about them. 



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222616580


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * This enumerator filters out files with the common hidden file prefixes 
'.' and '_'.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+private final Predicate fileFilter;
+private final RegexFileFilter regexFileFilter;
+
+public NonSplittingRegexEnumerator(String pathPattern) {
+this(pathPattern, new DefaultFileFilter());
+}
+
+public NonSplittingRegexEnumerator(String pathPattern, Predicate 
fileFilter) {
+this.regexFileFilter = new RegexFileFilter(pathPattern);
+this.fileFilter = fileFilter;
+}
+
+@Override
+public Collection enumerateSplits(Path[] paths, int 
minDesiredSplits)
+throws IOException {
+final ArrayList splits = new ArrayList<>();
+for (Path path : paths) {
+final FileSystem fs = path.getFileSystem();
+final FileStatus status = fs.getFileStatus(path);
+addSplitsForPath(status, fs, splits, regexFileFilter);

Review Comment:
   It aims to matching a directory and handling all files under the matched 
directory.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [flink] ruanhang1993 commented on a diff in pull request #22249: [FLINK-17398][connector/filesystem] Filesystem sources support flexible path reading

2023-06-08 Thread via GitHub


ruanhang1993 commented on code in PR #22249:
URL: https://github.com/apache/flink/pull/22249#discussion_r1222615155


##
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/enumerate/NonSplittingRegexEnumerator.java:
##
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.connector.file.src.enumerate;
+
+import org.apache.flink.connector.file.src.FileSourceSplit;
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * This {@code FileEnumerator} enumerates all files under the given paths 
recursively. Each file
+ * matched the given regex pattern becomes one split; this enumerator does not 
split files into
+ * smaller "block" units.
+ *
+ * This enumerator filters out files with the common hidden file prefixes 
'.' and '_'.
+ */
+public class NonSplittingRegexEnumerator extends 
NonSplittingRecursiveEnumerator {
+
+private final Predicate fileFilter;
+private final RegexFileFilter regexFileFilter;
+
+public NonSplittingRegexEnumerator(String pathPattern) {
+this(pathPattern, new DefaultFileFilter());
+}
+
+public NonSplittingRegexEnumerator(String pathPattern, Predicate 
fileFilter) {
+this.regexFileFilter = new RegexFileFilter(pathPattern);
+this.fileFilter = fileFilter;
+}
+
+@Override
+public Collection enumerateSplits(Path[] paths, int 
minDesiredSplits)
+throws IOException {
+final ArrayList splits = new ArrayList<>();
+for (Path path : paths) {
+final FileSystem fs = path.getFileSystem();
+final FileStatus status = fs.getFileStatus(path);
+addSplitsForPath(status, fs, splits, regexFileFilter);

Review Comment:
   The method `addSplitsForPath ` will be invoked in itself at line 83. The 
last parameter may be null.
   When it is null, we will know the dir/file comes from a matched path. And we 
will handle them if they are not filtered by the custom `fileFilter`.
   
   



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org