-
Notifications
You must be signed in to change notification settings - Fork 2.8k
NIFI-14337 - Enhance JoltTransformJSON to Support JOLT Transformation… #9785
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9ef7ed
NIFI-14337 Enhance JoltTransformJSON to Support JOLT Transformations …
Srilatha-ramreddy 566fc4f
NIFI-14337 Fix Checkstyle Warning
Srilatha-ramreddy f3fa31e
NIFI-14337 Use Arguments.argumentSet to define the testcase names
Srilatha-ramreddy 3b91741
NIFI-14337 - Remove JSON Attribute expression language support and so…
Srilatha-ramreddy 84bf6c1
NIFI-14337 - Fix the log messages
Srilatha-ramreddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ | |||||||||
import org.apache.nifi.annotation.documentation.Tags; | ||||||||||
import org.apache.nifi.annotation.lifecycle.OnScheduled; | ||||||||||
import org.apache.nifi.components.PropertyDescriptor; | ||||||||||
import org.apache.nifi.expression.ExpressionLanguageScope; | ||||||||||
import org.apache.nifi.flowfile.FlowFile; | ||||||||||
import org.apache.nifi.flowfile.attributes.CoreAttributes; | ||||||||||
import org.apache.nifi.jolt.util.TransformUtils; | ||||||||||
|
@@ -41,6 +42,7 @@ | |||||||||
import org.apache.nifi.processor.exception.ProcessException; | ||||||||||
import org.apache.nifi.processor.util.StandardValidators; | ||||||||||
import org.apache.nifi.util.StopWatch; | ||||||||||
import org.apache.nifi.util.StringUtils; | ||||||||||
import org.apache.nifi.util.file.classloader.ClassLoaderUtils; | ||||||||||
|
||||||||||
import java.io.InputStream; | ||||||||||
|
@@ -55,11 +57,28 @@ | |||||||||
@Tags({"json", "jolt", "transform", "shiftr", "chainr", "defaultr", "removr", "cardinality", "sort"}) | ||||||||||
@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) | ||||||||||
@WritesAttribute(attribute = "mime.type", description = "Always set to application/json") | ||||||||||
@CapabilityDescription("Applies a list of Jolt specifications to the flowfile JSON payload. A new FlowFile is created " | ||||||||||
+ "with transformed content and is routed to the 'success' relationship. If the JSON transform " | ||||||||||
+ "fails, the original FlowFile is routed to the 'failure' relationship.") | ||||||||||
@CapabilityDescription("Applies a list of Jolt specifications to either the FlowFile JSON content or a specified FlowFile JSON attribute. " | ||||||||||
+ "If the JSON transform fails, the original FlowFile is routed to the 'failure' relationship.") | ||||||||||
@RequiresInstanceClassLoading | ||||||||||
public class JoltTransformJSON extends AbstractJoltTransform { | ||||||||||
|
||||||||||
public static final PropertyDescriptor JSON_SOURCE = new PropertyDescriptor.Builder() | ||||||||||
.name("JSON Source") | ||||||||||
.description("Specifies whether the Jolt transformation is applied to FlowFile JSON content or to specified FlowFile JSON attribute.") | ||||||||||
.required(true) | ||||||||||
.allowableValues(SourceStrategy.class) | ||||||||||
.defaultValue(SourceStrategy.FLOW_FILE) | ||||||||||
.build(); | ||||||||||
|
||||||||||
public static final PropertyDescriptor JSON_SOURCE_ATTRIBUTE = new PropertyDescriptor.Builder() | ||||||||||
.name("JSON Source Attribute") | ||||||||||
.description("The FlowFile attribute containing JSON to be transformed.") | ||||||||||
.dependsOn(JSON_SOURCE, SourceStrategy.ATTRIBUTE) | ||||||||||
.required(true) | ||||||||||
.expressionLanguageSupported(ExpressionLanguageScope.NONE) | ||||||||||
.addValidator(StandardValidators.ATTRIBUTE_KEY_VALIDATOR) | ||||||||||
.build(); | ||||||||||
|
||||||||||
public static final PropertyDescriptor PRETTY_PRINT = new PropertyDescriptor.Builder() | ||||||||||
.name("Pretty Print") | ||||||||||
.displayName("Pretty Print") | ||||||||||
|
@@ -80,16 +99,18 @@ public class JoltTransformJSON extends AbstractJoltTransform { | |||||||||
|
||||||||||
public static final Relationship REL_SUCCESS = new Relationship.Builder() | ||||||||||
.name("success") | ||||||||||
.description("The FlowFile with transformed content will be routed to this relationship") | ||||||||||
.description("The FlowFile with successfully transformed content or updated attribute will be routed to this relationship") | ||||||||||
.build(); | ||||||||||
public static final Relationship REL_FAILURE = new Relationship.Builder() | ||||||||||
.name("failure") | ||||||||||
.description("If a FlowFile fails processing for any reason (for example, the FlowFile is not valid JSON), it will be routed to this relationship") | ||||||||||
.description("If the JSON transformation fails (e.g., due to invalid JSON in the content or attribute), the original FlowFile is routed to this relationship.") | ||||||||||
.build(); | ||||||||||
|
||||||||||
private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = Stream.concat( | ||||||||||
getCommonPropertyDescriptors().stream(), | ||||||||||
Stream.of( | ||||||||||
JSON_SOURCE, | ||||||||||
JSON_SOURCE_ATTRIBUTE, | ||||||||||
PRETTY_PRINT, | ||||||||||
MAX_STRING_LENGTH | ||||||||||
) | ||||||||||
|
@@ -122,14 +143,34 @@ public void onTrigger(final ProcessContext context, ProcessSession session) thro | |||||||||
|
||||||||||
final ComponentLog logger = getLogger(); | ||||||||||
final StopWatch stopWatch = new StopWatch(true); | ||||||||||
|
||||||||||
final Object inputJson; | ||||||||||
try (final InputStream in = session.read(original)) { | ||||||||||
inputJson = jsonUtil.jsonToObject(in); | ||||||||||
} catch (final Exception e) { | ||||||||||
logger.error("JSON parsing failed for {}", original, e); | ||||||||||
session.transfer(original, REL_FAILURE); | ||||||||||
return; | ||||||||||
final boolean sourceStrategyFlowFile = SourceStrategy.FLOW_FILE == context.getProperty(JSON_SOURCE).asAllowableValue(SourceStrategy.class); | ||||||||||
String jsonSourceAttributeName = null; | ||||||||||
|
||||||||||
if (sourceStrategyFlowFile) { | ||||||||||
try (final InputStream in = session.read(original)) { | ||||||||||
inputJson = jsonUtil.jsonToObject(in); | ||||||||||
} catch (final Exception e) { | ||||||||||
logger.error("JSON parsing failed on FlowFile content for {}", original, e); | ||||||||||
session.transfer(original, REL_FAILURE); | ||||||||||
return; | ||||||||||
} | ||||||||||
} else { | ||||||||||
jsonSourceAttributeName = context.getProperty(JSON_SOURCE_ATTRIBUTE).getValue(); | ||||||||||
final String jsonSourceAttributeValue = original.getAttribute(jsonSourceAttributeName); | ||||||||||
if (StringUtils.isBlank(jsonSourceAttributeValue)) { | ||||||||||
logger.error("FlowFile attribute [{}] value is blank", jsonSourceAttributeName); | ||||||||||
session.transfer(original, REL_FAILURE); | ||||||||||
return; | ||||||||||
} else { | ||||||||||
try { | ||||||||||
inputJson = jsonUtil.jsonToObject(jsonSourceAttributeValue); | ||||||||||
} catch (final Exception e) { | ||||||||||
logger.error("JSON parsing failed on FlowFile attribute for {}", original, e); | ||||||||||
session.transfer(original, REL_FAILURE); | ||||||||||
return; | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
final String jsonString; | ||||||||||
|
@@ -152,13 +193,18 @@ public void onTrigger(final ProcessContext context, ProcessSession session) thro | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
FlowFile transformed = session.write(original, out -> out.write(jsonString.getBytes(StandardCharsets.UTF_8))); | ||||||||||
|
||||||||||
final String transformType = context.getProperty(JOLT_TRANSFORM).getValue(); | ||||||||||
transformed = session.putAttribute(transformed, CoreAttributes.MIME_TYPE.key(), "application/json"); | ||||||||||
session.transfer(transformed, REL_SUCCESS); | ||||||||||
session.getProvenanceReporter().modifyContent(transformed, "Modified With " + transformType, stopWatch.getElapsed(TimeUnit.MILLISECONDS)); | ||||||||||
logger.info("Transform completed for {}", original); | ||||||||||
if (sourceStrategyFlowFile) { | ||||||||||
FlowFile transformed = session.write(original, out -> out.write(jsonString.getBytes(StandardCharsets.UTF_8))); | ||||||||||
final String transformType = context.getProperty(JOLT_TRANSFORM).getValue(); | ||||||||||
transformed = session.putAttribute(transformed, CoreAttributes.MIME_TYPE.key(), "application/json"); | ||||||||||
session.transfer(transformed, REL_SUCCESS); | ||||||||||
session.getProvenanceReporter().modifyContent(transformed, "Modified With " + transformType, stopWatch.getElapsed(TimeUnit.MILLISECONDS)); | ||||||||||
logger.info("Transform completed on FlowFile content for {}", original); | ||||||||||
} else { | ||||||||||
session.putAttribute(original, jsonSourceAttributeName, jsonString); | ||||||||||
session.transfer(original, REL_SUCCESS); | ||||||||||
logger.info("Transform completed on FlowFile attribute for {}", original); | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, it may be beneficial to name the attribute which was transformed so it is clear in the logs
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dan-s1 Updated as per feedback. |
||||||||||
} | ||||||||||
|
||||||||||
@OnScheduled | ||||||||||
|
46 changes: 46 additions & 0 deletions
46
...le/nifi-jolt-processors/src/main/java/org/apache/nifi/processors/jolt/SourceStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.nifi.processors.jolt; | ||
|
||
import org.apache.nifi.components.DescribedValue; | ||
|
||
public enum SourceStrategy implements DescribedValue { | ||
FLOW_FILE("JOLT transformation applied to FlowFile content."), | ||
ATTRIBUTE("JOLT transformation applied to FlowFile attribute."); | ||
|
||
private final String description; | ||
|
||
SourceStrategy(final String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
return name(); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return name(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a good idea to include the name of the attribute like you did above.