-
Notifications
You must be signed in to change notification settings - Fork 52
/
DebeziumPipeLine.java
236 lines (193 loc) · 6.72 KB
/
DebeziumPipeLine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package cn.hashdata.bireme.pipeline;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.TopicPartition;
import org.apache.logging.log4j.LogManager;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import cn.hashdata.bireme.BiremeException;
import cn.hashdata.bireme.BiremeUtility;
import cn.hashdata.bireme.Context;
import cn.hashdata.bireme.Record;
import cn.hashdata.bireme.Row;
import cn.hashdata.bireme.Table;
import cn.hashdata.bireme.Row.RowType;
/**
* {@code DebeziumPipeLine} is a kind of {@code KafkaPipeLine} whose change data coming from
* Debezium.
*
* @author yuze
*
*/
public class DebeziumPipeLine extends KafkaPipeLine {
public DebeziumPipeLine(Context cxt, SourceConfig conf, String topic) {
super(cxt, conf, "Debezium-" + conf.name + "-" + topic);
List<TopicPartition> topicPartition =
consumer.partitionsFor(topic)
.stream()
.map(p -> new TopicPartition(p.topic(), p.partition()))
.collect(Collectors.toList());
consumer.assign(topicPartition);
logger = LogManager.getLogger("Bireme." + myName);
logger.info("Create new Debezium PipeLine. Name: {}", myName);
if (topicPartition.size() > 1) {
logger.warn("Topic {} has {} partitions", topic, topicPartition.size());
}
}
@Override
public Transformer createTransformer() {
return new DebeziumTransformer();
}
class DebeziumTransformer extends KafkaTransformer {
public DebeziumTransformer() {
super();
}
private String getMappedTableName(DebeziumRecord record) {
return cxt.tableMap.get(record.topic);
}
private String getOriginTableName(DebeziumRecord record) {
return record.topic;
}
@Override
public boolean transform(ConsumerRecord<String, String> change, Row row)
throws BiremeException {
JsonParser jsonParser = new JsonParser();
JsonObject value = (JsonObject) jsonParser.parse(change.value());
if (!value.has("payload") || value.get("payload").isJsonNull()) {
return false;
}
JsonObject payLoad = value.getAsJsonObject("payload");
DebeziumRecord record = new DebeziumRecord(change.topic(), payLoad);
Table table = cxt.tablesInfo.get(getMappedTableName(record));
row.type = record.type;
row.produceTime = record.produceTime;
row.originTable = getOriginTableName(record);
row.mappedTable = getMappedTableName(record);
row.keys = formatColumns(record, table, table.keyNames, false);
if (row.type != RowType.DELETE) {
row.tuple = formatColumns(record, table, table.columnName, false);
}
return true;
}
@Override
protected byte[] decodeToBinary(String data) {
byte[] decoded = null;
decoded = Base64.decodeBase64(data);
return decoded;
}
@Override
protected String decodeToBit(String data, int precision) {
switch (data) {
case "true":
return "1";
case "false":
return "0";
}
StringBuilder sb = new StringBuilder();
String oneByte;
String result;
byte[] decoded = Base64.decodeBase64(data);
ArrayUtils.reverse(decoded);
for (byte b : decoded) {
oneByte = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
sb.append(oneByte);
}
result = sb.toString();
return result.substring(result.length() - precision);
}
@Override
protected String decodeToTime(String data, int fieldType, int precision) {
StringBuilder sb = new StringBuilder();
switch (fieldType) {
case Types.TIME:
case Types.TIMESTAMP: {
// For TIMETZ and TIMESTAMPTZ, debezium will send the right format which can be load
// directly.
if (data.contains(String.valueOf('Z'))) {
return data;
}
int sec = Integer.parseInt(data.substring(0, data.length() - 9));
String fraction = data.substring(data.length() - 9, data.length() - 9 + precision);
Date d = new Date(sec * 1000L);
SimpleDateFormat df;
if (fieldType == Types.TIME) {
df = new SimpleDateFormat("HH:mm:ss");
} else {
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
df.setTimeZone(TimeZone.getTimeZone("GMT"));
sb.append(df.format(d));
sb.append('.' + fraction);
break;
}
case Types.DATE: {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse("1970-01-01"));
} catch (ParseException e) {
logger.error("Can not decode Data/Time {}, message{}.", data, e.getMessage());
return "";
}
c.add(Calendar.DATE, Integer.parseInt(data));
sb.append(sdf.format(c.getTime()));
break;
}
default:
sb.append(data);
break;
}
return sb.toString();
}
@Override
protected String decodeToNumeric(String data, int fieldType, int precision) {
byte[] value = Base64.decodeBase64(data);
BigDecimal bigDecimal = new BigDecimal(new BigInteger(value), precision);
return bigDecimal.toString();
}
class DebeziumRecord implements Record {
public String topic;
public Long produceTime;
public RowType type;
public JsonObject data;
public DebeziumRecord(String topic, JsonObject payLoad) {
this.topic = topic;
char op = payLoad.get("op").getAsCharacter();
this.produceTime = payLoad.get("ts_ms").getAsLong();
JsonElement element = null;
switch (op) {
case 'r':
case 'c':
type = RowType.INSERT;
element = payLoad.get("after");
break;
case 'u':
type = RowType.UPDATE;
element = payLoad.get("after");
break;
case 'd':
type = RowType.DELETE;
element = payLoad.get("before");
break;
}
this.data = element.getAsJsonObject();
}
@Override
public String getField(String fieldName, boolean oldValue) throws BiremeException {
return BiremeUtility.jsonGetIgnoreCase(data, fieldName);
}
}
}
}