Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dsk-dsc-flink
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
shezaixing
dsk-dsc-flink
Commits
ba62a7c4
Commit
ba62a7c4
authored
Jan 16, 2025
by
liaowenwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化代码
parent
e2cad0bc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
17 deletions
+16
-17
MysqlDataTransferFunction.java
.../flink/dsc/common/function/MysqlDataTransferFunction.java
+16
-17
No files found.
src/main/java/com/dsk/flink/dsc/common/function/MysqlDataTransferFunction.java
View file @
ba62a7c4
...
@@ -3,6 +3,8 @@ package com.dsk.flink.dsc.common.function;
...
@@ -3,6 +3,8 @@ package com.dsk.flink.dsc.common.function;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.map.MapUtil
;
import
cn.hutool.core.map.MapUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.dsk.flink.dsc.utils.EnvProperties
;
import
com.dsk.flink.dsc.utils.EnvProperties
;
...
@@ -12,6 +14,7 @@ import org.apache.flink.util.Collector;
...
@@ -12,6 +14,7 @@ import org.apache.flink.util.Collector;
import
java.util.*
;
import
java.util.*
;
/**
/**
* 重构代码
* @author lww
* @author lww
* @date 2025-01-14
* @date 2025-01-14
*/
*/
...
@@ -41,8 +44,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -41,8 +44,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
this
.
dbInfoMap
=
envProps
;
this
.
dbInfoMap
=
envProps
;
}
}
private
static
String
tranferInsertSql
(
String
table
,
JSONObject
dataObj
,
JSONObject
mysqlType
)
{
private
static
String
tranferInsertSql
(
String
table
,
JSONObject
dataObj
,
JSONObject
mysqlType
)
{
Set
<
String
>
columnSet
=
mysqlType
.
keySet
();
Set
<
String
>
columnSet
=
mysqlType
.
keySet
();
StringBuilder
sb
=
new
StringBuilder
(
"REPLACE INTO "
).
append
(
table
).
append
(
" ("
);
StringBuilder
sb
=
new
StringBuilder
(
"REPLACE INTO "
).
append
(
table
).
append
(
" ("
);
...
@@ -58,7 +59,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -58,7 +59,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
return
sb
.
toString
();
return
sb
.
toString
();
}
}
private
static
String
transferDeleteSql
(
String
table
,
JSONObject
dataObj
,
JSONObject
mysqlType
,
Set
<
String
>
pkNameSet
)
{
private
static
String
transferDeleteSql
(
String
table
,
JSONObject
dataObj
,
JSONObject
mysqlType
,
Set
<
String
>
pkNameSet
)
{
StringBuilder
whereClauseBuilder
=
new
StringBuilder
();
StringBuilder
whereClauseBuilder
=
new
StringBuilder
();
for
(
String
pk
:
pkNameSet
)
{
for
(
String
pk
:
pkNameSet
)
{
...
@@ -70,14 +70,7 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -70,14 +70,7 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
return
String
.
format
(
"DELETE FROM %s WHERE %s"
,
table
,
whereClauseBuilder
);
return
String
.
format
(
"DELETE FROM %s WHERE %s"
,
table
,
whereClauseBuilder
);
}
}
/**
* @author shezaixing
* @date 2023/12/7 14:23
* @description 判断拼接字符串时类型(是否需要加上引号)
*
*/
private
static
String
getValueString
(
JSONObject
dataObj
,
String
columnKey
,
String
mysqlType
){
private
static
String
getValueString
(
JSONObject
dataObj
,
String
columnKey
,
String
mysqlType
){
if
(
null
==
dataObj
.
get
(
columnKey
)){
if
(
null
==
dataObj
.
get
(
columnKey
)){
return
"null"
;
return
"null"
;
}
}
...
@@ -100,7 +93,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -100,7 +93,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
public
void
processElement
(
JSONObject
value
,
Context
ctx
,
Collector
<
Tuple3
<
String
,
String
,
Long
>>
out
)
throws
Exception
{
public
void
processElement
(
JSONObject
value
,
Context
ctx
,
Collector
<
Tuple3
<
String
,
String
,
Long
>>
out
)
throws
Exception
{
//返回数据集合
//返回数据集合
String
type
=
value
.
getString
(
"type"
);
String
type
=
value
.
getString
(
"type"
);
//JSONArray dataList = value.getJSONArray("data");
JSONObject
mysqlType
=
value
.
getJSONObject
(
"mysqlType"
);
JSONObject
mysqlType
=
value
.
getJSONObject
(
"mysqlType"
);
String
table
=
value
.
getString
(
"table"
);
String
table
=
value
.
getString
(
"table"
);
JSONArray
pkNames
=
value
.
getJSONArray
(
"pkNames"
);
JSONArray
pkNames
=
value
.
getJSONArray
(
"pkNames"
);
...
@@ -117,9 +109,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -117,9 +109,6 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
mysqlType
.
put
(
"is_del"
,
"int"
);
mysqlType
.
put
(
"is_del"
,
"int"
);
dataObj
.
put
(
"is_del"
,
"DELETE"
.
equals
(
type
)
?
1
:
0
);
dataObj
.
put
(
"is_del"
,
"DELETE"
.
equals
(
type
)
?
1
:
0
);
}
}
//处理先后顺序
//获取该条数据的表名和主键作为唯一的groupKey
StringBuilder
groupKeyBuilder
=
new
StringBuilder
(
table
);
StringBuilder
groupKeyBuilder
=
new
StringBuilder
(
table
);
for
(
String
pk
:
pkNameSet
)
{
for
(
String
pk
:
pkNameSet
)
{
String
pkValue
=
getValueString
(
dataObj
,
pk
,
mysqlType
.
getString
(
pk
));
String
pkValue
=
getValueString
(
dataObj
,
pk
,
mysqlType
.
getString
(
pk
));
...
@@ -128,9 +117,19 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
...
@@ -128,9 +117,19 @@ public class MysqlDataTransferFunction extends ProcessFunction<JSONObject, Tuple
String
groupKey
=
groupKeyBuilder
.
toString
();
String
groupKey
=
groupKeyBuilder
.
toString
();
//添加分表参数
//添加分表参数
//String shardingRule = dataObj.getString("shardingRule");
String
shardingRule
=
dataObj
.
getString
(
"shardingRule"
);
//String shardedTable = dbInfoMap.getSharded_table();
if
(
StrUtil
.
isNotBlank
(
shardingRule
))
{
Map
<
String
,
Object
>
map
=
JSON
.
parseObject
(
shardingRule
,
Map
.
class
);
String
strategy
=
MapUtil
.
getStr
(
map
,
"strategy"
);
Map
<
String
,
Object
>
strategyMap
=
JSON
.
parseObject
(
strategy
,
Map
.
class
);
int
i
=
MapUtil
.
getInt
(
strategyMap
,
"sharding-count"
);
if
(
i
>
1
){
String
str
=
MapUtil
.
getStr
(
strategyMap
,
"sharding-column"
);
Integer
val
=
dataObj
.
getInteger
(
str
);
val
=
val
==
null
?
1
:
val
;
table
=
table
.
concat
(
"_"
).
concat
(
String
.
valueOf
(
val
%
i
));
}
}
if
(
"INSERT"
.
equals
(
type
)
||
"UPDATE"
.
equals
(
type
)){
if
(
"INSERT"
.
equals
(
type
)
||
"UPDATE"
.
equals
(
type
)){
excueteSql
=
tranferInsertSql
(
table
,
dataObj
,
mysqlType
);
excueteSql
=
tranferInsertSql
(
table
,
dataObj
,
mysqlType
);
}
else
{
}
else
{
...
...
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