You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/02/25 21:44:44 UTC

[GitHub] [nifi] pvillard31 opened a new pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

pvillard31 opened a new pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088
 
 
   Thank you for submitting a contribution to Apache NiFi.
   
   Please provide a short description of the PR here:
   
   #### Description of PR
   
   _Enables X functionality; fixes bug NIFI-YYYY._
   
   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? _Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not `squash` or use `--force` when pushing to allow for clean monitoring of changes._
   
   ### 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?
   - [ ] Have you verified that the full build is successful on both JDK 8 and JDK 11?
   - [ ] 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.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#discussion_r387732307
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java
 ##########
 @@ -214,24 +227,36 @@ protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(final String
         }
 
         final Set<String> requiredKeys = validationContext.getProperty(LOOKUP_SERVICE).asControllerService(LookupService.class).getRequiredKeys();
-        final Set<String> missingKeys = requiredKeys.stream()
-            .filter(key -> !dynamicPropNames.contains(key))
-            .collect(Collectors.toSet());
 
-        if (!missingKeys.isEmpty()) {
-            final List<ValidationResult> validationResults = new ArrayList<>();
-            for (final String missingKey : missingKeys) {
-                final ValidationResult result = new ValidationResult.Builder()
-                    .subject(missingKey)
-                    .valid(false)
-                    .explanation("The configured Lookup Services requires that a key be provided with the name '" + missingKey
-                        + "'. Please add a new property to this Processor with a name '" + missingKey
-                        + "' and provide a RecordPath that can be used to retrieve the appropriate value.")
-                    .build();
-                validationResults.add(result);
+        if(validationContext.getProperty(IN_PLACE_REPLACEMENT).asBoolean()) {
+            // it must be a single key lookup service
+            if(requiredKeys.size() != 1) {
+                return Collections.singleton(new ValidationResult.Builder()
+                        .subject(LOOKUP_SERVICE.getDisplayName())
+                        .valid(false)
+                        .explanation("The configured Lookup Services should only require one key when in-place replacement is set to true.")
 
 Review comment:
   I think this is a typo. Should read "The configured Lookup Service" (i.e., Service should not be plural).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#discussion_r387733336
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java
 ##########
 @@ -214,24 +227,36 @@ protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(final String
         }
 
         final Set<String> requiredKeys = validationContext.getProperty(LOOKUP_SERVICE).asControllerService(LookupService.class).getRequiredKeys();
-        final Set<String> missingKeys = requiredKeys.stream()
-            .filter(key -> !dynamicPropNames.contains(key))
-            .collect(Collectors.toSet());
 
-        if (!missingKeys.isEmpty()) {
-            final List<ValidationResult> validationResults = new ArrayList<>();
-            for (final String missingKey : missingKeys) {
-                final ValidationResult result = new ValidationResult.Builder()
-                    .subject(missingKey)
-                    .valid(false)
-                    .explanation("The configured Lookup Services requires that a key be provided with the name '" + missingKey
-                        + "'. Please add a new property to this Processor with a name '" + missingKey
-                        + "' and provide a RecordPath that can be used to retrieve the appropriate value.")
-                    .build();
-                validationResults.add(result);
+        if(validationContext.getProperty(IN_PLACE_REPLACEMENT).asBoolean()) {
+            // it must be a single key lookup service
+            if(requiredKeys.size() != 1) {
+                return Collections.singleton(new ValidationResult.Builder()
+                        .subject(LOOKUP_SERVICE.getDisplayName())
+                        .valid(false)
+                        .explanation("The configured Lookup Services should only require one key when in-place replacement is set to true.")
 
 Review comment:
   Would also recommend updating this explanation if updating the "In-Place Replacement" property to be a strategy. Also would even recommend being a bit more explicit in telling the user that the configured Lookup Service is not compatible with the selected strategy. As written, it sounds like the service is doing something wrong because the service "should only require one key".

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#discussion_r387737802
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java
 ##########
 @@ -263,6 +288,70 @@ public void onPropertyModified(final PropertyDescriptor descriptor, final String
     protected Set<Relationship> route(final Record record, final RecordSchema writeSchema, final FlowFile flowFile, final ProcessContext context,
         final Tuple<Map<String, RecordPath>, RecordPath> flowFileContext) {
 
+        final boolean isInPlaceReplacement = context.getProperty(IN_PLACE_REPLACEMENT).asBoolean();
+
+        if(isInPlaceReplacement) {
+            return doInPlaceReplacement(record, writeSchema, flowFile, context, flowFileContext);
+        } else {
+            return doResultPathReplacement(record, writeSchema, flowFile, context, flowFileContext);
+        }
+
+    }
+
+    private Set<Relationship> doInPlaceReplacement(Record record, RecordSchema writeSchema, FlowFile flowFile,
+            ProcessContext context, Tuple<Map<String, RecordPath>, RecordPath> flowFileContext) {
+
+        final String lookupKey = (String) context.getProperty(LOOKUP_SERVICE).asControllerService(LookupService.class).getRequiredKeys().iterator().next();
+
+        final Map<String, RecordPath> recordPaths = flowFileContext.getKey();
+        final Map<String, Object> lookupCoordinates = new HashMap<>(recordPaths.size());
+
+        for (final Map.Entry<String, RecordPath> entry : recordPaths.entrySet()) {
+            final String coordinateKey = entry.getKey();
+            final RecordPath recordPath = entry.getValue();
+
+            final RecordPathResult pathResult = recordPath.evaluate(record);
+            final List<FieldValue> lookupFieldValues = pathResult.getSelectedFields()
+                .filter(fieldVal -> fieldVal.getValue() != null)
+                .collect(Collectors.toList());
+
+            if (lookupFieldValues.isEmpty()) {
+                final Set<Relationship> rels = routeToMatchedUnmatched ? UNMATCHED_COLLECTION : SUCCESS_COLLECTION;
+                getLogger().debug("RecordPath for property '{}' did not match any fields in a record for {}; routing record to {}", new Object[] {coordinateKey, flowFile, rels});
+                return rels;
+            }
+
+            for (FieldValue fieldValue : lookupFieldValues) {
+                final Object coordinateValue = (fieldValue.getValue() instanceof Number || fieldValue.getValue() instanceof Boolean)
+                        ? fieldValue.getValue() : DataTypeUtils.toString(fieldValue.getValue(), (String) null);
+                lookupCoordinates.put(lookupKey, coordinateValue);
+
+                final Optional<?> lookupValueOption;
+                try {
+                    lookupValueOption = lookupService.lookup(lookupCoordinates, flowFile.getAttributes());
+                } catch (final Exception e) {
+                    throw new ProcessException("Failed to lookup coordinates " + lookupCoordinates + " in Lookup Service", e);
+                }
+
+                if (!lookupValueOption.isPresent()) {
+                    final Set<Relationship> rels = routeToMatchedUnmatched ? UNMATCHED_COLLECTION : SUCCESS_COLLECTION;
+                    return rels;
+                }
+
+                final Object lookupValue = lookupValueOption.get();
+
+                final DataType inferredDataType = DataTypeUtils.inferDataType(lookupValue, RecordFieldType.STRING.getDataType());
+                fieldValue.updateValue(lookupValue, inferredDataType);
+
+            }
+        }
+
+        final Set<Relationship> rels = routeToMatchedUnmatched ? MATCHED_COLLECTION : SUCCESS_COLLECTION;
+        return rels;
+    }
+
+    private Set<Relationship> doResultPathReplacement(Record record, RecordSchema writeSchema, FlowFile flowFile,
 
 Review comment:
   `writeSchema` is not used. Can remove it.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] pvillard31 edited a comment on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
pvillard31 edited a comment on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#issuecomment-594952963
 
 
   Thanks for the review @markap14 - I pushed a commit to address your remarks and added an ``additionalDetails.html`` page to document the processor with examples. Feel free to comment/suggest additional modifications. Thanks again!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] pvillard31 commented on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
pvillard31 commented on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#issuecomment-594952963
 
 
   Thanks for the review @markap14 - I pushed a commit to address your remarks and added an ``additionalDetails.html`` to document the processor with examples. Feel free to comment/suggest additional modifications. Thanks again!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on issue #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#issuecomment-595954985
 
 
   All looks good to me here @pvillard31. Thanks for adding that `additionalDetails.html`. I think with that added, and the change to the more descriptive Allowable Values, it's much easier to understand how the processor operates. Very nicely done! +1 merged to master.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#discussion_r387735460
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java
 ##########
 @@ -263,6 +288,70 @@ public void onPropertyModified(final PropertyDescriptor descriptor, final String
     protected Set<Relationship> route(final Record record, final RecordSchema writeSchema, final FlowFile flowFile, final ProcessContext context,
         final Tuple<Map<String, RecordPath>, RecordPath> flowFileContext) {
 
+        final boolean isInPlaceReplacement = context.getProperty(IN_PLACE_REPLACEMENT).asBoolean();
+
+        if(isInPlaceReplacement) {
+            return doInPlaceReplacement(record, writeSchema, flowFile, context, flowFileContext);
+        } else {
+            return doResultPathReplacement(record, writeSchema, flowFile, context, flowFileContext);
+        }
+
+    }
+
+    private Set<Relationship> doInPlaceReplacement(Record record, RecordSchema writeSchema, FlowFile flowFile,
 
 Review comment:
   `writeSchema` is not used. Should remove it.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 closed pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 closed pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [nifi] markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4088: NIFI-7197 - In-place replacement in LookupRecord processor
URL: https://github.com/apache/nifi/pull/4088#discussion_r387731844
 
 

 ##########
 File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java
 ##########
 @@ -144,6 +144,18 @@
         .required(true)
         .build();
 
+    static final PropertyDescriptor IN_PLACE_REPLACEMENT = new PropertyDescriptor.Builder()
 
 Review comment:
   I do think this change makes a lot of sense. However, I am a bit hesitant to add a boolean property value to control this. I think it's a bit unclear how exactly the behavior of the processor changes, as a "true" or "false" doesn't convey well that the entire behavior of the processor is really changed.
   
   Rather, I would recommend a "Strategy" property. Similar to how ReplaceText and work, and even this Processor already has a "Routing Strategy" property. For example, a property maybe named "Record Update Strategy" with allowable values of "Use <Result RecordPath> Property" and "Replace Existing Values". I feel this makes it more clear to the user that the behavior is significantly changed by this property. It also allows each of these Allowable Values to have a description that explains more clearly exactly how the Processor will behave, rather than having a single property whose description attempts to describe both behaviors.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services