Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
72c765d4
Unverified
Commit
72c765d4
authored
Jun 14, 2024
by
soulzz
Committed by
GitHub
Jun 14, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Feature] add cdcsource sink table name mapping, regular matching modification (#3592)
Co-authored-by:
zhangmz
<
soulmz@dingtalk.com
>
parent
5d87ce9c
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
508 additions
and
6 deletions
+508
-6
build-extr.sh
build-extr.sh
+64
-0
AbstractSinkBuilder.java
...core/src/main/java/org/dinky/cdc/AbstractSinkBuilder.java
+60
-6
AbstractSinkBuilderTest.java
.../src/test/java/org/dinky/cdc/AbstractSinkBuilderTest.java
+248
-0
FlinkCDCConfig.java
...on/src/main/java/org/dinky/data/model/FlinkCDCConfig.java
+9
-0
cdcsource_mysqlcdc2mysql_regex.md
...uide/cdcsource_practice/cdcsource_mysqlcdc2mysql_regex.md
+124
-0
cdcsource_overview.md
.../practical_guide/cdcsource_practice/cdcsource_overview.md
+3
-0
No files found.
build-extr.sh
0 → 100755
View file @
72c765d4
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 执行构建命令
# echo "执行命令: $CMD"
# $CMD
#
# 使用说明
#
# 1. 默认构建:直接运行脚本,将使用默认的 jdk11 和 flink-1.18 版本。
#./build.sh
# 2. 指定 JDK 版本:使用 -j 参数来指定 JDK 版本,例如使用 jdk8:
#./build.sh -j jdk8
# 3. 指定 Flink 版本:使用 -f 参数来指定 Flink 版本,例如使用 flink-1.14:
#./build.sh -f flink-1.14
# 4. 同时指定 JDK 和 Flink 版本:你可以同时指定 JDK 和 Flink 版本:
#./build.sh -j jdk8 -f flink-1.15
# 默认参数
JDK_VERSION
=
"jdk11"
FLINK_VERSION
=
"flink-1.18"
# 帮助信息
usage
()
{
echo
"Usage:
$0
[-j <jdk_version>] [-f <flink_version>]"
echo
" -j <jdk_version> 指定 JDK 版本 (默认: jdk11, 可选: jdk8)"
echo
" -f <flink_version> 指定 Flink 版本 (默认: flink-1.18, 可选: flink-1.14, flink-1.15)"
exit
1
}
# 解析命令行参数
while
getopts
"j:f:"
opt
;
do
case
${
opt
}
in
j
)
JDK_VERSION
=
$OPTARG
;;
f
)
FLINK_VERSION
=
$OPTARG
;;
*
)
usage
;;
esac
done
# 构建命令
CMD
=
"./mvnw clean package -Dmaven.test.skip=true -P prod,
${
JDK_VERSION
}
,flink-single-version,scala-2.12,aliyun,
${
FLINK_VERSION
}
,web"
# 执行构建命令
echo
"执行命令:
$CMD
"
$CMD
\ No newline at end of file
dinky-cdc/dinky-cdc-core/src/main/java/org/dinky/cdc/AbstractSinkBuilder.java
View file @
72c765d4
...
@@ -66,11 +66,14 @@ import java.time.ZoneId;
...
@@ -66,11 +66,14 @@ import java.time.ZoneId;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.Optional
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
javax.xml.bind.DatatypeConverter
;
import
javax.xml.bind.DatatypeConverter
;
...
@@ -503,22 +506,73 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
...
@@ -503,22 +506,73 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
public
String
getSinkTableName
(
Table
table
)
{
public
String
getSinkTableName
(
Table
table
)
{
String
tableName
=
table
.
getName
();
String
tableName
=
table
.
getName
();
Map
<
String
,
String
>
sink
=
config
.
getSink
();
Map
<
String
,
String
>
sink
=
config
.
getSink
();
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
"table.prefix.schema"
)))
{
tableName
=
table
.
getSchema
()
+
"_"
+
tableName
;
// Add table name mapping logic
String
mappingRoute
=
sink
.
get
(
FlinkCDCConfig
.
TABLE_MAPPING_ROUTES
);
if
(
mappingRoute
!=
null
)
{
Map
<
String
,
String
>
mappingRules
=
parseMappingRoute
(
mappingRoute
);
if
(
mappingRules
.
containsKey
(
tableName
))
{
tableName
=
mappingRules
.
get
(
tableName
);
}
}
}
tableName
=
sink
.
getOrDefault
(
"table.prefix"
,
""
)
+
tableName
+
sink
.
getOrDefault
(
"table.suffix"
,
""
);
tableName
=
sink
.
getOrDefault
(
FlinkCDCConfig
.
TABLE_PREFIX
,
""
)
+
tableName
+
sink
.
getOrDefault
(
FlinkCDCConfig
.
TABLE_SUFFIX
,
""
);
// table.lower and table.upper can not be true at the same time
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
FlinkCDCConfig
.
TABLE_LOWER
))
&&
Boolean
.
parseBoolean
(
sink
.
get
(
FlinkCDCConfig
.
TABLE_UPPER
)))
{
throw
new
IllegalArgumentException
(
"table.lower and table.upper can not be true at the same time"
);
}
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
"table.lower"
)))
{
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
FlinkCDCConfig
.
TABLE_UPPER
)))
{
tableName
=
tableName
.
toUpperCase
();
}
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
FlinkCDCConfig
.
TABLE_LOWER
)))
{
tableName
=
tableName
.
toLowerCase
();
tableName
=
tableName
.
toLowerCase
();
}
}
// Implement regular expressions to replace table names through
// sink.table.replace.pattern and table.replace.with
String
replacePattern
=
sink
.
get
(
FlinkCDCConfig
.
TABLE_REPLACE_PATTERN
);
String
replaceWith
=
sink
.
get
(
FlinkCDCConfig
.
TABLE_REPLACE_WITH
);
if
(
replacePattern
!=
null
&&
replaceWith
!=
null
)
{
Pattern
pattern
=
Pattern
.
compile
(
replacePattern
);
Matcher
matcher
=
pattern
.
matcher
(
tableName
);
tableName
=
matcher
.
replaceAll
(
replaceWith
);
}
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
"table.upper"
)))
{
// add schema
tableName
=
tableName
.
toUpperCase
();
if
(
Boolean
.
parseBoolean
(
sink
.
get
(
"table.prefix.schema"
)))
{
tableName
=
table
.
getSchema
()
+
"_"
+
tableName
;
}
}
return
tableName
;
return
tableName
;
}
}
/**
* Mapping table name Original table name: target table name, multiple table names are implemented through mapping
* <pre>
* k is original table name, v is target table name.
* Single table name mapping via k:v format.
* Multiple table names are mapped in k:v,k:v format. Note: use commas to separate them.
* </pre>
*
* @param mappingRoute sink.table.mapping-route
* @return Map<String, String> key is original table name, value is target table name
*/
private
Map
<
String
,
String
>
parseMappingRoute
(
String
mappingRoute
)
{
Map
<
String
,
String
>
mappingRules
=
new
HashMap
<>();
String
[]
mappings
=
mappingRoute
.
split
(
","
);
for
(
String
mapping
:
mappings
)
{
String
[]
parts
=
mapping
.
split
(
":"
);
if
(
parts
.
length
==
2
)
{
mappingRules
.
put
(
parts
[
0
],
parts
[
1
]);
}
}
return
mappingRules
;
}
protected
List
<
String
>
getPKList
(
Table
table
)
{
protected
List
<
String
>
getPKList
(
Table
table
)
{
if
(
Asserts
.
isNullCollection
(
table
.
getColumns
()))
{
if
(
Asserts
.
isNullCollection
(
table
.
getColumns
()))
{
return
new
ArrayList
<>();
return
new
ArrayList
<>();
...
...
dinky-cdc/dinky-cdc-core/src/test/java/org/dinky/cdc/AbstractSinkBuilderTest.java
0 → 100644
View file @
72c765d4
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org
.
dinky
.
cdc
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
org.dinky.data.model.FlinkCDCConfig
;
import
org.dinky.data.model.Table
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
/**
* test Sink Builder
*
* @author soulmz
* @since 2024/06/12
*/
public
class
AbstractSinkBuilderTest
{
@Mock
private
FlinkCDCConfig
config
;
@InjectMocks
private
TestSinkBuilder
sinkBuilder
;
@Before
public
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
sinkBuilder
=
new
TestSinkBuilder
(
config
);
}
@Test
public
void
testGetSinkTableNameWithPrefixAndSuffix
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
"pre_"
);
sinkConfig
.
put
(
"table.suffix"
,
"_suf"
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"testTable"
,
"testSchema"
,
null
);
String
expectedTableName
=
"pre_testTable_suf"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithOnlyPrefix
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
"pre_"
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"testTable"
,
"testSchema"
,
null
);
String
expectedTableName
=
"pre_testTable"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithOnlySuffix
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
"_suf"
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"testTable"
,
"testSchema"
,
null
);
String
expectedTableName
=
"testTable_suf"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithSchemaPrefixEnabled
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix.schema"
,
"true"
);
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"testTable"
,
"testSchema"
,
null
);
String
expectedTableName
=
"testSchema_testTable"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithConversionLowerCase
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"true"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"TestTable"
,
"TestSchema"
,
null
);
String
expectedTableName
=
"testtable"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithConversionUpperCase
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"true"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"TestTable"
,
"TestSchema"
,
null
);
String
expectedTableName
=
"TESTTABLE"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithConversionUpperAndLowerCase
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"true"
);
sinkConfig
.
put
(
"table.upper"
,
"true"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"TestTable"
,
"TestSchema"
,
null
);
Assert
.
assertThrows
(
IllegalArgumentException
.
class
,
()
->
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithNoConfigPrefixOrSuffix
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.prefix"
,
""
);
sinkConfig
.
put
(
"table.suffix"
,
""
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"testTable"
,
"testSchema"
,
null
);
String
expectedTableName
=
"testTable"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithReplace
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.replace.pattern"
,
"t_(.*?)_"
);
sinkConfig
.
put
(
"table.replace.with"
,
"ods_$1_"
);
sinkConfig
.
put
(
"table.lower"
,
"false"
);
sinkConfig
.
put
(
"table.upper"
,
"false"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
table
=
new
Table
(
"t_example_test"
,
"testSchema"
,
null
);
String
expectedTableName
=
"ods_example_test"
;
Assert
.
assertEquals
(
expectedTableName
,
sinkBuilder
.
getSinkTableName
(
table
));
}
@Test
public
void
testGetSinkTableNameWithMappingRoute
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.mapping-routes"
,
"t_biz_ss:m_biz_ss,t_biz_aa:m_biz_aa,t_biz_bb:"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
tableSS
=
new
Table
(
"t_biz_ss"
,
"testSchema"
,
null
);
String
expectedTableNameSS
=
"m_biz_ss"
;
Assert
.
assertEquals
(
expectedTableNameSS
,
sinkBuilder
.
getSinkTableName
(
tableSS
));
Table
tableAA
=
new
Table
(
"t_biz_aa"
,
"testSchema"
,
null
);
String
expectedTableNameAA
=
"m_biz_aa"
;
Assert
.
assertEquals
(
expectedTableNameAA
,
sinkBuilder
.
getSinkTableName
(
tableAA
));
// If the rules do not match, the original table name will be used.
Table
tableBB
=
new
Table
(
"t_biz_bb"
,
"testSchema"
,
null
);
String
expectedTableNameBB
=
"m_biz_bb"
;
Assert
.
assertNotEquals
(
expectedTableNameBB
,
sinkBuilder
.
getSinkTableName
(
tableBB
));
}
@Test
public
void
testGetSinkTableNameWithMappingRouteAndReplace
()
{
Map
<
String
,
String
>
sinkConfig
=
new
HashMap
<>();
sinkConfig
.
put
(
"table.mapping-routes"
,
"t_biz_ss:m_biz_ss,t_biz_aa:m_biz_aa,t_biz_bb:"
);
sinkConfig
.
put
(
"table.replace.pattern"
,
"^t_(.*?)"
);
sinkConfig
.
put
(
"table.replace.with"
,
"ods_$1"
);
when
(
config
.
getSink
()).
thenReturn
(
sinkConfig
);
Table
tableSS
=
new
Table
(
"t_biz_ss"
,
"testSchema"
,
null
);
String
expectedTableNameSS
=
"m_biz_ss"
;
Assert
.
assertEquals
(
expectedTableNameSS
,
sinkBuilder
.
getSinkTableName
(
tableSS
));
Table
tableAA
=
new
Table
(
"t_biz_aa"
,
"testSchema"
,
null
);
String
expectedTableNameAA
=
"m_biz_aa"
;
Assert
.
assertEquals
(
expectedTableNameAA
,
sinkBuilder
.
getSinkTableName
(
tableAA
));
// Unmatched rules will use the original table name and match the rule table.replace.pattern="t_(.*?)_", and the
// target will be replaced by ods
Table
tableBB
=
new
Table
(
"t_biz_bb"
,
"testSchema"
,
null
);
String
expectedTableNameBB
=
"ods_biz_bb"
;
Assert
.
assertEquals
(
expectedTableNameBB
,
sinkBuilder
.
getSinkTableName
(
tableBB
));
// Unmatched rules will use the original table name and match the rule table.replace.pattern="t_(.*?)_", and the
// target will be replaced by ods
Table
tableCC
=
new
Table
(
"t_product_spu"
,
"testSchema"
,
null
);
String
expectedTableNameCC
=
"ods_product_spu"
;
Assert
.
assertEquals
(
expectedTableNameCC
,
sinkBuilder
.
getSinkTableName
(
tableCC
));
}
}
class
TestSinkBuilder
extends
AbstractSinkBuilder
{
public
TestSinkBuilder
(
FlinkCDCConfig
config
)
{
super
(
config
);
}
public
TestSinkBuilder
()
{
super
();
}
@Override
public
String
getHandle
()
{
return
"test-sql"
;
}
@Override
public
SinkBuilder
create
(
FlinkCDCConfig
config
)
{
return
new
TestSinkBuilder
(
config
);
}
}
dinky-common/src/main/java/org/dinky/data/model/FlinkCDCConfig.java
View file @
72c765d4
...
@@ -39,6 +39,12 @@ public class FlinkCDCConfig {
...
@@ -39,6 +39,12 @@ public class FlinkCDCConfig {
public
static
final
String
TABLE_LOWER
=
"table.lower"
;
public
static
final
String
TABLE_LOWER
=
"table.lower"
;
public
static
final
String
TABLE_RENAME
=
"table.rename"
;
public
static
final
String
TABLE_RENAME
=
"table.rename"
;
public
static
final
String
TIMEZONE
=
"timezone"
;
public
static
final
String
TIMEZONE
=
"timezone"
;
// 表映射 类似 flink cdc route 映射功能
public
static
final
String
TABLE_MAPPING_ROUTES
=
"table.mapping-routes"
;
// 表名 正则表达式替换 pattern 表达式, 替换规则 with
public
static
final
String
TABLE_REPLACE_PATTERN
=
"table.replace.pattern"
;
public
static
final
String
TABLE_REPLACE_WITH
=
"table.replace.with"
;
private
String
type
;
private
String
type
;
private
String
hostname
;
private
String
hostname
;
private
Integer
port
;
private
Integer
port
;
...
@@ -149,6 +155,9 @@ public class FlinkCDCConfig {
...
@@ -149,6 +155,9 @@ public class FlinkCDCConfig {
case
TABLE_LOWER:
case
TABLE_LOWER:
case
TABLE_RENAME:
case
TABLE_RENAME:
case
TIMEZONE:
case
TIMEZONE:
case
TABLE_REPLACE_PATTERN:
case
TABLE_REPLACE_WITH:
case
TABLE_MAPPING_ROUTES:
return
true
;
return
true
;
default
:
default
:
return
false
;
return
false
;
...
...
docs/docs/practical_guide/cdcsource_practice/cdcsource_mysqlcdc2mysql_regex.md
0 → 100644
View file @
72c765d4
---
sidebar_position
:
6
position
:
6
id
:
cdcsource_mysqlcdc2mysql_regex
title
:
MySQLCDC 整库到 MySQL - 通过正则表达式替换、表映射路由案例
---
## 前置准备
-
请确保已经在 Flink/lib 和 dinky/extends 目录下放置了 Jdbc 的 Flink connector jar。如果提交模式为 Application/Per-Job,请确保 Jdbc connector jar 已经放置在 HDFS 中
-
请确保已经在 Flink/lib 和 dinky/extends 目录下放置了 MySQL CDC 的 Flink connector jar。 如果提交模式为 Application/Per-Job,请确保 MySQL CDC connector jar 已经放置在 HDFS 中
-
如在两方启动后才进行放置上述 jar 包,请重启 Flink 和 Dinky 服务,或者使用 Dinky 中提供的
[
ADD CUSTOMJAR
](
../../extend/expand_statements/add_jar_statement
)
功能进行加载。
## 示例一:使用 `sink.table.replace.pattern` 和 `sink.table.replace.with` 正则匹配和替换
通过正则表达式替换原表名称,并自动创建目标表名。
注意事项:
-
该示例是将 mysql 整库同步到 mysql 表,且写入名为 ods 的库,通过正则表达式匹配
`t_`
并替换成
`ods_`
。
-
该示例参数中的
`#{tableName}`
为占位符,实际执行时会替换为实际表名,如
`ods_products`
、
`ods_orders`
等。
-
该示例 sink 中的各个参数均可根据实际情况进行调整,请按照 mysql 连接器官方文档进行配置。并请遵守整库同步的规范。
> 该示例为将 mysql 整库同步到另一个 mysql 数据库,写入 test 库,表名使用 `sink.table.replace.pattern` 和 `sink.table.replace.with` 正则匹配和替换,表名全小写,开启自动建表。
```
sql showLineNumbers
EXECUTE CDCSOURCE cdc_mysql WITH (
'connector' = 'mysql-cdc',
'hostname' = '127.0.0.1',
'port' = '3306',
'password' = '123456',
'checkpoint' = '3000',
'scan.startup.mode' = 'initial',
'parallelism' = '1',
'table-name' = 'bigdata\.t_products,bigdata\.t_orders',
'sink.connector' = 'jdbc',
'sink.url' = 'jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false',
'sink.username' = 'root',
'sink.password' = '123456',
'username' = 'root',
'sink.sink.db' = 'test',
-- 正则匹配表名,进行替换
'sink.table.replace.pattern' = '^t_(.*?)',
'sink.table.replace.with' = 'ods_$1',
'sink.table.lower' = 'true',
'sink.table-name' = '#{tableName}',
'sink.driver' = 'com.mysql.jdbc.Driver',
'sink.sink.buffer-flush.interval' = '2s',
'sink.sink.buffer-flush.max-rows' = '100',
'sink.sink.max-retries' = '5',.
'sink.auto.create' = 'true'
)
```
## 示例二:使用 `sink.table.mapping-routes` 进行表名和目标表名进行映射替换
> 使用 `sink.table.mapping-routes` 进行表名和目标表名进行映射替换。
**注意:原表和目标表名格式,`k1:v1,k2:v2` 键值对,多张表通过逗号分割。**
```sql showLineNumbers
EXECUTE CDCSOURCE cdc_mysql WITH (
'connector' = 'mysql-cdc',
'hostname' = '127.0.0.1',
'port' = '3306',
'username' = 'root',
'password' = '123456',
'checkpoint' = '3000',
'scan.startup.mode' = 'initial',
'parallelism' = '1',
'table-name' = 'bigdata\.t_example_a,bigdata\.example_b',
'sink.connector' = 'jdbc',
'sink.url' = 'jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false',
'sink.username' = 'root',
'sink.password' = '123456',
'sink.sink.db' = 'test',
-- 映射表名 原表 t_example_a 替换成 test_example_a ,多张表通过逗号分割
'sink.table.mapping-routes' = 't_example_a:test_example_a,example_b:t_example_b',
'sink.table.lower' = 'true',
'sink.table-name' = '#{tableName}',
'sink.driver' = 'com.mysql.jdbc.Driver',
'sink.sink.buffer-flush.interval' = '2s',
'sink.sink.buffer-flush.max-rows' = '100',
'sink.sink.max-retries' = '5',
'sink.auto.create' = 'true'
)
```
## 示例三:这三个参数 `sink.table.mapping-routes` 和 `sink.table.replace.pattern` 和 `sink.table.replace.with` 可以同时搭配使用。
注意: mapping-routes 规则优先级比较高。其次是 replace.pattern 和 replace.with 规则。
```
sql showLineNumbers
EXECUTE CDCSOURCE cdc_mysql WITH (
'connector' = 'mysql-cdc',
'hostname' = '127.0.0.1',
'port' = '3306',
'username' = 'root',
'password' = '123456',
'checkpoint' = '3000',
'scan.startup.mode' = 'initial',
'parallelism' = '1',
'table-name' = 'bigdata\.t_example_a,bigdata\.example_b',
'sink.connector' = 'jdbc',
'sink.url' = 'jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false',
'sink.username' = 'root',
'sink.password' = '123456',
'sink.sink.db' = 'test',
-- 映射表名 原表 t_example_a 替换成 test_example_a ,多张表通过逗号分割
'sink.table.mapping-routes' = 't_example_a:test_example_a,example_b:t_example_b',
-- 正则匹配表名,进行替换
'sink.table.replace.pattern' = '^t_(.*?)', -- replace t_example_b -> ods_example_b , test_example_a 则不会变成 ods_example_a ,因为 table.mapping-routes 优先级较高
'sink.table.replace.with' = 'ods_$1',
'sink.table.lower' = 'true',
'sink.table-name' = '#{tableName}',
'sink.driver' = 'com.mysql.jdbc.Driver',
'sink.sink.buffer-flush.interval' = '2s',
'sink.sink.buffer-flush.max-rows' = '100',
'sink.sink.max-retries' = '5',
'sink.auto.create' = 'true'
)
```
\ No newline at end of file
docs/docs/practical_guide/cdcsource_practice/cdcsource_overview.md
View file @
72c765d4
...
@@ -138,6 +138,9 @@ add customjar 'flink-sql-connector-mysql-cdc-2.3.0.jar'
...
@@ -138,6 +138,9 @@ add customjar 'flink-sql-connector-mysql-cdc-2.3.0.jar'
| sink.table.suffix | 否 | 无 | 目标表的表名后缀 |
| sink.table.suffix | 否 | 无 | 目标表的表名后缀 |
| sink.table.upper | 否 | false | 目标表的表名全大写 |
| sink.table.upper | 否 | false | 目标表的表名全大写 |
| sink.table.lower | 否 | false | 目标表的表名全小写 |
| sink.table.lower | 否 | false | 目标表的表名全小写 |
| sink.table.replace.pattern | 否 | false | 正则表达式,用于匹配原表名,如: t_biz_b
`'sink.table.replace.pattern' = 't_(.*?)_'`
**必须搭配:sink.table.replace.with 一起使用**
|
| sink.table.replace.with | 否 | false | 替换成目标表名格式, 如:
`'sink.table.replace.with' = '$1_new'`
or
`'sink.table.replace.with' = 'ods_$1_'`
|
| sink.table.mapping-routes | 否 | false | 表名映射,
`原表名:映射表名`
。使用方式:
`'sink.table.mapping-routes' = 't_biz_a:ods_biz_a,t_biz_b:ods_biz_b'`
,多张表参数逗号','分割。
**注:如果原表命名过长,可以通过表名映射解决**
|
| sink.auto.create | 否 | false | 目标数据源自动建表,目前只支持 Mysql,其他可自行扩展 |
| sink.auto.create | 否 | false | 目标数据源自动建表,目前只支持 Mysql,其他可自行扩展 |
| sink.timezone | 否 | UTC | 指定目标数据源的时区,在数据类型转换时自动生效 |
| sink.timezone | 否 | UTC | 指定目标数据源的时区,在数据类型转换时自动生效 |
| sink.column.replace.line-break | 否 | false | 指定是否去除换行符,即在数据转换中进行 REGEXP_REPLACE(column, '
\\
n', '') |
| sink.column.replace.line-break | 否 | false | 指定是否去除换行符,即在数据转换中进行 REGEXP_REPLACE(column, '
\\
n', '') |
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment