Add jq expression support in flattenSpec#4171
Conversation
|
@gianm I appreciate if you'd review. I'm using this in my production system. |
|
@knoguchi sounds like useful feature however use of https://github.com/eiiches/jackson-jq is questionable, I'm not really sure about all the legalities but mentioned repository doesn't have a LICENSE file or any apache etc headers on any of the source code. |
|
@himanshug Thanks for the comment. jackson-jq itself is Apache2.0 per https://github.com/eiiches/jackson-jq/blob/develop/COPYING#L10. jq is MIT per manual. jq uses a library copyrighted by Lucent Technology -- the wording appears to be FOSS compatible but I'm not a lawyer to say for sure. |
| <dependency> | ||
| <groupId>net.thisptr</groupId> | ||
| <artifactId>jackson-jq</artifactId> | ||
| <version>RELEASE</version> |
There was a problem hiding this comment.
Please fix this to a specific version, and also, please put the version in the dependencyManagement section of the main pom.
| <dependency> | ||
| <groupId>net.thisptr</groupId> | ||
| <artifactId>jackson-jq</artifactId> | ||
| <version>0.0.7</version> |
There was a problem hiding this comment.
The specific version should go in the dependencyManagement section of the main pom (it helps keep versions in sync across modules)
| path = new FlattenExpr(JsonQuery.compile(fieldSpec.getExpr())); | ||
| } | ||
| catch (JsonQueryException e) { | ||
| throw new ParseException(e, "Unable to compile JQ expression [%s]", fieldSpec.getExpr()); |
There was a problem hiding this comment.
It would be better for this to be an IllegalArgumentException. ParseExceptions are meant to represent parse errors on individual lines, but this error applies to the entire flatten spec.
| fields.add(JSONPathFieldSpec.createRootField("ts")); | ||
|
|
||
| fields.add(JSONPathFieldSpec.createRootField("d1")); | ||
| //fields.add(JSONPathFieldSpec.createRootField("d2")); |
There was a problem hiding this comment.
Please don't include commented out code.
| fields.add(JSONPathFieldSpec.createJqField("ae1[2].e1.d2", ".ae1[2].e1.d2")); | ||
|
|
||
| fields.add(JSONPathFieldSpec.createRootField("m3")); | ||
| //fields.add(JSONPathFieldSpec.createRootField("m4")); |
There was a problem hiding this comment.
Please don't include commented out code.
|
Based on the following evidence the licensing seems ok to me.
|
|
@b-slim can you please check |
|
@himanshug this looks good to me, thanks ! |
|
@b-slim thanks for checking that. took another look today, LGTM . 👍 after existing comments are addressed. |
|
Thanks all for the reviews and comments. I will fix them shortly. |
|
I've addressed the comments. Not sure why Travis failed for the last commit 79c7baa |
This PR adds a new expression type "jq" to the flattenSpec. "jq" is a well known JSON manipulation tool. JSONPath is ok for extracting values out of the nested data structure but it lacks value translation capability. jq is pretty good at it and it can be used as ETL tool, effectively.
Example: a JSON has
secondFieldandnanosecFieldfields where integer values are stored, and you want to extract the sum of the two fields, and get the derived fieldtotalSecond. Another example is to apply regex on URL and get directory name. The flattenSpec can be written like this.The benchmark that accesses nested fields shows no significant difference than JSONPath.
JsonPath related code has been modified to use
JacksonJsonNodeJsonProvider andJsonNodeinstead ofJacksonJsonProviderandMap<String, Object>so that both JsonPath and Jackson-JQ use the same JsonNode document.The only ugly part is the newly added
FlattenExpradapter that wraps JsonPath and JsonQuery classes. I'm not sure the best way to create a common interface for the 3rd party classes. Please enlighten me if there is a better way.Lastly, please note, the jq library used in this PR is jackson-jq that is a Java port, not the original jq nor libjq wrapper.