Files
CanalSharp/sample/CanalSharp.SimpleClient/Program.cs
T
2018-09-20 16:01:20 +08:00

89 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CanalSharp.Client.Impl;
using Com.Alibaba.Otter.Canal.Protocol;
namespace CanalSharp.SimpleClient
{
class Program
{
static void Main(string[] args)
{
var destination = "example";
var connector = CanalConnectors.NewSingleConnector("127.0.0.1", 11111, destination, "", "");
connector.Connect();
connector.Subscribe();
while (true)
{
var message = connector.Get(5000);
var batchId = message.Id;
if (batchId == -1 || message.Entries.Count <= 0)
{
Console.WriteLine("没有数据了.............");
Thread.Sleep(300);
}
PrintEntry(message.Entries);
}
}
private static void PrintEntry(List<Entry> entrys)
{
foreach (var entry in entrys)
{
if (entry.EntryType == EntryType.Transactionbegin || entry.EntryType == EntryType.Transactionend)
{
continue;
}
RowChange rowChange = null;
try
{
rowChange = RowChange.Parser.ParseFrom(entry.StoreValue);
}
catch (Exception e)
{
}
if (rowChange != null)
{
EventType eventType = rowChange.EventType;
Console.WriteLine(
$"================> binlog[{entry.Header.LogfileName}:{entry.Header.LogfileOffset}] , name[{entry.Header.SchemaName},{entry.Header.TableName}] , eventType :{eventType}");
foreach (var rowData in rowChange.RowDatas)
{
if (eventType == EventType.Delete)
{
PrintColumn(rowData.BeforeColumns.ToList());
}
else if (eventType == EventType.Insert)
{
PrintColumn(rowData.BeforeColumns.ToList());
}
else
{
Console.WriteLine("-------> before");
PrintColumn(rowData.BeforeColumns.ToList());
Console.WriteLine("-------> after");
PrintColumn(rowData.AfterColumns.ToList());
}
}
}
}
}
private static void PrintColumn(List<Column> columns)
{
foreach (var column in columns)
{
Console.WriteLine($"{column.Name} : {column.Value} update= {column.Updated}");
}
}
}
}