Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
Description
JSON attributes are treated like normal strings and are escaped by the sink. For example, if the body or a header contains the following value:
{"foo":"bar"}
It will be added like this in Elasticsearch:
{"@message": "{\"foo\":\"bar\"}}"
We end up with a plain string instead of a valid JSON field.
I think I found how to fix this bug. The source of the problem is caused by the way a "complex field" is added. The ES XContent classes are used to parse the data in the detected format, but then, instead of adding the parsed data, the string() method is called and it converts it back to a string that is the same as the initial data! Here is the current code with added comments:
XContentBuilder tmp = jsonBuilder(); // This tmp builder is completely useless. parser = XContentFactory.xContent(contentType).createParser(data); parser.nextToken(); tmp.copyCurrentStructure(parser); // This copies the whole parsed data in this tmp builder. // Here, by calling tmp.string(), we get the parsed data converted back to a string. // This means that tmp.string() == String(data)! // All this parsing for nothing... // And then, as the field(String, String) method is called on the builder, and the builder being a jsonBuilder, // the string will be escaped according to the JSON specifications. builder.field(fieldName, tmp.string());
If we really want to take advantage of the XContent classes, we have to add the parsed data to the builder. To do this, it is as simply as:
parser = XContentFactory.xContent(contentType).createParser(data); parser.nextToken(); // Add the field name, but not the value. builder.field(fieldName); // This will add the whole parsed content as the value of the field. builder.copyCurrentStructure(parser);
I tried this and it works as expected.
Attachments
Attachments
Issue Links
- relates to
-
FLUME-2126 Problem in elasticsearch sink when the event body is a complex field
- Resolved
- links to