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
1cd17d96
Commit
1cd17d96
authored
Jan 15, 2025
by
wangpeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加主键KEY
parent
799702f6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
16 deletions
+51
-16
KafkaSinkBuilder.java
...e/src/main/java/org/dinky/cdc/kafka/KafkaSinkBuilder.java
+2
-2
KeySerializationSchema.java
...main/java/org/dinky/cdc/kafka/KeySerializationSchema.java
+49
-14
No files found.
dinky-cdc/dinky-cdc-core/src/main/java/org/dinky/cdc/kafka/KafkaSinkBuilder.java
View file @
1cd17d96
...
...
@@ -115,7 +115,7 @@ public class KafkaSinkBuilder extends AbstractSinkBuilder implements Serializabl
.
setBootstrapServers
(
config
.
getSink
().
get
(
"brokers"
))
.
setRecordSerializer
(
KafkaRecordSerializationSchema
.
builder
()
.
setTopic
(
config
.
getSink
().
get
(
"topic"
))
.
setKeySerializationSchema
(
new
KeySerializationSchema
())
.
setKeySerializationSchema
(
new
KeySerializationSchema
(
null
))
.
setValueSerializationSchema
(
new
SimpleStringSchema
())
.
build
())
.
setDeliverGuarantee
(
kafkaProducerConfig
.
containsKey
(
"transactional.guarantee"
)
?
DeliveryGuarantee
.
valueOf
(
kafkaProducerConfig
.
getProperty
(
"transactional.guarantee"
))
:
DeliveryGuarantee
.
valueOf
(
...
...
@@ -184,7 +184,7 @@ public class KafkaSinkBuilder extends AbstractSinkBuilder implements Serializabl
.
setBootstrapServers
(
config
.
getSink
().
get
(
"brokers"
))
.
setRecordSerializer
(
KafkaRecordSerializationSchema
.
builder
()
.
setTopic
(
topic
)
.
setKeySerializationSchema
(
new
KeySerializationSchema
())
.
setKeySerializationSchema
(
new
KeySerializationSchema
(
k
))
.
setValueSerializationSchema
(
new
SimpleStringSchema
())
.
build
())
.
setDeliverGuarantee
(
kafkaProducerConfig
.
containsKey
(
"transactional.guarantee"
)
?
DeliveryGuarantee
.
valueOf
(
kafkaProducerConfig
.
getProperty
(
"transactional.guarantee"
))
:
DeliveryGuarantee
.
valueOf
(
...
...
dinky-cdc/dinky-cdc-core/src/main/java/org/dinky/cdc/kafka/KeySerializationSchema.java
View file @
1cd17d96
...
...
@@ -2,36 +2,70 @@ package org.dinky.cdc.kafka;
import
org.apache.flink.api.common.serialization.SerializationSchema
;
import
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper
;
import
org.dinky.data.model.Column
;
import
org.dinky.data.model.Table
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.nio.charset.StandardCharsets
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Objects
;
public
class
KeySerializationSchema
implements
SerializationSchema
<
String
>
{
public
class
KeySerializationSchema
implements
SerializationSchema
<
String
>
{
protected
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
KeySerializationSchema
.
class
);
private
static
final
ObjectMapper
OBJECT_MAPPER
=
new
ObjectMapper
();
private
Table
table
;
public
KeySerializationSchema
(
Table
table
)
{
this
.
table
=
table
;
}
@Override
public
byte
[]
serialize
(
String
element
)
{
try
{
Map
<
String
,
Object
>
map
=
OBJECT_MAPPER
.
readValue
(
element
,
Map
.
class
);
LinkedHashMap
source
=
(
LinkedHashMap
)
map
.
get
(
"source"
);
String
db
=
source
.
get
(
"db"
).
toString
();
String
tableName
=
source
.
get
(
"table"
).
toString
();
String
schema
=
source
.
containsKey
(
"schema"
)
?
source
.
get
(
"schema"
).
toString
()
:
""
;
if
(
schema
.
isEmpty
())
{
return
(
db
+
"."
+
tableName
).
getBytes
();
}
else
{
return
(
db
+
"."
+
schema
+
"."
+
tableName
).
getBytes
();
}
if
(
Objects
.
isNull
(
table
))
return
topicKey
(
map
);
return
multiTopic
(
table
,
map
);
}
catch
(
Exception
e
)
{
// 记录更详细的日志信息
System
.
err
.
println
(
"Failed to serialize element: "
+
element
);
logger
.
error
(
"Failed to serialize element: "
+
element
);
e
.
printStackTrace
();
// 返回空字节数组,避免返回 null
return
new
byte
[
0
];
}
}
private
byte
[]
multiTopic
(
Table
table
,
Map
<
String
,
Object
>
map
)
{
List
<
Column
>
columns
=
table
.
getColumns
();
String
key
=
""
;
for
(
Column
column
:
columns
)
{
if
(
column
.
isKeyFlag
())
{
key
=
column
.
getName
();
break
;
}
}
if
(
""
.
equals
(
key
))
topicKey
(
map
);
Object
after
=
map
.
get
(
"after"
);
if
(
Objects
.
nonNull
(
after
))
{
Map
<
String
,
Object
>
afterMap
=
(
Map
<
String
,
Object
>)
after
;
Object
keyValue
=
afterMap
.
get
(
key
);
if
(
Objects
.
nonNull
(
keyValue
))
return
String
.
valueOf
(
keyValue
).
getBytes
(
StandardCharsets
.
UTF_8
);
}
return
topicKey
(
map
);
}
private
byte
[]
topicKey
(
Map
<
String
,
Object
>
map
)
{
LinkedHashMap
source
=
(
LinkedHashMap
)
map
.
get
(
"source"
);
String
db
=
source
.
get
(
"db"
).
toString
();
String
tableName
=
source
.
get
(
"table"
).
toString
();
String
schema
=
source
.
containsKey
(
"schema"
)
?
source
.
get
(
"schema"
).
toString
()
:
""
;
if
(
schema
.
isEmpty
())
{
return
(
db
+
"."
+
tableName
).
getBytes
();
}
else
{
return
(
db
+
"."
+
schema
+
"."
+
tableName
).
getBytes
();
}
}
}
\ No newline at end of file
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