|
17 | 17 |
|
18 | 18 | package org.apache.seatunnel.connectors.seatunnel.paimon.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; |
| 20 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
24 | 21 | import org.apache.seatunnel.api.source.Boundedness;
|
25 | 22 | import org.apache.seatunnel.api.source.SeaTunnelSource;
|
26 | 23 | import org.apache.seatunnel.api.source.SourceReader;
|
27 | 24 | import org.apache.seatunnel.api.source.SourceSplitEnumerator;
|
28 |
| -import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 25 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 26 | +import org.apache.seatunnel.api.table.catalog.TablePath; |
29 | 27 | import org.apache.seatunnel.api.table.type.SeaTunnelRow;
|
30 | 28 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
|
31 |
| -import org.apache.seatunnel.common.config.CheckConfigUtil; |
32 |
| -import org.apache.seatunnel.common.config.CheckResult; |
33 |
| -import org.apache.seatunnel.common.constants.PluginType; |
34 |
| -import org.apache.seatunnel.connectors.seatunnel.paimon.exception.PaimonConnectorErrorCode; |
35 |
| -import org.apache.seatunnel.connectors.seatunnel.paimon.exception.PaimonConnectorException; |
36 |
| -import org.apache.seatunnel.connectors.seatunnel.paimon.utils.RowTypeConverter; |
37 |
| - |
38 |
| -import org.apache.hadoop.conf.Configuration; |
39 |
| -import org.apache.hadoop.fs.Path; |
40 |
| -import org.apache.paimon.catalog.Catalog; |
41 |
| -import org.apache.paimon.catalog.CatalogContext; |
42 |
| -import org.apache.paimon.catalog.CatalogFactory; |
43 |
| -import org.apache.paimon.catalog.Identifier; |
44 |
| -import org.apache.paimon.options.Options; |
45 |
| -import org.apache.paimon.table.Table; |
46 |
| - |
47 |
| -import com.google.auto.service.AutoService; |
| 29 | +import org.apache.seatunnel.connectors.seatunnel.paimon.catalog.PaimonCatalog; |
| 30 | +import org.apache.seatunnel.connectors.seatunnel.paimon.config.PaimonSourceConfig; |
| 31 | +import org.apache.seatunnel.connectors.seatunnel.paimon.source.converter.SqlToPaimonPredicateConverter; |
48 | 32 |
|
49 |
| -import java.util.HashMap; |
50 |
| -import java.util.Map; |
| 33 | +import org.apache.paimon.predicate.Predicate; |
| 34 | +import org.apache.paimon.table.Table; |
51 | 35 |
|
52 |
| -import static org.apache.seatunnel.connectors.seatunnel.paimon.config.PaimonConfig.DATABASE; |
53 |
| -import static org.apache.seatunnel.connectors.seatunnel.paimon.config.PaimonConfig.HDFS_SITE_PATH; |
54 |
| -import static org.apache.seatunnel.connectors.seatunnel.paimon.config.PaimonConfig.TABLE; |
55 |
| -import static org.apache.seatunnel.connectors.seatunnel.paimon.config.PaimonConfig.WAREHOUSE; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
56 | 38 |
|
57 | 39 | /** Paimon connector source class. */
|
58 |
| -@AutoService(SeaTunnelSource.class) |
59 | 40 | public class PaimonSource
|
60 | 41 | implements SeaTunnelSource<SeaTunnelRow, PaimonSourceSplit, PaimonSourceState> {
|
61 | 42 |
|
62 | 43 | private static final long serialVersionUID = 1L;
|
63 | 44 |
|
64 | 45 | public static final String PLUGIN_NAME = "Paimon";
|
65 | 46 |
|
66 |
| - private Config pluginConfig; |
| 47 | + private ReadonlyConfig readonlyConfig; |
67 | 48 |
|
68 | 49 | private SeaTunnelRowType seaTunnelRowType;
|
69 | 50 |
|
70 |
| - private Table table; |
| 51 | + private Table paimonTable; |
| 52 | + |
| 53 | + private Predicate predicate; |
| 54 | + |
| 55 | + private CatalogTable catalogTable; |
| 56 | + |
| 57 | + public PaimonSource(ReadonlyConfig readonlyConfig, PaimonCatalog paimonCatalog) { |
| 58 | + this.readonlyConfig = readonlyConfig; |
| 59 | + PaimonSourceConfig paimonSourceConfig = new PaimonSourceConfig(readonlyConfig); |
| 60 | + TablePath tablePath = |
| 61 | + TablePath.of(paimonSourceConfig.getNamespace(), paimonSourceConfig.getTable()); |
| 62 | + this.catalogTable = paimonCatalog.getTable(tablePath); |
| 63 | + this.paimonTable = paimonCatalog.getPaimonTable(tablePath); |
| 64 | + this.seaTunnelRowType = catalogTable.getSeaTunnelRowType(); |
| 65 | + // TODO: We can use this to realize the column projection feature later |
| 66 | + String filterSql = readonlyConfig.get(PaimonSourceConfig.QUERY_SQL); |
| 67 | + this.predicate = |
| 68 | + SqlToPaimonPredicateConverter.convertSqlWhereToPaimonPredicate( |
| 69 | + this.paimonTable.rowType(), filterSql); |
| 70 | + } |
71 | 71 |
|
72 | 72 | @Override
|
73 | 73 | public String getPluginName() {
|
74 | 74 | return PLUGIN_NAME;
|
75 | 75 | }
|
76 | 76 |
|
77 |
| - @Override |
78 |
| - public void prepare(Config pluginConfig) throws PrepareFailException { |
79 |
| - this.pluginConfig = pluginConfig; |
80 |
| - final CheckResult result = |
81 |
| - CheckConfigUtil.checkAllExists( |
82 |
| - pluginConfig, WAREHOUSE.key(), DATABASE.key(), TABLE.key()); |
83 |
| - if (!result.isSuccess()) { |
84 |
| - throw new PaimonConnectorException( |
85 |
| - SeaTunnelAPIErrorCode.CONFIG_VALIDATION_FAILED, |
86 |
| - String.format( |
87 |
| - "PluginName: %s, PluginType: %s, Message: %s", |
88 |
| - getPluginName(), PluginType.SOURCE, result.getMsg())); |
89 |
| - } |
90 |
| - // initialize paimon table |
91 |
| - final String warehouse = pluginConfig.getString(WAREHOUSE.key()); |
92 |
| - final String database = pluginConfig.getString(DATABASE.key()); |
93 |
| - final String table = pluginConfig.getString(TABLE.key()); |
94 |
| - final Map<String, String> optionsMap = new HashMap<>(); |
95 |
| - optionsMap.put(WAREHOUSE.key(), warehouse); |
96 |
| - final Options options = Options.fromMap(optionsMap); |
97 |
| - final Configuration hadoopConf = new Configuration(); |
98 |
| - if (pluginConfig.hasPath(HDFS_SITE_PATH.key())) { |
99 |
| - hadoopConf.addResource(new Path(pluginConfig.getString(HDFS_SITE_PATH.key()))); |
100 |
| - } |
101 |
| - final CatalogContext catalogContext = CatalogContext.create(options, hadoopConf); |
102 |
| - try (Catalog catalog = CatalogFactory.createCatalog(catalogContext)) { |
103 |
| - Identifier identifier = Identifier.create(database, table); |
104 |
| - this.table = catalog.getTable(identifier); |
105 |
| - } catch (Exception e) { |
106 |
| - String errorMsg = |
107 |
| - String.format( |
108 |
| - "Failed to get table [%s] from database [%s] on warehouse [%s]", |
109 |
| - database, table, warehouse); |
110 |
| - throw new PaimonConnectorException( |
111 |
| - PaimonConnectorErrorCode.GET_TABLE_FAILED, errorMsg, e); |
112 |
| - } |
113 |
| - // TODO: Support column projection |
114 |
| - seaTunnelRowType = RowTypeConverter.convert(this.table.rowType()); |
115 |
| - } |
116 |
| - |
117 | 77 | @Override
|
118 | 78 | public Boundedness getBoundedness() {
|
119 | 79 | return Boundedness.BOUNDED;
|
120 | 80 | }
|
121 | 81 |
|
122 | 82 | @Override
|
123 |
| - public SeaTunnelDataType<SeaTunnelRow> getProducedType() { |
124 |
| - return seaTunnelRowType; |
| 83 | + public List<CatalogTable> getProducedCatalogTables() { |
| 84 | + return Collections.singletonList(catalogTable); |
125 | 85 | }
|
126 | 86 |
|
127 | 87 | @Override
|
128 | 88 | public SourceReader<SeaTunnelRow, PaimonSourceSplit> createReader(
|
129 | 89 | SourceReader.Context readerContext) throws Exception {
|
130 |
| - return new PaimonSourceReader(readerContext, table, seaTunnelRowType); |
| 90 | + |
| 91 | + return new PaimonSourceReader(readerContext, paimonTable, seaTunnelRowType, predicate); |
131 | 92 | }
|
132 | 93 |
|
133 | 94 | @Override
|
134 | 95 | public SourceSplitEnumerator<PaimonSourceSplit, PaimonSourceState> createEnumerator(
|
135 | 96 | SourceSplitEnumerator.Context<PaimonSourceSplit> enumeratorContext) throws Exception {
|
136 |
| - return new PaimonSourceSplitEnumerator(enumeratorContext, table); |
| 97 | + return new PaimonSourceSplitEnumerator(enumeratorContext, paimonTable, predicate); |
137 | 98 | }
|
138 | 99 |
|
139 | 100 | @Override
|
140 | 101 | public SourceSplitEnumerator<PaimonSourceSplit, PaimonSourceState> restoreEnumerator(
|
141 | 102 | SourceSplitEnumerator.Context<PaimonSourceSplit> enumeratorContext,
|
142 | 103 | PaimonSourceState checkpointState)
|
143 | 104 | throws Exception {
|
144 |
| - return new PaimonSourceSplitEnumerator(enumeratorContext, table, checkpointState); |
| 105 | + return new PaimonSourceSplitEnumerator( |
| 106 | + enumeratorContext, paimonTable, checkpointState, predicate); |
145 | 107 | }
|
146 | 108 | }
|
0 commit comments