使用 flatMap 透视 Kafka KTable 结果

Eva*_*kas 5 apache-kafka apache-kafka-streams

我有两个 JSON 文档如下:

[
  {
    "ProductID": 12,
    "ProductName": "Product 1",
    "CountryID": 55,
    "CountryName": "Country 1",
    "Companies": [{
        "CompanyID": 1,
        "CompanyName": "Company 1"
      }, {
        "CompanyID": 2,
        "CompanyName": "Company 2"
      }
    ]
  },
  {
    "ProductID": 13,
    "ProductName": "Product 2",
    "CountryID": 55,
    "CountryName": "Country 1",
    "Companies": [{
        "CompanyID": 1,
        "CompanyName": "Company 1"
      }, {
        "CompanyID": 2,
        "CompanyName": "Company 2"
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

这应该是一个KTable,而不是一个KStream,因为一些记录会被删除。

关键是(ProductID, CountryID)

我想对这些数据进行透视,并以这种方式将数组中的(CompanyID)每个(ProductID, CountryID)组合作为键:

[
  {
    "CompanyID": 1,
    "CompanyName": "Company 1",
    "ProductsCountries": [{
        "ProductID": 12,
        "CountryID": 55
      }, {
        "ProductID": 13,
        "CountryID": 55
      }
    ]
  },
  {
    "CompanyID": 2,
    "CompanyName": "Company 2",
    "ProductsCountries": [{
        "ProductID": 12,
        "CountryID": 55
      }, {
        "ProductID": 13,
        "CountryID": 55
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

每当一个(ProductID, CountryID)组合从我的 KTable 中消失时,我想将它从(CompanyID)拥有它的数组中删除。

使用 KStream 这似乎是半可能的,因为我可以只对其进行 flatMap 并将新(ProductID, CountryID)组合添加到我的文档中,但是我将无法捕获删除。

有没有办法用KTable做到这一点?