17
17
18
18
package org .apache .doris .flink .tools .cdc .mongodb .serializer ;
19
19
20
+ import org .apache .flink .annotation .VisibleForTesting ;
21
+ import org .apache .flink .util .StringUtils ;
22
+
20
23
import com .fasterxml .jackson .databind .JsonNode ;
21
24
import com .fasterxml .jackson .databind .ObjectMapper ;
22
25
import com .fasterxml .jackson .databind .node .NullNode ;
23
26
import com .fasterxml .jackson .databind .node .ObjectNode ;
27
+ import org .apache .doris .flink .catalog .doris .DataModel ;
24
28
import org .apache .doris .flink .catalog .doris .DorisSystem ;
25
29
import org .apache .doris .flink .catalog .doris .FieldSchema ;
26
30
import org .apache .doris .flink .cfg .DorisOptions ;
30
34
import org .apache .doris .flink .sink .writer .serializer .jsondebezium .CdcSchemaChange ;
31
35
import org .apache .doris .flink .sink .writer .serializer .jsondebezium .JsonDebeziumChangeContext ;
32
36
import org .apache .doris .flink .tools .cdc .SourceSchema ;
37
+ import org .apache .doris .flink .tools .cdc .mongodb .MongoDBSchema ;
33
38
import org .apache .doris .flink .tools .cdc .mongodb .MongoDBType ;
34
39
import org .apache .doris .flink .tools .cdc .mongodb .MongoDateConverter ;
40
+ import org .apache .doris .flink .tools .cdc .utils .DorisTableUtil ;
41
+ import org .apache .doris .flink .tools .cdc .utils .JsonNodeExtractUtil ;
35
42
import org .slf4j .Logger ;
36
43
import org .slf4j .LoggerFactory ;
37
44
@@ -60,17 +67,19 @@ public class MongoJsonDebeziumSchemaChange extends CdcSchemaChange {
60
67
61
68
private final Map <String , Map <String , String >> tableFields ;
62
69
63
- private final SchemaChangeManager schemaChangeManager ;
70
+ private SchemaChangeManager schemaChangeManager ;
64
71
65
- private final DorisSystem dorisSystem ;
72
+ private DorisSystem dorisSystem ;
66
73
67
74
public Map <String , String > tableMapping ;
68
75
private final DorisOptions dorisOptions ;
76
+ public JsonDebeziumChangeContext changeContext ;
69
77
70
78
private final Set <String > specialFields =
71
79
new HashSet <>(Arrays .asList (DATE_FIELD , TIMESTAMP_FIELD , DECIMAL_FIELD , LONG_FIELD ));
72
80
73
81
public MongoJsonDebeziumSchemaChange (JsonDebeziumChangeContext changeContext ) {
82
+ this .changeContext = changeContext ;
74
83
this .objectMapper = changeContext .getObjectMapper ();
75
84
this .dorisOptions = changeContext .getDorisOptions ();
76
85
this .tableFields = new HashMap <>();
@@ -96,6 +105,35 @@ public boolean schemaChange(JsonNode recordRoot) {
96
105
String cdcTableIdentifier = getCdcTableIdentifier (recordRoot );
97
106
String dorisTableIdentifier =
98
107
getDorisTableIdentifier (cdcTableIdentifier , dorisOptions , tableMapping );
108
+
109
+ // if table dorisTableIdentifier is null, create table
110
+ if (StringUtils .isNullOrWhitespaceOnly (dorisTableIdentifier )) {
111
+ String [] split = cdcTableIdentifier .split ("\\ ." );
112
+ String targetDb = changeContext .getTargetDatabase ();
113
+ String sourceTable = split [1 ];
114
+ String dorisTable = changeContext .getTableNameConverter ().convert (sourceTable );
115
+ LOG .info (
116
+ "The table [{}.{}] does not exist. Attempting to create a new table named: {}.{}" ,
117
+ targetDb ,
118
+ sourceTable ,
119
+ targetDb ,
120
+ dorisTable );
121
+ tableMapping .put (cdcTableIdentifier , String .format ("%s.%s" , targetDb , dorisTable ));
122
+ dorisTableIdentifier = tableMapping .get (cdcTableIdentifier );
123
+ Map <String , Object > stringObjectMap = extractAfterRow (logData );
124
+ JsonNode jsonNode = objectMapper .valueToTree (stringObjectMap );
125
+
126
+ MongoDBSchema mongoSchema = new MongoDBSchema (jsonNode , targetDb , dorisTable , "" );
127
+
128
+ mongoSchema .setModel (DataModel .UNIQUE );
129
+ DorisTableUtil .tryCreateTableIfAbsent (
130
+ dorisSystem ,
131
+ targetDb ,
132
+ dorisTable ,
133
+ mongoSchema ,
134
+ changeContext .getDorisTableConf ());
135
+ }
136
+
99
137
String [] tableInfo = dorisTableIdentifier .split ("\\ ." );
100
138
if (tableInfo .length != 2 ) {
101
139
throw new DorisRuntimeException ();
@@ -163,6 +201,10 @@ private JsonNode getFullDocument(JsonNode recordRoot) {
163
201
}
164
202
}
165
203
204
+ public Map <String , Object > extractAfterRow (JsonNode recordRoot ) {
205
+ return JsonNodeExtractUtil .extractAfterRow (recordRoot , objectMapper );
206
+ }
207
+
166
208
private void checkAndUpdateSchemaChange (
167
209
JsonNode logData , String dorisTableIdentifier , String database , String table ) {
168
210
Map <String , String > tableFieldMap = tableFields .get (dorisTableIdentifier );
@@ -207,4 +249,14 @@ public String getCdcTableIdentifier(JsonNode record) {
207
249
String db = nameSpace .get (FIELD_DATABASE ).asText ();
208
250
return SourceSchema .getString (db , null , table );
209
251
}
252
+
253
+ @ VisibleForTesting
254
+ public void setDorisSystem (DorisSystem dorisSystem ) {
255
+ this .dorisSystem = dorisSystem ;
256
+ }
257
+
258
+ @ VisibleForTesting
259
+ public void setSchemaChangeManager (SchemaChangeManager schemaChangeManager ) {
260
+ this .schemaChangeManager = schemaChangeManager ;
261
+ }
210
262
}
0 commit comments