
message Entry { //Entry is the structure of a single subscription event. One event is equivalent to one binlog event in MySQL.Header header = 1; //Event headerEvent event = 2; //Event body}message Header {int32 version = 1; //Entry protocol versionSourceType sourceType = 2; //The type of the source database, including MySQL, Oracle, and other typesMessageType messageType = 3; //The type of the message, that is, the type of the Event, including BEGIN, COMMIT, DML, etc.uint32 timestamp = 4; //The timestamp of the Event in the original binlogint64 serverId = 5; // The server ID of the sourcestring fileName = 6; // The file name of the source binloguint64 position = 7; // The offset of the event in the source binlog filestring gtid = 8; // The GTID of the current transactionstring schemaName = 9; // The schema affected by the changestring tableName = 10; // The table affected by the changeuint64 seqId = 11; //Globally incremental sequence numberuint64 eventIndex = 12; //If a large event is fragmented, each fragment is numbered from 0. This field is meaningless in the current version and reserved for future extension.bool isLast = 13; //Whether the current event is the last fragment of the event. If so, the value is true. This field is meaningless in the current version and reserved for future extension.repeated KVPair properties = 15;}message Event {BeginEvent beginEvent = 1; //The begin event in the binlogDMLEvent dmlEvent = 2; // The dml event in the binlogCommitEvent commitEvent = 3; // The commit event in the binlogDDLEvent ddlEvent = 4; // The ddl event in the binlogRollbackEvent rollbackEvent = 5; // The rollback event. This field is meaningless in the current version.HeartbeatEvent heartbeatEvent = 6; // The heartbeat event periodically sent by the source databaseCheckpointEvent checkpointEvent = 7; // The checkpoint event added by the subscription backend. One is automatically generated every 10 seconds for Kafka production and consumption offset management.repeated KVPair properties = 15;}
message Entries {repeated Entry items = 1; //entry list}
message Envelope {int32 version = 1; // The protocol version, which determines how the data content is decoded.uint32 total = 2;uint32 index = 3;bytes data = 4; // The current version is 1, indicating that the data in the data field is the result of PB serialization of Entries.repeated KVPair properties = 15;}
// Create a Flink-kafka consumerFlinkKafkaConsumer<RecordMsgObject> consumer =new FlinkKafkaConsumer<>(topic, new DeserializeProtobufToRecordMsgObject(), props);
// A custom deserializer that deserializes raw messages into RecordMsgObject objects.@Overridepublic RecordMsgObject deserialize(ConsumerRecord<byte[], byte[]> record) throws Exception {RecordMsgObject obj = new RecordMsgObject();obj.topic = record.topic();obj.partition = record.partition();obj.offset = record.offset();obj.partitionSeq = getPartitionSeq(record);obj.key = new String(record.key());obj.headers = record.headers();// What is received here is the binary value of Envelope.obj.value = record.value();return obj;}
// Process the received messages by partition.stream.keyBy(RecordMsgObject::getPartition).process(new SubscribeMsgProcess(trans2sql)).setParallelism(1);
// Deserialize the envelope. In this demo, one envelope can store the data of an entire binlog event by default.SubscribeProtobufData.Envelope envelope = SubscribeProtobufData.Envelope.parseFrom(record.value);if (1 != envelope.getVersion()) {throw new IllegalStateException(String.format("unsupported version: %d", envelope.getVersion()));}
// Deserialize to obtain Entries.ByteString envelopeData = envelope.getData();SubscribeProtobufData.Entries entries;if (1 == envelope.getTotal()) {entries = SubscribeProtobufData.Entries.parseFrom(envelopeData.toByteArray());} else {entries = SubscribeProtobufData.Entries.parseFrom(shardMsgMap.get(shardId).toByteArray());shardMsgMap.remove(shardId);}
// Iterate through each Entry and print the sql statement based on the Entry type.for (SubscribeProtobufData.Entry entry : entries.getItemsList()) {onEntry(record.partition, record.offset, ps, entry, trans2sql);}
message Data {DataType dataType = 1;string charset = 2; // The encoding type of DataType_STRING, value stored in bvstring sv = 3; // The string value of DataType_INT8/16/32/64/UINT8/16/32/64/Float32/64/DataType_DECIMALbytes bv = 4; // The value of DataType_STRING/DataType_BYTES}
enum DataType {NIL = 0; // The value is NULL.INT8 = 1;INT16 = 2;INT32 = 3;INT64 = 4;UINT8 = 5;UINT16 = 6;UINT32 = 7;UINT64 = 8;FLOAT32 = 9;FLOAT64 = 10;BYTES = 11;DECIMAL = 12;STRING = 13;NA = 14; // The value does not exist (N/A)}
DATE, TIME, and DATETIME types do not support time zone.TIMESTAMP type supports time zone. For fields of this type: the system will convert the current time zone to Universal Time Coordinated (UTC) when storing them and convert UTC back to the current time zone when querying them.TDSQL MySQL Field Type | Corresponding Protobuf DataType Enumeration Values |
MYSQL_TYPE_NULL | NIL |
MYSQL_TYPE_INT8 | INT8 |
MYSQL_TYPE_INT16 | INT16 |
MYSQL_TYPE_INT24 | INT32 |
MYSQL_TYPE_INT32 | INT32 |
MYSQL_TYPE_INT64 | INT64 |
MYSQL_TYPE_BIT | INT64 |
MYSQL_TYPE_YEAR | INT64 |
MYSQL_TYPE_FLOAT | FLOAT32 |
MYSQL_TYPE_DOUBLE | FLOAT64 |
MYSQL_TYPE_VARCHAR | STRING |
MYSQL_TYPE_STRING | STRING |
MYSQL_TYPE_VAR_STRING | STRING |
MYSQL_TYPE_TIMESTAMP | STRING |
MYSQL_TYPE_DATE | STRING |
MYSQL_TYPE_TIME | STRING |
MYSQL_TYPE_DATETIME | STRING |
MYSQL_TYPE_TIMESTAMP_NEW | STRING |
MYSQL_TYPE_DATE_NEW | STRING |
MYSQL_TYPE_TIME_NEW | STRING |
MYSQL_TYPE_DATETIME_NEW | STRING |
MYSQL_TYPE_ENUM | STRING |
MYSQL_TYPE_SET | STRING |
MYSQL_TYPE_DECIMAL | DECIMAL |
MYSQL_TYPE_DECIMAL_NEW | DECIMAL |
MYSQL_TYPE_JSON | BYTES |
MYSQL_TYPE_BLOB | BYTES |
MYSQL_TYPE_TINY_BLOB | BYTES |
MYSQL_TYPE_MEDIUM_BLOB | BYTES |
MYSQL_TYPE_LONG_BLOB | BYTES |
MYSQL_TYPE_GEOMETRY | BYTES |
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários