什么是 debezium 解决方案来显示谁以及何时删除记录日志?

Posted

技术标签:

【中文标题】什么是 debezium 解决方案来显示谁以及何时删除记录日志?【英文标题】:What is debezium solution to show who and when deleted records logs? 【发布时间】:2018-05-13 03:57:41 【问题描述】:

我有 spring boot + hibernate 应用程序,现在我想使用 debezium 来捕捉日期变化。 假设这个类是我的 POJO 实体类之一:

@Entity
@Table(name = "app_user")
public class User 
    @Id @GeneratedValue
    @Column(name = "id")
    private long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String firstName;

    // getters and setters are omitted for brevity


@Entity
@Table(name = "app_article")
public class Article 
    @Id @GeneratedValue
    @Column(name = "id")
    private long id;

    @Column(name = "title")
    private String title;

    @Column(name = "category")
    private String category;

    @ManyToOne
    @JoinColumn(name = "created_by")
    private User createdBy;

    @ManyToOne
    @JoinColumn(name = "updated_by")
    private User updatedBy;

    @Column(name = "created_date")
    private Date createdDate;

    @Column(name = "updated_date")
    private Date updatedDate;

    // getters and setters are omitted for brevity

现在,如果我创建新的 Article 对象并使用如下代码将其持久化:

Article article = new Article();

article.setId(1);
article.setTitle("Introduction to Debezium");
article.setCategory("auditing");
article.setCreatedBy(Utils.getCurrentUser());
article.setUpdatedBy(Utils.getCurrentUser());
article.setCreatedDate(new Date());
article.setUpdatedDate(new Date());

entityManager.persist(article);

然后再次加载并更改一些字段并将其持久化:

Article art = entityManager.load(Article.class, 1);

art.setTitle("Introduction to Debezium for beginners");
art.setCategory("auditing and cdc");
art.setCreatedBy(Utils.getCurrentUser());
art.setUpdatedBy(Utils.getCurrentUser());
art.setCreatedDate(new Date());
art.setUpdatedDate(new Date());

entityManager.persist(art);

然后删除它:

entityManager.delete(Article.class, 1);

我可能会在每个步骤中看到这样的日志:

// create

    "payload" : 
        "before" : null,
        "after" : 
            "id" : 1,
            "title" : "Introduction to Debezium",
            "category" : "auditing",
            "created_by" : 1,
            "updated_by" : 1,
            "created_date" : "11/29/2017",
            "updated_date" : "11/29/2017"
        ,
        "source" : 
            "name" : "dbserver1",
            "server_id" : 223344,
            "ts_sec" : 1500369632,
            "gtid" : null,
            "file" : "mysql-bin.000003",
            "pos" : 364,
            "row" : 0,
            "snapshot" : null,
            "thread" : 13,
            "db" : "app",
            "table" : "app_article"
        ,
        "op" : "c",
        "ts_ms" : 1500369632095
    


// update

    "payload" : 
        "before" : 
            "id" : 1,
            "title" : "Introduction to Debezium",
            "category" : "auditing",
            "created_by" : 1,
            "updated_by" : 1,
            "created_date" : "11/29/2017",
            "updated_date" : "11/29/2017"
        
        "after" : 
            "id" : 1,
            "title" : "Introduction to Debezium for beginners",
            "category" : "auditing and cdc",
            "created_by" : 1,
            "updated_by" : 1,
            "created_date" : "11/29/2017",
            "updated_date" : "11/29/2017"
        ,
        "source" : 
            "name" : "dbserver1",
            "server_id" : 223344,
            "ts_sec" : 1500369632,
            "gtid" : null,
            "file" : "mysql-bin.000003",
            "pos" : 364,
            "row" : 0,
            "snapshot" : null,
            "thread" : 13,
            "db" : "app",
            "table" : "app_article"
        ,
        "op" : "u",
        "ts_ms" : 1500369632095
    


// delete

    "payload" : 
        "before" : 
            "id" : 1,
            "title" : "Introduction to Debezium for beginners",
            "category" : "auditing and cdc",
            "created_by" : 1,
            "updated_by" : 1,
            "created_date" : "11/29/2017",
            "updated_date" : "11/29/2017"
        ,
        "after": null,
        "source" : 
            "name" : "dbserver1",
            "server_id" : 223344,
            "ts_sec" : 1500369632,
            "gtid" : null,
            "file" : "mysql-bin.000003",
            "pos" : 364,
            "row" : 0,
            "snapshot" : null,
            "thread" : 13,
            "db" : "app",
            "table" : "app_article"
        ,
        "op" : "d",
        "ts_ms" : 1500369632095
    

正如您在上面看到的,我无法检测到如何从数据库中删除记录。我怎么解决这个问题?我应该使用 Hibernate Enver 之类的东西吗 补充一下?

【问题讨论】:

【参考方案1】:

您可以使用 逻辑 删除,这实际上不会从您的表中删除 Article 记录,而只是设置一个 deleted 标志。这样,用户将成为传输的更新事件的一部分。

然后您可以实施某种(异步运行的)内务处理,以物理方式删除记录。执行此删除操作的(技术)用户不会对由此产生的删除事件感兴趣。

【讨论】:

以上是关于什么是 debezium 解决方案来显示谁以及何时删除记录日志?的主要内容,如果未能解决你的问题,请参考以下文章

我如何知道谁在 Centos 6 上安装了哪个 CPAN 模块以及何时安装?

何时使用 Helpers 而不是 Partials

什么是 nestjs typeorm 中的 getRepositoryToken 以及何时使用它?

使用 debezium 确定交易的结束

什么是 Request.InputStream 以及何时使用它?

Containers vs Serverless:你选择谁,何时选择?