コード例 #1
0
        // 发送Update事件类型的变更记录到指定的数据存储中
        private void PostUpdatedRecords(List <Column> beforeColumns, List <Column> afterColumns, Entry entry)
        {
            _canalLogger?.LogDebug($"[{_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 = CanalRepositoryFactory.GetCanalRepositoryInstance(_options);
            _canalRepository.SaveChangeHistoriesAsync(changeLogs);

            _canalLogger?.LogDebug($"[{_xdpCanalOption.LogSource}]", $"### One Update event on {entry.Header.SchemaName} recorded.");
        }
コード例 #2
0
        public void Initialize()
        {
            _canalLogger?.LogDebug("Starting to initialize log database.");

            _canalRepository = CanalRepositoryFactory.GetCanalRepositoryInstance(_options);
            _canalRepository.InitializeAsync();

            _canalLogger?.LogDebug("Finished to initialize log database.");
        }
コード例 #3
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.");
        }