Exemplo n.º 1
0
        // 发送Update事件类型的变更记录到指定的数据存储中
        private void PostUpdatedRecords(List <Column> beforeColumns, List <Column> afterColumns, Entry entry)
        {
            _canalLogger?.LogInformation($"[{_xdpCanalOption.LogSource}]", $"### One Update event on {entry.Header.SchemaName} recording.");

            List <ChangeLog> changeLogs = new List <ChangeLog>();

            foreach (var before in beforeColumns)
            {
                foreach (var after in afterColumns)
                {
                    if (after.Updated && before.Index == after.Index)
                    {
                        ChangeLog changeLog = new ChangeLog
                        {
                            SchemaName    = entry.Header.SchemaName,
                            TableName     = entry.Header.TableName,
                            EventType     = entry.Header.EventType.ToString(),
                            PreviousValue = before.Value,
                            CurrentValue  = after.Value,
                            ExecuteTime   = DateConvertUtil.ToDateTime(entry.Header.ExecuteTime),
                            ColumnName    = after.Name
                        };

                        changeLogs.Add(changeLog);
                    }
                }
            }

            _canalRepository = new MySqlCanalRepository(_options);
            _canalRepository.SaveChangeHistoriesAsync(changeLogs);

            _canalLogger?.LogInformation($"[{_xdpCanalOption.LogSource}]", $"### One Update event on {entry.Header.SchemaName} recorded.");
        }
Exemplo n.º 2
0
        // 发送Insert或Delete事件类型的变更记录到指定服务的数据存储中
        private void PostRecords(List <Column> columns, Entry entry, string eventType)
        {
            _canalLogger?.LogDebug($"[{_xdpCanalOption.LogSource}]", $"### One {eventType} event on {entry.Header.SchemaName} recording.");

            StringBuilder recordBuilder = new StringBuilder();

            recordBuilder.Append("{");

            for (int i = 0; i < columns.Count; i++)
            {
                var column = columns[i];
                if (i == columns.Count - 1)
                {
                    recordBuilder.Append($"\"{column.Name}\":{column.Value ?? string.Empty}");
                }
                else
                {
                    recordBuilder.Append($"\"{column.Name}\":{column.Value ?? string.Empty},");
                }
            }

            recordBuilder.Append("}");

            List <ChangeLog> changeLogs = new List <ChangeLog>();
            ChangeLog        changeLog  = new ChangeLog
            {
                SchemaName  = entry.Header.SchemaName,
                TableName   = entry.Header.TableName,
                EventType   = entry.Header.EventType.ToString(),
                ExecuteTime = DateConvertUtil.ToDateTime(entry.Header.ExecuteTime)
            };

            switch (entry.Header.EventType)
            {
            case EventType.Insert:
                changeLog.CurrentValue = recordBuilder.ToString();
                break;

            case EventType.Delete:
                changeLog.PreviousValue = recordBuilder.ToString();
                break;
            }

            changeLogs.Add(changeLog);
            _canalRepository = CanalRepositoryFactory.GetCanalRepositoryInstance(_options);
            _canalRepository.SaveChangeHistoriesAsync(changeLogs);

            _canalLogger?.LogDebug($"[{_xdpCanalOption.LogSource}]", $"### One {eventType} event on {entry.Header.SchemaName} recorded.");
        }
Exemplo n.º 3
0
        // 发送Insert & Delete事件类型的变更记录到指定服务的数据存储中
        private void PostRecords(List <Column> columns, Entry entry)
        {
            _canalLogger?.LogInformation($"[{_xdpCanalOption.LogSource}]", $"### One Insert/Delete event on {entry.Header.SchemaName} recording.");

            List <ColumnChange> columnChanges = new List <ColumnChange>();

            foreach (var column in columns)
            {
                columnChanges.Add(new ColumnChange()
                {
                    ColumnName  = column.Name,
                    ColumnValue = column.Value
                });
            }

            List <ChangeLog> changLogs = new List <ChangeLog>();
            ChangeLog        changeLog = new ChangeLog
            {
                SchemaName  = entry.Header.SchemaName,
                TableName   = entry.Header.TableName,
                EventType   = entry.Header.EventType.ToString(),
                ExecuteTime = DateConvertUtil.ToDateTime(entry.Header.ExecuteTime)
            };

            switch (entry.Header.EventType)
            {
            case EventType.Insert:
                changeLog.CurrentValue = JsonConvert.SerializeObject(columnChanges);
                break;

            case EventType.Delete:
                changeLog.PreviousValue = JsonConvert.SerializeObject(columnChanges);
                break;
            }

            changLogs.Add(changeLog);
            _canalRepository = new MySqlCanalRepository(_options);
            _canalRepository.SaveChangeHistoriesAsync(changLogs);

            _canalLogger?.LogInformation($"[{_xdpCanalOption.LogSource}]", $"### One Insert/Delete event on {entry.Header.SchemaName} recorded.");
        }