forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimization of binlog to pulsar use canal (apache#3268)
### Motivation Optimization of mysql binlog to pulsar use canal ### Modifications Add MessageUtils for parse columns Support query of presto sql ### Result #### INSERT mysql: ``` MariaDB [aaa]> insert into users321(name, extra) values('xxxddxxxmm', 'ddd'); ``` python consumer: ``` {u'timestamp': u'2018-12-31 00:31:24', u'message': u'[{"data":null,"database":"","es":1546234283000,"id":58,"isDdl":false,"mysqlType":null,"old":null,"sql":"insert into users321(name, extra) values(\'xxxddxxxmm\', \'ddd\')","sqlType":null,"table":"","ts":1546234284669,"type":"QUERY"},{"data":[{"isKey":"1","isNull":"0","index":"0","mysqlType":"int(11)","columnName":"id","columnValue":"34","updated":"1"},{"isKey":"0","isNull":"0","index":"1","mysqlType":"varchar(50)","columnName":"name","columnValue":"xxxddxxxmm","updated":"1"},{"isKey":"0","isNull":"0","index":"2","mysqlType":"varchar(50)","columnName":"extra","columnValue":"ddd","updated":"1"}],"database":"aaa","es":1546234283000,"id":58,"isDdl":false,"mysqlType":null,"old":null,"sql":"","sqlType":null,"table":"users321","ts":1546234284670,"type":"INSERT"}]', u'id': 58} ``` presto sql: ``` presto> select id, timestamp, message from pulsar."public/default".my_topic_test where id=58; id | timestamp | ----+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 58 | 2018-12-31 00:31:24 | [{"data":null,"database":"","es":1546234283000,"id":58,"isDdl":false,"mysqlType":null,"old":null,"sql":"insert into users321(name, extra) values('xxxddxxxmm', 'ddd')","sqlType" ```
- Loading branch information
Showing
5 changed files
with
309 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
pulsar-io/canal/src/main/java/org/apache/pulsar/io/canal/CanalByteSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.canal; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.serializer.SerializerFeature; | ||
import com.alibaba.otter.canal.protocol.Message; | ||
import com.alibaba.otter.canal.protocol.FlatMessage; | ||
import org.apache.pulsar.io.core.annotations.Connector; | ||
import org.apache.pulsar.io.core.annotations.IOType; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A Simple class for mysql binlog sync to pulsar. | ||
*/ | ||
@Connector( | ||
name = "canal", | ||
type = IOType.SOURCE, | ||
help = "The CanalByteSource is used for syncing mysql binlog to Pulsar.", | ||
configClass = CanalSourceConfig.class) | ||
public class CanalByteSource extends CanalAbstractSource<byte[]> { | ||
|
||
@Override | ||
public Long getMessageId(Message message) { | ||
return message.getId(); | ||
} | ||
|
||
@Override | ||
public byte[] extractValue(List<FlatMessage> flatMessages) { | ||
String messages = JSON.toJSONString(flatMessages, SerializerFeature.WriteMapNullValue); | ||
return messages.getBytes(); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
pulsar-io/canal/src/main/java/org/apache/pulsar/io/canal/CanalStringSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.canal; | ||
|
||
import com.alibaba.fastjson.serializer.SerializerFeature; | ||
import com.alibaba.otter.canal.protocol.FlatMessage; | ||
import com.alibaba.otter.canal.protocol.Message; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.List; | ||
import com.alibaba.fastjson.JSON; | ||
import org.apache.pulsar.io.core.annotations.Connector; | ||
import org.apache.pulsar.io.core.annotations.IOType; | ||
|
||
/** | ||
* A Simple class for mysql binlog sync to pulsar. | ||
*/ | ||
@Connector( | ||
name = "canal", | ||
type = IOType.SOURCE, | ||
help = "The CanalStringSource is used for syncing mysql binlog to Pulsar, easy to use presto sql search.", | ||
configClass = CanalSourceConfig.class) | ||
public class CanalStringSource extends CanalAbstractSource<CanalMessage> { | ||
|
||
private Long messageId; | ||
|
||
@Override | ||
public Long getMessageId(Message message) { | ||
this.messageId = message.getId(); | ||
return this.messageId; | ||
} | ||
|
||
@Override | ||
public CanalMessage extractValue(List<FlatMessage> flatMessages) { | ||
String messages = JSON.toJSONString(flatMessages, SerializerFeature.WriteMapNullValue); | ||
CanalMessage canalMessage = new CanalMessage(); | ||
Date date = new Date(); | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
canalMessage.setTimestamp(dateFormat.format(date)); | ||
canalMessage.setId(this.messageId); | ||
canalMessage.setMessage(messages); | ||
return canalMessage; | ||
} | ||
|
||
} | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
class CanalMessage { | ||
private Long id; | ||
private String message; | ||
private String timestamp; | ||
} |
Oops, something went wrong.