-
Notifications
You must be signed in to change notification settings - Fork 52
/
MaxwellPipeLine.java
174 lines (140 loc) · 5 KB
/
MaxwellPipeLine.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
package cn.hashdata.bireme.pipeline;
import java.util.Arrays;
import java.util.HashMap;
import org.apache.commons.codec.binary.Base64;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.TopicPartition;
import org.apache.logging.log4j.LogManager;
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 MaxwellPipeLine} is a kind of {@code KafkaPipeLine} whose change data coming from Maxwell.
*
* @author yuze
*
*/
public class MaxwellPipeLine extends KafkaPipeLine {
public MaxwellPipeLine(Context cxt, SourceConfig conf, int partitionID) {
super(cxt, conf, "Maxwell-" + conf.name + "-" + conf.topic + "-" + partitionID);
consumer.assign(Arrays.asList(new TopicPartition(conf.topic, partitionID)));
logger = LogManager.getLogger("Bireme." + myName);
logger.info("Create new Maxwell Pipeline. Name: {}", myName);
}
@Override
public Transformer createTransformer() {
return new MaxwellTransformer();
}
/**
* {@code MaxwellChangeTransformer} is a type of {@code Transformer}. It is used to transform data
* to {@code Row} from <B>Maxwell</B> data source.
*
* @author yuze
*
*/
class MaxwellTransformer extends KafkaTransformer {
HashMap<String, String> tableMap;
public MaxwellTransformer() {
super();
tableMap = conf.tableMap;
}
private String getMappedTableName(MaxwellRecord record) {
return cxt.tableMap.get(record.dataSource + "." + record.database + "." + record.table);
}
private String getOriginTableName(MaxwellRecord record) {
return record.dataSource + "." + record.database + "." + record.table;
}
private boolean filter(MaxwellRecord record) {
String fullTableName = record.dataSource + "." + record.database + "." + record.table;
if (!tableMap.containsKey(fullTableName)) {
return true;
}
return false;
}
@Override
protected byte[] decodeToBinary(String data) {
byte[] decoded = null;
decoded = Base64.decodeBase64(data);
return decoded;
}
@Override
protected String decodeToBit(String data, int precision) {
String binaryStr = Integer.toBinaryString(Integer.valueOf(data));
return String.format("%" + precision + "s", binaryStr).replace(' ', '0');
}
@Override
public boolean transform(ConsumerRecord<String, String> change, Row row)
throws BiremeException {
MaxwellRecord record = new MaxwellRecord(change.value());
if (filter(record)) {
return false;
}
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.INSERT || row.type == RowType.UPDATE) {
row.tuple = formatColumns(record, table, table.columnName, false);
}
if (row.type == RowType.UPDATE) {
row.oldKeys = formatColumns(record, table, table.keyNames, true);
if (row.keys.equals(row.oldKeys)) {
row.oldKeys = null;
}
}
return true;
}
class MaxwellRecord implements Record {
public String dataSource;
public String database;
public String table;
public Long produceTime;
public RowType type;
public JsonObject data;
public JsonObject old;
public MaxwellRecord(String changeValue) {
JsonParser jsonParser = new JsonParser();
JsonObject value = (JsonObject) jsonParser.parse(changeValue);
this.dataSource = getPipeLineName();
this.database = value.get("database").getAsString();
this.table = value.get("table").getAsString();
this.produceTime = value.get("ts").getAsLong();
this.data = value.get("data").getAsJsonObject();
if (value.has("old") && !value.get("old").isJsonNull()) {
this.old = value.get("old").getAsJsonObject();
}
switch (value.get("type").getAsString()) {
case "insert":
type = RowType.INSERT;
break;
case "update":
type = RowType.UPDATE;
break;
case "delete":
type = RowType.DELETE;
break;
}
}
@Override
public String getField(String fieldName, boolean oldValue) throws BiremeException {
String field = null;
if (oldValue) {
try {
field = BiremeUtility.jsonGetIgnoreCase(old, fieldName);
return field;
} catch (BiremeException ignore) {
}
}
return BiremeUtility.jsonGetIgnoreCase(data, fieldName);
}
}
}
}