You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by ki...@apache.org on 2022/08/16 01:23:37 UTC

[incubator-seatunnel] branch dev updated: [Feature][Connector-V2] Add local file connector source (#2419)

This is an automated email from the ASF dual-hosted git repository.

kirs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new eff595c45 [Feature][Connector-V2] Add local file connector source (#2419)
eff595c45 is described below

commit eff595c452479c62dd035d24f7f8b39da58430e3
Author: TyrantLucifer <Ty...@gmail.com>
AuthorDate: Tue Aug 16 09:23:31 2022 +0800

    [Feature][Connector-V2] Add local file connector source (#2419)
    
    * [Feature][Connector-V2] Add local file source connector (#1946)
---
 docs/en/connector-v2/source/LocalFile.md           | 35 ++++++++++++
 plugin-mapping.properties                          |  1 +
 .../connector-file/connector-file-local/pom.xml    |  1 -
 .../file/local/source/LocalFileSource.java         | 65 ++++++++++++++++++++++
 .../local/source/config/LocalSourceConfig.java     | 23 ++++++++
 5 files changed, 124 insertions(+), 1 deletion(-)

diff --git a/docs/en/connector-v2/source/LocalFile.md b/docs/en/connector-v2/source/LocalFile.md
new file mode 100644
index 000000000..f725186fc
--- /dev/null
+++ b/docs/en/connector-v2/source/LocalFile.md
@@ -0,0 +1,35 @@
+# LocalFile
+
+> Local file source connector
+
+## Description
+
+Read data from local file system.
+
+## Options
+
+| name         | type   | required | default value |
+|--------------| ------ |----------|---------------|
+| path         | string | yes      | -             |
+| type         | string | yes      | -             |
+
+### path [string]
+
+The source file path.
+
+### type [string]
+
+File type, supported as the following file types:
+
+`text` `csv` `parquet` `orc` `json`
+
+## Example
+
+```hcon
+
+LocalFile {
+  path = "/apps/hive/demo/student"
+  type = "parquet"
+}
+
+```
\ No newline at end of file
diff --git a/plugin-mapping.properties b/plugin-mapping.properties
index f52f98bee..65a3991e0 100644
--- a/plugin-mapping.properties
+++ b/plugin-mapping.properties
@@ -108,6 +108,7 @@ seatunnel.source.Kudu = connector-kudu
 seatunnel.sink.Kudu = connector-kudu
 seatunnel.sink.Email = connector-email
 seatunnel.sink.HdfsFile = connector-file-hadoop
+seatunnel.source.LocalFile = connector-file-local
 seatunnel.sink.LocalFile = connector-file-local
 seatunnel.source.Pulsar = connector-pulsar
 seatunnel.source.Hudi = connector-hudi
diff --git a/seatunnel-connectors-v2/connector-file/connector-file-local/pom.xml b/seatunnel-connectors-v2/connector-file/connector-file-local/pom.xml
index 16eb200cd..3d71d062d 100644
--- a/seatunnel-connectors-v2/connector-file/connector-file-local/pom.xml
+++ b/seatunnel-connectors-v2/connector-file/connector-file-local/pom.xml
@@ -38,7 +38,6 @@
         <dependency>
             <groupId>org.apache.flink</groupId>
             <artifactId>flink-shaded-hadoop-2</artifactId>
-            <version>${flink-shaded-hadoop-2.version}</version>
             <scope>provided</scope>
         </dependency>
     </dependencies>
diff --git a/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/LocalFileSource.java b/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/LocalFileSource.java
new file mode 100644
index 000000000..58d2083b9
--- /dev/null
+++ b/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/LocalFileSource.java
@@ -0,0 +1,65 @@
+/*
+ * 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.source;
+
+import org.apache.seatunnel.api.common.PrepareFailException;
+import org.apache.seatunnel.api.source.SeaTunnelSource;
+import org.apache.seatunnel.common.config.CheckConfigUtil;
+import org.apache.seatunnel.common.config.CheckResult;
+import org.apache.seatunnel.common.constants.PluginType;
+import org.apache.seatunnel.connectors.seatunnel.file.config.FileSystemType;
+import org.apache.seatunnel.connectors.seatunnel.file.exception.FilePluginException;
+import org.apache.seatunnel.connectors.seatunnel.file.local.source.config.LocalSourceConfig;
+import org.apache.seatunnel.connectors.seatunnel.file.source.BaseFileSource;
+import org.apache.seatunnel.connectors.seatunnel.file.source.reader.ReadStrategyFactory;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+
+import java.io.IOException;
+
+@AutoService(SeaTunnelSource.class)
+public class LocalFileSource extends BaseFileSource {
+
+    @Override
+    public String getPluginName() {
+        return FileSystemType.LOCAL.getFileSystemPluginName();
+    }
+
+    @Override
+    public void prepare(Config pluginConfig) throws PrepareFailException {
+        CheckResult result = CheckConfigUtil.checkAllExists(pluginConfig, LocalSourceConfig.FILE_PATH, LocalSourceConfig.FILE_TYPE);
+        if (!result.isSuccess()) {
+            throw new PrepareFailException(getPluginName(), PluginType.SOURCE, result.getMsg());
+        }
+        readStrategy = ReadStrategyFactory.of(pluginConfig.getString(LocalSourceConfig.FILE_TYPE));
+        String path = pluginConfig.getString(LocalSourceConfig.FILE_PATH);
+        hadoopConf = null;
+        try {
+            filePaths = readStrategy.getFileNamesByPath(hadoopConf, path);
+        } catch (IOException e) {
+            throw new PrepareFailException(getPluginName(), PluginType.SOURCE, "Check file path fail.");
+        }
+        try {
+            rowType = readStrategy.getSeaTunnelRowTypeInfo(hadoopConf, filePaths.get(0));
+        } catch (FilePluginException e) {
+            throw new PrepareFailException(getPluginName(), PluginType.SOURCE, "Read file schema error.", e);
+        }
+    }
+}
diff --git a/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/config/LocalSourceConfig.java b/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/config/LocalSourceConfig.java
new file mode 100644
index 000000000..8c9f303ac
--- /dev/null
+++ b/seatunnel-connectors-v2/connector-file/connector-file-local/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/local/source/config/LocalSourceConfig.java
@@ -0,0 +1,23 @@
+/*
+ * 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.source.config;
+
+import org.apache.seatunnel.connectors.seatunnel.file.config.BaseSourceConfig;
+
+public class LocalSourceConfig extends BaseSourceConfig {
+}