|
17 | 17 |
|
18 | 18 | package org.apache.seatunnel.connectors.seatunnel.file.sftp.source;
|
19 | 19 |
|
20 |
| -import org.apache.seatunnel.shade.com.typesafe.config.Config; |
21 |
| - |
22 |
| -import org.apache.seatunnel.api.common.PrepareFailException; |
23 |
| -import org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode; |
24 | 20 | import org.apache.seatunnel.api.configuration.ReadonlyConfig;
|
25 |
| -import org.apache.seatunnel.api.source.SeaTunnelSource; |
26 |
| -import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; |
27 |
| -import org.apache.seatunnel.api.table.catalog.schema.TableSchemaOptions; |
28 |
| -import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
29 |
| -import org.apache.seatunnel.common.config.CheckConfigUtil; |
30 |
| -import org.apache.seatunnel.common.config.CheckResult; |
31 |
| -import org.apache.seatunnel.common.constants.PluginType; |
32 |
| -import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; |
33 |
| -import org.apache.seatunnel.connectors.seatunnel.file.config.FileFormat; |
34 | 21 | import org.apache.seatunnel.connectors.seatunnel.file.config.FileSystemType;
|
35 |
| -import org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorErrorCode; |
36 |
| -import org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorException; |
37 |
| -import org.apache.seatunnel.connectors.seatunnel.file.sftp.config.SftpConf; |
38 |
| -import org.apache.seatunnel.connectors.seatunnel.file.sftp.config.SftpConfigOptions; |
39 |
| -import org.apache.seatunnel.connectors.seatunnel.file.source.BaseFileSource; |
40 |
| -import org.apache.seatunnel.connectors.seatunnel.file.source.reader.ReadStrategyFactory; |
41 |
| - |
42 |
| -import com.google.auto.service.AutoService; |
| 22 | +import org.apache.seatunnel.connectors.seatunnel.file.sftp.config.MultipleTableSFTPFileSourceConfig; |
| 23 | +import org.apache.seatunnel.connectors.seatunnel.file.source.BaseMultipleTableFileSource; |
43 | 24 |
|
44 |
| -import java.io.IOException; |
| 25 | +public class SftpFileSource extends BaseMultipleTableFileSource { |
| 26 | + public SftpFileSource(ReadonlyConfig config) { |
| 27 | + super(new MultipleTableSFTPFileSourceConfig(config)); |
| 28 | + } |
45 | 29 |
|
46 |
| -@AutoService(SeaTunnelSource.class) |
47 |
| -public class SftpFileSource extends BaseFileSource { |
48 | 30 | @Override
|
49 | 31 | public String getPluginName() {
|
50 | 32 | return FileSystemType.SFTP.getFileSystemPluginName();
|
51 | 33 | }
|
52 |
| - |
53 |
| - @Override |
54 |
| - public void prepare(Config pluginConfig) throws PrepareFailException { |
55 |
| - CheckResult result = |
56 |
| - CheckConfigUtil.checkAllExists( |
57 |
| - pluginConfig, |
58 |
| - SftpConfigOptions.FILE_PATH.key(), |
59 |
| - SftpConfigOptions.FILE_FORMAT_TYPE.key(), |
60 |
| - SftpConfigOptions.SFTP_HOST.key(), |
61 |
| - SftpConfigOptions.SFTP_PORT.key(), |
62 |
| - SftpConfigOptions.SFTP_USER.key(), |
63 |
| - SftpConfigOptions.SFTP_PASSWORD.key()); |
64 |
| - if (!result.isSuccess()) { |
65 |
| - throw new FileConnectorException( |
66 |
| - SeaTunnelAPIErrorCode.CONFIG_VALIDATION_FAILED, |
67 |
| - String.format( |
68 |
| - "PluginName: %s, PluginType: %s, Message: %s", |
69 |
| - getPluginName(), PluginType.SOURCE, result.getMsg())); |
70 |
| - } |
71 |
| - FileFormat fileFormat = |
72 |
| - FileFormat.valueOf( |
73 |
| - pluginConfig |
74 |
| - .getString(SftpConfigOptions.FILE_FORMAT_TYPE.key()) |
75 |
| - .toUpperCase()); |
76 |
| - if (fileFormat == FileFormat.ORC || fileFormat == FileFormat.PARQUET) { |
77 |
| - throw new FileConnectorException( |
78 |
| - CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT, |
79 |
| - "Sftp file source connector only support read [text, csv, json, xml] files"); |
80 |
| - } |
81 |
| - String path = pluginConfig.getString(SftpConfigOptions.FILE_PATH.key()); |
82 |
| - hadoopConf = SftpConf.buildWithConfig(ReadonlyConfig.fromConfig(pluginConfig)); |
83 |
| - readStrategy = |
84 |
| - ReadStrategyFactory.of( |
85 |
| - pluginConfig.getString(SftpConfigOptions.FILE_FORMAT_TYPE.key())); |
86 |
| - readStrategy.setPluginConfig(pluginConfig); |
87 |
| - readStrategy.init(hadoopConf); |
88 |
| - try { |
89 |
| - filePaths = readStrategy.getFileNamesByPath(path); |
90 |
| - } catch (IOException e) { |
91 |
| - String errorMsg = String.format("Get file list from this path [%s] failed", path); |
92 |
| - throw new FileConnectorException( |
93 |
| - FileConnectorErrorCode.FILE_LIST_GET_FAILED, errorMsg, e); |
94 |
| - } |
95 |
| - // support user-defined schema |
96 |
| - // only json csv text type support user-defined schema now |
97 |
| - if (pluginConfig.hasPath(TableSchemaOptions.SCHEMA.key())) { |
98 |
| - switch (fileFormat) { |
99 |
| - case CSV: |
100 |
| - case TEXT: |
101 |
| - case JSON: |
102 |
| - case EXCEL: |
103 |
| - case XML: |
104 |
| - SeaTunnelRowType userDefinedSchema = |
105 |
| - CatalogTableUtil.buildWithConfig(pluginConfig).getSeaTunnelRowType(); |
106 |
| - readStrategy.setSeaTunnelRowTypeInfo(userDefinedSchema); |
107 |
| - rowType = readStrategy.getActualSeaTunnelRowTypeInfo(); |
108 |
| - break; |
109 |
| - case ORC: |
110 |
| - case PARQUET: |
111 |
| - case BINARY: |
112 |
| - throw new FileConnectorException( |
113 |
| - CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION, |
114 |
| - "SeaTunnel does not support user-defined schema for [parquet, orc, binary] files"); |
115 |
| - default: |
116 |
| - // never got in there |
117 |
| - throw new FileConnectorException( |
118 |
| - CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT, |
119 |
| - "SeaTunnel does not supported this file format"); |
120 |
| - } |
121 |
| - } else { |
122 |
| - if (filePaths.isEmpty()) { |
123 |
| - // When the directory is empty, distribute default behavior schema |
124 |
| - rowType = CatalogTableUtil.buildSimpleTextSchema(); |
125 |
| - return; |
126 |
| - } |
127 |
| - try { |
128 |
| - rowType = readStrategy.getSeaTunnelRowTypeInfo(filePaths.get(0)); |
129 |
| - } catch (FileConnectorException e) { |
130 |
| - String errorMsg = |
131 |
| - String.format("Get table schema from file [%s] failed", filePaths.get(0)); |
132 |
| - throw new FileConnectorException( |
133 |
| - CommonErrorCodeDeprecated.TABLE_SCHEMA_GET_FAILED, errorMsg, e); |
134 |
| - } |
135 |
| - } |
136 |
| - } |
137 | 34 | }
|
0 commit comments