mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
@@ -3,26 +3,51 @@ package com.alibaba.otter.canal.parse.driver.mysql;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket;
|
||||||
|
|
||||||
public class MysqlConnectorTest {
|
public class MysqlConnectorTest {
|
||||||
|
|
||||||
public static void main(String args[]) {
|
@Test
|
||||||
MysqlConnector connector = new MysqlConnector(new InetSocketAddress("yddb01.mysql.database.chinacloudapi.cn",
|
public void testQuery() {
|
||||||
3306), "ps-admin01@yddb01", "1qaz3edc");
|
|
||||||
|
MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "xxxxx", "xxxxx");
|
||||||
try {
|
try {
|
||||||
connector.connect();
|
connector.connect();
|
||||||
MysqlQueryExecutor executor = new MysqlQueryExecutor(connector);
|
MysqlQueryExecutor executor = new MysqlQueryExecutor(connector);
|
||||||
ResultSetPacket result = executor.query("show variables like '%char%';");
|
ResultSetPacket result = executor.query("show variables like '%char%';");
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
|
result = executor.query("select * from test.test1");
|
||||||
|
System.out.println(result);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
Assert.fail(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
connector.disconnect();
|
connector.disconnect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Assert.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
public void testUpdate() {
|
||||||
|
|
||||||
|
MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "xxxxx", "xxxxx");
|
||||||
|
try {
|
||||||
|
connector.connect();
|
||||||
|
MysqlUpdateExecutor executor = new MysqlUpdateExecutor(connector);
|
||||||
|
executor.update("insert into test.test2(id,name,score,text_value) values(null,'中文1',10,'中文2')");
|
||||||
|
} catch (IOException e) {
|
||||||
|
Assert.fail(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
connector.disconnect();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Assert.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ public class CanalKafkaProducer implements CanalMQProducer {
|
|||||||
private void send(MQProperties.CanalDestination canalDestination, String topicName, Message message)
|
private void send(MQProperties.CanalDestination canalDestination, String topicName, Message message)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
if (!kafkaProperties.getFlatMessage()) {
|
if (!kafkaProperties.getFlatMessage()) {
|
||||||
List<ProducerRecord<String, Message>> records = new ArrayList<>();
|
List<ProducerRecord<String, Message>> records = new ArrayList<ProducerRecord<String, Message>>();
|
||||||
if (canalDestination.getPartitionHash() != null && !canalDestination.getPartitionHash().isEmpty()) {
|
if (canalDestination.getPartitionHash() != null && !canalDestination.getPartitionHash().isEmpty()) {
|
||||||
Message[] messages = MQMessageUtils.messagePartition(message,
|
Message[] messages = MQMessageUtils.messagePartition(message,
|
||||||
canalDestination.getPartitionsNum(),
|
canalDestination.getPartitionsNum(),
|
||||||
@@ -148,12 +148,12 @@ public class CanalKafkaProducer implements CanalMQProducer {
|
|||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
Message messagePartition = messages[i];
|
Message messagePartition = messages[i];
|
||||||
if (messagePartition != null) {
|
if (messagePartition != null) {
|
||||||
records.add(new ProducerRecord<>(topicName, i, null, messagePartition));
|
records.add(new ProducerRecord<String, Message>(topicName, i, null, messagePartition));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final int partition = canalDestination.getPartition() != null ? canalDestination.getPartition() : 0;
|
final int partition = canalDestination.getPartition() != null ? canalDestination.getPartition() : 0;
|
||||||
records.add(new ProducerRecord<>(topicName, partition, null, message));
|
records.add(new ProducerRecord<String, Message>(topicName, partition, null, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!records.isEmpty()) {
|
if (!records.isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user