Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-969 Added support for standard JSON #1179

Closed
wants to merge 3 commits into from
Closed

Conversation

olegz
Copy link
Contributor

@olegz olegz commented Nov 3, 2016

Thank you for submitting a contribution to Apache NiFi.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically master)?

  • Is your initial contribution a single, squashed commit?

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.

polish

@mattyb149
Copy link
Contributor

Reviewing...


runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0);
out.assertContentEquals("{\"name\":\"Alyssa\",\"favorite_number\":{\"int\":256},\"favorite_color\":null}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With standard JSON, shouldn't favorite_number be 256, not {"int":256} ?

Copy link
Contributor Author

@olegz olegz Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattyb149 Not sure, but that is what is outputted by JSONEncoder and as you can see it's different then the result of toString(). Perhaps it needs to be flipped where toString() is in fact standard JSON and JSONEncoder provides AVRO JSON. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it looks backwards as-is, @rdblue what do you think? It seems like toString() is giving standard JSON and the JSONEncoder is giving AVRO-JSON...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONEncoder produces Avro JSON according to the Avro spec, and the toString produces something like it, but not quite either Avro JSON or just normal JSON you would expect. I would not rely on the toString representation because it isn't well defined and may change in future releases. I'd say the best option is to build a new conversion for normal JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rdblue , will polish the code to reflect that

@@ -225,4 +229,14 @@ public void process(final InputStream rawIn, final OutputStream rawOut) throws I
flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
session.transfer(flowFile, REL_SUCCESS);
}

private byte[] toAvroJSON(Schema shcemaToUse, GenericRecord datum) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo "shcemaToUse" should be "schemaToUse"

out.write('[');
}
byte[] outputBytes = (currRecord == null) ? EMPTY_JSON_OBJECT
: (useAvroJson ? toAvroJSON(stream.getSchema(), currRecord) : genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like toString() is still being used for encoding here, Ryan recommended a different encoder for normal JSON. Not sure what that entails though, @rdblue can you elaborate on the approach? At first glance it looks like you might need to subclass JsonEncoder to handle unions differently.

@@ -239,6 +239,37 @@ public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() thro
}

@Test
public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer_StandardJson() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name says StandardJson but the USE_AVRO_JSON property is set to true in the test

@@ -266,6 +297,34 @@ public void testMultipleAvroMessages() throws IOException {
}

@Test
public void testMultipleAvroMessagesStandardJson() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name says StandardJson but the USE_AVRO_JSON property is set to true in the test

@mattyb149
Copy link
Contributor

I took the liberty of writing a StandardJsonEncoder that mimics the output of GenericData.toString(datum), the commit to add that to the PR is mattyb149@879efb7

@olegz @rdblue If this satisfies the concerns, please feel free to use it for this PR/Jira.

@olegz
Copy link
Contributor Author

olegz commented Feb 14, 2017

The issue described in the underlying JIRA and this PR is essentially addressed or to be addressed/maintained by a new Schema Registry based transformer effort - #1436
So, closing it and suggesting to close the underlying JIRA as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants