如何将多维SQL json字符串转换为有效的JSON对象,以便可以将其映射到Elasticsearch索引模板字段类型。
示例:
我们假设这是我的Elasticsearch索引模板。
{
"settings": {
"number_of_shards": "5"
},
"mappings": {
"_doc": {
"properties": {
"manager": {
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "text"
}
}
},
"employees": {
"type": "nested",
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "text"
},
"addresses": {
"type": "nested",
"properties": {
"street": {
"type": "text"
},
"city": {
"type": "text"
}
}
}
}
}
}
}
}
}
有问题的情况是“员工”数组,它是内部“地址”数组JSON转换。
“员工”的SQL数据示例:
[{"age":30,"name":"User 1","addresses":"[{\"street\":"Home street 1",\"city\":"New York"},{\"street\":"Home street 2",\"city\":"Washington"}]"}]
Logstash过滤器配置:如果“雇员”不包含嵌套数组,则转换有效。但是问题出在我的数组有嵌套数组时。
filter {
ruby {
code => "
require 'json'
employees_json_value = JSON.parse(event.get('employees').to_s)
event.set('employees',employees_json_value )
"
}
}
问题是如何将嵌套“地址”数组的“员工”列转换为JSON对象,以便可以将其映射到Elasticsearch索引模板。