使用 Logstash 将两个 mysql 表中的数据与单个 Elasticsearch 索引同步

Sync data from two mysql tables with single Elasticsearch Index using Logstash

我有两个 mysql 表,我想将它们保存到一个弹性索引中。

Table 1: Productid, name ...等

Table 2: ProductWarehouseproductId, warehouseId, qtyAvailable ...等

Product 一对多 ProductWarehouse

我的问题是,如何将每个 productProductWarehouse 条记录保存为数组?

如果ProductWarehouse被修改,如何反映在弹性指数上?

最佳做法是什么?

Elastic 中的产品索引应如下所示:

{
 "id",
 "name": ...,
 "warehouses": [
   {"warehouseId": 1, "qtyAvailable": 3},
   {"warehouseId": 10, "qtyAvailable": 30}
 ]
}

我的建议是为您需要的结构创建一个 MySQL 视图:Product joins with ProductWarehouse。此视图仅用于同步,然后使用 Logstash 从该视图同步到 ElasticSearch。

然后你可以使用 ElasticSearsh 索引作为这个结构:

{
 "id",
 "name": ...,
 "warehouses": 1
}
{
 "id",
 "name": ...,
 "warehouses": 3
}