【发布时间】:2016-12-14 01:23:31
【问题描述】:
是否可以使用 logstash 将 xml 转换为对象数组?
那将是我的示例文档:
{
"Title" : "My blog title",
"Body" : "My first post ever",
"Metadata" : "<root><Tags><TagTypeID>1</TagTypeID><TagValue>twitter</TagValue></Tags><Tags><TagTypeID>1</TagTypeID><TagValue>facebook</TagValue></Tags><Tags><TagTypeID>2</TagTypeID><TagValue>usa</TagValue></Tags><Tags><TagTypeID>3</TagTypeID><TagValue>smartphones</TagValue></Tags></root>"
}
理想情况下,我想输出这个:
{
"Title" : "My blog title",
"Body" : "My first post ever",
"Metadata" : [
{
"TagTypeID" : "1",
"TagValue" : "twitter"
},
{
"TagTypeID" : "1",
"TagValue" : "facebook"
},
{
"TagTypeID" : "2",
"TagValue" : "usa"
},
{
"TagTypeID" : "3",
"TagValue" : "smartphones"
}
]
}
但是我无法做到这一点。我尝试使用这样的 xml 过滤器:
xml
{
source => "Metadata"
target => "Parsed"
}
但是,它会输出这个
{
"Title" : "My blog title",
"Body" : "My first post ever",
"@version" : "1",
"@timestamp" : "2015-10-27T17:21:31.961Z",
"Parsed" : {
"Tags" : [
{
"TagTypeID" : ["1"],
"TagValue" : ["twitter"]
},
{
"TagTypeID" : ["1"],
"TagValue" : ["facebook"]
},
{
"TagTypeID" : ["2"],
"TagValue" : ["usa"]
},
{
"TagTypeID" : ["3"],
"TagValue" : ["smartphones"]
}
]
}
}
我不希望将我的值存储为数组(我知道那里总是只有一个值)。
我知道要从我的输入中带回哪些字段,因此我可以自己映射结构,这不需要是动态的(尽管这样会很好)。
Allow splitting of lists / arrays into multiple events 似乎很有用,但它的文档记录很差,我找不到如何在我的用例中使用此过滤器的信息。
Logstash, split event from an xml file in multiples documents keeping information from root tags 类似,但不完全是我想要实现的目标。
Logstash: XML to JSON output from array to string 这似乎很有用,但是它硬编码数组的第一个元素必须作为单个项目(不是数组的一部分)输出。它让我想起了这个:
{
"Title" : "My blog title",
"Body" : "My first post ever",
"@version" : "1",
"@timestamp" : "2015-10-27T17:21:31.961Z",
"Parsed" : {
"Tags" : [
{
"TagTypeID" : "1",
"TagValue" : "twitter"
},
{
"TagTypeID" : ["1"],
"TagValue" : ["facebook"]
},
{
"TagTypeID" : ["2"],
"TagValue" : ["usa"]
},
{
"TagTypeID" : ["3"],
"TagValue" : ["smartphones"]
}
]
}
}
- 可以在不创建自定义过滤器的情况下完成此操作吗? (我没有 Ruby 方面的经验)
- 或者我在这里缺少一些基本的东西?
【问题讨论】:
标签: arrays xml logstash logstash-configuration