You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by olegz <gi...@git.apache.org> on 2016/11/03 14:57:41 UTC

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

GitHub user olegz opened a pull request:

    https://github.com/apache/nifi/pull/1179

    NIFI-969 Added support for standard JSON

    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](http://www.apache.org/legal/resolved.html#category-a)? 
    - [ ] 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

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/olegz/nifi NIFI-969

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/1179.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1179
    
----
commit 8821a46c00be7cff788a56da90bd026cd88946cb
Author: Oleg Zhurakousky <ol...@suitcase.io>
Date:   2016-11-03T13:24:33Z

    NIFI-969 Added support for standard JSON
    
    polish

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87228142
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/main/java/org/apache/nifi/processors/avro/ConvertAvroToJSON.java ---
    @@ -136,81 +167,54 @@ public void onTrigger(ProcessContext context, ProcessSession session) throws Pro
                 return;
             }
     
    -        final String containerOption = context.getProperty(CONTAINER_OPTIONS).getValue();
    -        final boolean useContainer = containerOption.equals(CONTAINER_ARRAY);
    -        // Wrap a single record (inclusive of no records) only when a container is being used
    -        final boolean wrapSingleRecord = context.getProperty(WRAP_SINGLE_RECORD).asBoolean() && useContainer;
    -
    -        final String stringSchema = context.getProperty(SCHEMA).getValue();
    -        final boolean schemaLess = stringSchema != null;
    -
             try {
                 flowFile = session.write(flowFile, new StreamCallback() {
                     @Override
                     public void process(final InputStream rawIn, final OutputStream rawOut) throws IOException {
                         final GenericData genericData = GenericData.get();
     
    -                    if (schemaLess) {
    -                        if (schema == null) {
    -                            schema = new Schema.Parser().parse(stringSchema);
    -                        }
    -                        try (final InputStream in = new BufferedInputStream(rawIn);
    -                             final OutputStream out = new BufferedOutputStream(rawOut)) {
    -                            final DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema);
    -                            final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(in, null);
    -                            final GenericRecord record = reader.read(null, decoder);
    -
    -                            // Schemaless records are singletons, so both useContainer and wrapSingleRecord
    -                            // need to be true before we wrap it with an array
    +                    try (OutputStream out = new BufferedOutputStream(rawOut); InputStream in = new BufferedInputStream(rawIn)) {
    +                        DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema);
    +                        if (schema != null) {
    +                            BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(in, null);
    +                            GenericRecord currRecord = reader.read(null, decoder);
                                 if (useContainer && wrapSingleRecord) {
                                     out.write('[');
                                 }
    -
    -                            final byte[] outputBytes = (record == null) ? EMPTY_JSON_OBJECT : genericData.toString(record).getBytes(StandardCharsets.UTF_8);
    +                            byte[] outputBytes = (currRecord == null) ? EMPTY_JSON_OBJECT
    +                                    : (useAvroJson ? toAvroJSON(schema, currRecord) : genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8));
                                 out.write(outputBytes);
    -
                                 if (useContainer && wrapSingleRecord) {
                                     out.write(']');
                                 }
    -                        }
    -                    } else {
    -                        try (final InputStream in = new BufferedInputStream(rawIn);
    -                             final OutputStream out = new BufferedOutputStream(rawOut);
    -                             final DataFileStream<GenericRecord> reader = new DataFileStream<>(in, new GenericDatumReader<GenericRecord>())) {
    -
    -                            int recordCount = 0;
    -                            GenericRecord currRecord = null;
    -                            if (reader.hasNext()) {
    -                                currRecord = reader.next();
    -                                recordCount++;
    -                            }
    -
    -                            // Open container if desired output is an array format and there are are multiple records or
    -                            // if configured to wrap single record
    -                            if (reader.hasNext() && useContainer || wrapSingleRecord) {
    -                                out.write('[');
    -                            }
    -
    -                            // Determine the initial output record, inclusive if we should have an empty set of Avro records
    -                            final byte[] outputBytes = (currRecord == null) ? EMPTY_JSON_OBJECT : genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8);
    -                            out.write(outputBytes);
    -
    -                            while (reader.hasNext()) {
    -                                if (useContainer) {
    -                                    out.write(',');
    -                                } else {
    -                                    out.write('\n');
    +                        } else {
    +                            try (DataFileStream<GenericRecord> stream = new DataFileStream<>(in, reader)) {
    +                                int recordCount = 0;
    +                                GenericRecord currRecord = null;
    +                                if (stream.hasNext()) {
    +                                    currRecord = stream.next();
    +                                    recordCount++;
    +                                }
    +                                if (stream.hasNext() && useContainer || wrapSingleRecord) {
    +                                    out.write('[');
    +                                }
    +                                byte[] outputBytes = (currRecord == null) ? EMPTY_JSON_OBJECT
    +                                        : (useAvroJson ? toAvroJSON(stream.getSchema(), currRecord) : genericData.toString(currRecord).getBytes(StandardCharsets.UTF_8));
    --- End diff --
    
    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](https://github.com/apache/avro/blob/release-1.7.7/lang/java/avro/src/main/java/org/apache/avro/io/JsonEncoder.java) to handle unions differently.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87039137
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/test/java/org/apache/nifi/processors/avro/TestConvertAvroToJSON.java ---
    @@ -239,6 +239,37 @@ public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() thro
         }
     
         @Test
    +    public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer_StandardJson() throws IOException {
    +        final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    +        runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE);
    +        runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true));
    +        Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    +        String stringSchema = schema.toString();
    +        runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema);
    +        runner.setProperty(ConvertAvroToJSON.USE_STANDARD_JSON, "true");
    +
    +        final GenericRecord user1 = new GenericData.Record(schema);
    +        user1.put("name", "Alyssa");
    +        user1.put("favorite_number", 256);
    +
    +        final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    +        final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null);
    +        final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    +        datumWriter.write(user1, encoder);
    +
    +        encoder.flush();
    +        out1.flush();
    +        byte[] test = out1.toByteArray();
    +        runner.enqueue(test);
    +
    +        runner.run();
    +
    +        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}");
    --- End diff --
    
    @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?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87230748
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/test/java/org/apache/nifi/processors/avro/TestConvertAvroToJSON.java ---
    @@ -239,6 +239,37 @@ public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() thro
         }
     
         @Test
    +    public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer_StandardJson() throws IOException {
    --- End diff --
    
    The test method name says StandardJson but the USE_AVRO_JSON property is set to true in the test


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87230815
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/test/java/org/apache/nifi/processors/avro/TestConvertAvroToJSON.java ---
    @@ -266,6 +297,34 @@ public void testMultipleAvroMessages() throws IOException {
         }
     
         @Test
    +    public void testMultipleAvroMessagesStandardJson() throws IOException {
    --- End diff --
    
    The test method name says StandardJson but the USE_AVRO_JSON property is set to true in the test


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by olegz <gi...@git.apache.org>.
Github user olegz closed the pull request at:

    https://github.com/apache/nifi/pull/1179


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87227795
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/main/java/org/apache/nifi/processors/avro/ConvertAvroToJSON.java ---
    @@ -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 {
    --- End diff --
    
    typo "shcemaToUse" should be "schemaToUse"


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/1179
  
    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 https://github.com/mattyb149/nifi/commit/879efb7572888cf4bd863e1938a3a798bd87ff0c
    
    @olegz @rdblue If this satisfies the concerns, please feel free to use it for this PR/Jira.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/1179
  
    Reviewing...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r87201121
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/test/java/org/apache/nifi/processors/avro/TestConvertAvroToJSON.java ---
    @@ -239,6 +239,37 @@ public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() thro
         }
     
         @Test
    +    public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer_StandardJson() throws IOException {
    +        final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    +        runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE);
    +        runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true));
    +        Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    +        String stringSchema = schema.toString();
    +        runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema);
    +        runner.setProperty(ConvertAvroToJSON.USE_STANDARD_JSON, "true");
    +
    +        final GenericRecord user1 = new GenericData.Record(schema);
    +        user1.put("name", "Alyssa");
    +        user1.put("favorite_number", 256);
    +
    +        final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    +        final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null);
    +        final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    +        datumWriter.write(user1, encoder);
    +
    +        encoder.flush();
    +        out1.flush();
    +        byte[] test = out1.toByteArray();
    +        runner.enqueue(test);
    +
    +        runner.run();
    +
    +        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}");
    --- End diff --
    
    Thanks @rdblue , will polish the code to reflect that


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #1179: NIFI-969 Added support for standard JSON

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the issue:

    https://github.com/apache/nifi/pull/1179
  
    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 - https://github.com/apache/nifi/pull/1436
    So, closing it and suggesting to close the underlying JIRA as well


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1179: NIFI-969 Added support for standard JSON

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1179#discussion_r86877725
  
    --- Diff: nifi-nar-bundles/nifi-avro-bundle/nifi-avro-processors/src/test/java/org/apache/nifi/processors/avro/TestConvertAvroToJSON.java ---
    @@ -239,6 +239,37 @@ public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() thro
         }
     
         @Test
    +    public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer_StandardJson() throws IOException {
    +        final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    +        runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE);
    +        runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true));
    +        Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    +        String stringSchema = schema.toString();
    +        runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema);
    +        runner.setProperty(ConvertAvroToJSON.USE_STANDARD_JSON, "true");
    +
    +        final GenericRecord user1 = new GenericData.Record(schema);
    +        user1.put("name", "Alyssa");
    +        user1.put("favorite_number", 256);
    +
    +        final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    +        final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null);
    +        final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    +        datumWriter.write(user1, encoder);
    +
    +        encoder.flush();
    +        out1.flush();
    +        byte[] test = out1.toByteArray();
    +        runner.enqueue(test);
    +
    +        runner.run();
    +
    +        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}");
    --- End diff --
    
    With standard JSON, shouldn't favorite_number be 256, not {"int":256} ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---