使用过滤器更改 logstash 中的输入数据
Changing the input data in logstash using a filter
我的输入数据来自 table。 table 数据看起来像
<Customer_id> <Item_id> <Item name>
。对于客户带来的每件物品,table 中都有一个单独的行。例如,如果 c1 购买 i1,i2,i3,i4,i5 它将在 table 中有 5 行。
现在我想插入到 elasticsearch 中的数据是这样的:
{
"c1": [
{
"item_id": "i1",
"item_name": "ABC"
},
{
"item_id": "i2",
"item_name": "XYZ"
},
.....
],
"c2": [
{
"item_id": 4,
"item_name": "PQR"
}
]
}
如何修改logstash中的输入如上?
我的模式也是这样的:
项目:
item_id , item_name
购买:
cust_id、item_id
另外,您能否建议进行 SQL 查询以获得上述输出?
我采用的方法是创建一个 SQL 查询,将 Customer_ID
上的那些行分组在一起,并使用 GROUP_CONCAT
收集组中的所有项目。
然后,您可以将 logstash jdbc input 与上面提出的 SQL 查询一起使用,您应该会很好。
更新
我已将您的 SQL 查询修改为如下所示:
SELECT CONCAT('{"',cust_id,'": [',GROUP_CONCAT(CONCAT('{"item_id":',buy.item_id,','),CONCAT('"item_name": "',item.item_name,'"}')), ']}')
FROM item, buy
WHERE buy.item_id = item.item_id
GROUP BY cust_id
生成这样的行,非常接近您需要的行:
{"1": [{"item_id":1,"item_name": "abc"},{"item_id":2,"item_name": "xyz"}]}
{"2": [{"item_id":4,"item_name": "pqr"}]}
我的输入数据来自 table。 table 数据看起来像
<Customer_id> <Item_id> <Item name>
。对于客户带来的每件物品,table 中都有一个单独的行。例如,如果 c1 购买 i1,i2,i3,i4,i5 它将在 table 中有 5 行。
现在我想插入到 elasticsearch 中的数据是这样的:
{
"c1": [
{
"item_id": "i1",
"item_name": "ABC"
},
{
"item_id": "i2",
"item_name": "XYZ"
},
.....
],
"c2": [
{
"item_id": 4,
"item_name": "PQR"
}
]
}
如何修改logstash中的输入如上?
我的模式也是这样的:
项目: item_id , item_name
购买: cust_id、item_id
另外,您能否建议进行 SQL 查询以获得上述输出?
我采用的方法是创建一个 SQL 查询,将 Customer_ID
上的那些行分组在一起,并使用 GROUP_CONCAT
收集组中的所有项目。
然后,您可以将 logstash jdbc input 与上面提出的 SQL 查询一起使用,您应该会很好。
更新
我已将您的 SQL 查询修改为如下所示:
SELECT CONCAT('{"',cust_id,'": [',GROUP_CONCAT(CONCAT('{"item_id":',buy.item_id,','),CONCAT('"item_name": "',item.item_name,'"}')), ']}')
FROM item, buy
WHERE buy.item_id = item.item_id
GROUP BY cust_id
生成这样的行,非常接近您需要的行:
{"1": [{"item_id":1,"item_name": "abc"},{"item_id":2,"item_name": "xyz"}]}
{"2": [{"item_id":4,"item_name": "pqr"}]}