17
17
18
18
package org .apache .seatunnel .connectors .seatunnel .openmldb .source ;
19
19
20
- import org .apache .seatunnel .shade .com .typesafe .config .Config ;
21
-
22
20
import org .apache .seatunnel .api .common .JobContext ;
23
- import org .apache .seatunnel .api .common .PrepareFailException ;
24
- import org .apache .seatunnel .api .common .SeaTunnelAPIErrorCode ;
25
21
import org .apache .seatunnel .api .source .Boundedness ;
26
- import org .apache .seatunnel .api .source .SeaTunnelSource ;
27
22
import org .apache .seatunnel .api .source .SupportColumnProjection ;
23
+ import org .apache .seatunnel .api .table .catalog .CatalogTable ;
24
+ import org .apache .seatunnel .api .table .catalog .PhysicalColumn ;
25
+ import org .apache .seatunnel .api .table .catalog .TableIdentifier ;
26
+ import org .apache .seatunnel .api .table .catalog .TableSchema ;
28
27
import org .apache .seatunnel .api .table .type .BasicType ;
29
28
import org .apache .seatunnel .api .table .type .LocalTimeType ;
30
29
import org .apache .seatunnel .api .table .type .SeaTunnelDataType ;
31
30
import org .apache .seatunnel .api .table .type .SeaTunnelRow ;
32
- import org .apache .seatunnel .api .table .type .SeaTunnelRowType ;
33
- import org .apache .seatunnel .common .config .CheckConfigUtil ;
34
- import org .apache .seatunnel .common .config .CheckResult ;
35
31
import org .apache .seatunnel .common .constants .JobMode ;
36
- import org .apache .seatunnel .common .constants .PluginType ;
37
32
import org .apache .seatunnel .common .exception .CommonErrorCodeDeprecated ;
38
33
import org .apache .seatunnel .connectors .seatunnel .common .source .AbstractSingleSplitReader ;
39
34
import org .apache .seatunnel .connectors .seatunnel .common .source .AbstractSingleSplitSource ;
40
35
import org .apache .seatunnel .connectors .seatunnel .common .source .SingleSplitReaderContext ;
41
- import org .apache .seatunnel .connectors .seatunnel .openmldb .config .OpenMldbConfig ;
42
36
import org .apache .seatunnel .connectors .seatunnel .openmldb .config .OpenMldbParameters ;
43
37
import org .apache .seatunnel .connectors .seatunnel .openmldb .config .OpenMldbSqlExecutor ;
44
38
import org .apache .seatunnel .connectors .seatunnel .openmldb .exception .OpenMldbConnectorException ;
47
41
import com ._4paradigm .openmldb .sdk .Schema ;
48
42
import com ._4paradigm .openmldb .sdk .SqlException ;
49
43
import com ._4paradigm .openmldb .sdk .impl .SqlClusterExecutor ;
50
- import com .google .auto .service .AutoService ;
51
44
52
45
import java .sql .SQLException ;
53
46
import java .sql .Types ;
47
+ import java .util .Collections ;
54
48
import java .util .List ;
55
49
56
- @ AutoService (SeaTunnelSource .class )
57
50
public class OpenMldbSource extends AbstractSingleSplitSource <SeaTunnelRow >
58
51
implements SupportColumnProjection {
59
- private OpenMldbParameters openMldbParameters ;
52
+ private final OpenMldbParameters openMldbParameters ;
53
+ private final CatalogTable catalogTable ;
60
54
private JobContext jobContext ;
61
- private SeaTunnelRowType seaTunnelRowType ;
62
55
63
- @ Override
64
- public String getPluginName () {
65
- return "OpenMldb" ;
66
- }
67
-
68
- @ Override
69
- public void prepare (Config pluginConfig ) throws PrepareFailException {
70
- CheckResult result =
71
- CheckConfigUtil .checkAllExists (
72
- pluginConfig ,
73
- OpenMldbConfig .CLUSTER_MODE .key (),
74
- OpenMldbConfig .SQL .key (),
75
- OpenMldbConfig .DATABASE .key ());
76
- if (!result .isSuccess ()) {
77
- throw new OpenMldbConnectorException (
78
- SeaTunnelAPIErrorCode .CONFIG_VALIDATION_FAILED ,
79
- String .format (
80
- "PluginName: %s, PluginType: %s, Message: %s" ,
81
- getPluginName (), PluginType .SOURCE , result .getMsg ()));
82
- }
83
- if (pluginConfig .getBoolean (OpenMldbConfig .CLUSTER_MODE .key ())) {
84
- // cluster mode
85
- result =
86
- CheckConfigUtil .checkAllExists (
87
- pluginConfig ,
88
- OpenMldbConfig .ZK_HOST .key (),
89
- OpenMldbConfig .ZK_PATH .key ());
90
- } else {
91
- // single mode
92
- result =
93
- CheckConfigUtil .checkAllExists (
94
- pluginConfig , OpenMldbConfig .HOST .key (), OpenMldbConfig .PORT .key ());
95
- }
96
- if (!result .isSuccess ()) {
97
- throw new OpenMldbConnectorException (
98
- SeaTunnelAPIErrorCode .CONFIG_VALIDATION_FAILED ,
99
- String .format (
100
- "PluginName: %s, PluginType: %s, Message: %s" ,
101
- getPluginName (), PluginType .SOURCE , result .getMsg ()));
102
- }
103
- this .openMldbParameters = OpenMldbParameters .buildWithConfig (pluginConfig );
56
+ public OpenMldbSource (OpenMldbParameters openMldbParameters ) {
57
+ this .openMldbParameters = openMldbParameters ;
104
58
OpenMldbSqlExecutor .initSdkOption (openMldbParameters );
105
59
try {
106
60
SqlClusterExecutor sqlExecutor = OpenMldbSqlExecutor .getSqlExecutor ();
107
61
Schema inputSchema =
108
62
sqlExecutor .getInputSchema (
109
63
openMldbParameters .getDatabase (), openMldbParameters .getSql ());
110
64
List <Column > columnList = inputSchema .getColumnList ();
111
- this .seaTunnelRowType = convert (columnList );
65
+ this .catalogTable = convert (columnList );
112
66
} catch (SQLException | SqlException e ) {
113
67
throw new OpenMldbConnectorException (
114
68
CommonErrorCodeDeprecated .TABLE_SCHEMA_GET_FAILED ,
115
69
"Failed to initialize data schema" );
116
70
}
117
71
}
118
72
73
+ @ Override
74
+ public String getPluginName () {
75
+ return "OpenMldb" ;
76
+ }
77
+
119
78
@ Override
120
79
public Boundedness getBoundedness () {
121
80
return JobMode .BATCH .equals (jobContext .getJobMode ())
@@ -124,14 +83,15 @@ public Boundedness getBoundedness() {
124
83
}
125
84
126
85
@ Override
127
- public SeaTunnelDataType < SeaTunnelRow > getProducedType () {
128
- return seaTunnelRowType ;
86
+ public List < CatalogTable > getProducedCatalogTables () {
87
+ return Collections . singletonList ( catalogTable ) ;
129
88
}
130
89
131
90
@ Override
132
91
public AbstractSingleSplitReader <SeaTunnelRow > createReader (
133
92
SingleSplitReaderContext readerContext ) throws Exception {
134
- return new OpenMldbSourceReader (openMldbParameters , seaTunnelRowType , readerContext );
93
+ return new OpenMldbSourceReader (
94
+ openMldbParameters , catalogTable .getSeaTunnelRowType (), readerContext );
135
95
}
136
96
137
97
@ Override
@@ -166,14 +126,24 @@ private SeaTunnelDataType<?> convertSeaTunnelDataType(int type) {
166
126
}
167
127
}
168
128
169
- private SeaTunnelRowType convert (List <Column > columnList ) {
170
- String [] fieldsName = new String [columnList .size ()];
171
- SeaTunnelDataType <?>[] fieldsType = new SeaTunnelDataType <?>[columnList .size ()];
129
+ private CatalogTable convert (List <Column > columnList ) {
130
+ TableSchema .Builder builder = TableSchema .builder ();
172
131
for (int i = 0 ; i < columnList .size (); i ++) {
173
132
Column column = columnList .get (i );
174
- fieldsName [i ] = column .getColumnName ();
175
- fieldsType [i ] = convertSeaTunnelDataType (column .getSqlType ());
133
+ builder .column (
134
+ PhysicalColumn .of (
135
+ column .getColumnName (),
136
+ convertSeaTunnelDataType (column .getSqlType ()),
137
+ (Long ) null ,
138
+ column .isNotNull (),
139
+ null ,
140
+ null ));
176
141
}
177
- return new SeaTunnelRowType (fieldsName , fieldsType );
142
+ return CatalogTable .of (
143
+ TableIdentifier .of ("OpenMldb" , openMldbParameters .getDatabase (), "default" ),
144
+ builder .build (),
145
+ null ,
146
+ null ,
147
+ null );
178
148
}
179
149
}
0 commit comments