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