Skip to content

Commit

Permalink
fix debeziumSourcefunction ddl parse
Browse files Browse the repository at this point in the history
  • Loading branch information
wudi committed Sep 27, 2023
1 parent 3cf60c5 commit b997ac3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -197,7 +198,7 @@ public boolean schemaChangeV2(JsonNode recordRoot) {
String ddlSql = ddlSqlList.get(i);
boolean doSchemaChange = checkSchemaChange(ddlSchema);
status = doSchemaChange && execSchemaChange(ddlSql);
LOG.info("schema change status:{}", status);
LOG.info("schema change status:{}, ddl:{}", status, ddlSql);
}
} catch (Exception ex) {
LOG.warn("schema change error :", ex);
Expand All @@ -220,12 +221,14 @@ private boolean checkSchemaChange(DDLSchema ddlSchema) throws IOException, Illeg

@VisibleForTesting
public List<String> extractDDLList(JsonNode record) throws JsonProcessingException {
JsonNode historyRecord = objectMapper.readTree(extractJsonNode(record, "historyRecord"));
JsonNode historyRecord = extractHistoryRecord(record);
JsonNode tableChanges = historyRecord.get("tableChanges");
JsonNode tableChange = tableChanges.get(0);
String ddl = extractJsonNode(historyRecord, "ddl");
if(Objects.isNull(tableChanges) || Objects.isNull(ddl)){
return new ArrayList<>();
}
LOG.debug("received debezium ddl :{}", ddl);

JsonNode tableChange = tableChanges.get(0);
Matcher matcher = addDropDDLPattern.matcher(ddl);
if (Objects.isNull(tableChange)|| !tableChange.get("type").asText().equals("ALTER") || !matcher.find()) {
return null;
Expand Down Expand Up @@ -388,12 +391,17 @@ private Map<String, Object> extractRow(JsonNode recordRow) {
return recordMap != null ? recordMap : new HashMap<>();
}

public String extractDDL(JsonNode record) throws JsonProcessingException {
String historyRecord = extractJsonNode(record, "historyRecord");
if (Objects.isNull(historyRecord)) {
return null;
private JsonNode extractHistoryRecord(JsonNode record) throws JsonProcessingException {
if(record.has("historyRecord")){
return objectMapper.readTree(record.get("historyRecord").asText());
}
String ddl = extractJsonNode(objectMapper.readTree(historyRecord), "ddl");
//The ddl passed by some scenes will not be included in the historyRecord, such as DebeziumSourceFunction
return record;
}

public String extractDDL(JsonNode record) throws JsonProcessingException {
JsonNode historyRecord = extractHistoryRecord(record);
String ddl = extractJsonNode(historyRecord, "ddl");
LOG.debug("received debezium ddl :{}", ddl);
if (!Objects.isNull(ddl)) {
//filter add/drop operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public DataStreamSource<String> buildCdcSource(StreamExecutionEnvironment env) {
.tableList(schemaName + "." + tableName)
.username(username)
.password(password)
.includeSchemaChanges(true)
.startupOptions(startupOptions)
.deserializer(schema)
.debeziumProperties(debeziumProperties)
Expand Down

0 comments on commit b997ac3

Please sign in to comment.