markap14 commented on a change in pull request #5597:
URL: https://github.com/apache/nifi/pull/5597#discussion_r778352372



##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/MessageKeyResolvers.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.kafka.pubsub;
+
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.record.path.RecordPath;
+import org.apache.nifi.record.path.RecordPathResult;
+import org.apache.nifi.record.path.util.RecordPathCache;
+import org.apache.nifi.serialization.record.Record;
+
+import java.util.Optional;
+
+/**
+ * Collection of implementation of message key resolvers.
+ *
+ * Message key resolvers specify how to obtain a Kafka message key for a given 
message.
+ */
+final public class MessageKeyResolvers {
+
+    private MessageKeyResolvers() {
+    }
+
+    /**
+     * {@link MessageKeyResolver} that provides the previously existing 
functionality of treating the value of
+     * the message key field as the name of a property in the (flat) value 
record.
+     * The value of this property is extracted and returned as a String.
+     * <P/>
+     * <B>Note:</B> Nested properties are not supported!
+     */
+    public static MessageKeyResolver MESSAGE_VALUE_PROPERTY_RESOLVER = 
(flowFile, record, messageKeyField) ->

Review comment:
       Not sure that the name `MESSAGE_VALUE_PROPERTY_RESOLVER` is really clear 
as to what this is doing. Perhaps just call it `STRING_FIELD_RESOLVER`?

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/MessageKeyResolvers.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.kafka.pubsub;
+
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.record.path.RecordPath;
+import org.apache.nifi.record.path.RecordPathResult;
+import org.apache.nifi.record.path.util.RecordPathCache;
+import org.apache.nifi.serialization.record.Record;
+
+import java.util.Optional;
+
+/**
+ * Collection of implementation of message key resolvers.
+ *
+ * Message key resolvers specify how to obtain a Kafka message key for a given 
message.
+ */
+final public class MessageKeyResolvers {
+
+    private MessageKeyResolvers() {
+    }
+
+    /**
+     * {@link MessageKeyResolver} that provides the previously existing 
functionality of treating the value of
+     * the message key field as the name of a property in the (flat) value 
record.
+     * The value of this property is extracted and returned as a String.
+     * <P/>
+     * <B>Note:</B> Nested properties are not supported!
+     */
+    public static MessageKeyResolver MESSAGE_VALUE_PROPERTY_RESOLVER = 
(flowFile, record, messageKeyField) ->
+            record.getAsString(messageKeyField.getValue());
+
+    /**
+     * {@link MessageKeyResolver} that treats the value of the message key 
field as a {@link RecordPath} into
+     * the value record. The value of this property is extracted and returned 
as a String.
+     * <P/>
+     * <pre>
+     * Example:
+     *  message key field config value = '/address/zip'
+     *  value record =
+     *    {
+     *        "id": "10",
+     *        "address": {
+     *            "zip": "8804"
+     *        }
+     *    }
+     *  resulting message key = "8804"
+     *  </pre>
+     */
+    public static MessageKeyResolver 
RECORD_PATH_RESOLVER_FACTORY(RecordPathCache recordPathCache) {

Review comment:
       This is a method so should not be in upper case. Recommend just calling 
it `createRecordPathResolver`?

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/test/java/org/apache/nifi/processors/kafka/pubsub/TestPublishKafkaRecord_2_6.java
##########
@@ -364,6 +369,98 @@ public Object answer(final InvocationOnMock 
invocationOnMock) throws Throwable {
         assertEquals(partitionsForAge48.get(0), partitionsForAge48.get(1));
     }
 
+    @Test
+    public void testMessagePropertyKeyResolversWithAttribute() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", myAttributeValue);
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher(myAttributeValue)), any(String.class), 
nullable(Function.class), 
eq(MessageKeyResolvers.MESSAGE_VALUE_PROPERTY_RESOLVER));
+    }
+
+    @Test
+    public void testSimpleValueKeyResolversWithAttribute() throws IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.SIMPLE_VALUE_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();

Review comment:
       Should probably be `final Map<String, String>`

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/MessageKeyResolvers.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.kafka.pubsub;
+
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.record.path.RecordPath;
+import org.apache.nifi.record.path.RecordPathResult;
+import org.apache.nifi.record.path.util.RecordPathCache;
+import org.apache.nifi.serialization.record.Record;
+
+import java.util.Optional;
+
+/**
+ * Collection of implementation of message key resolvers.
+ *
+ * Message key resolvers specify how to obtain a Kafka message key for a given 
message.
+ */
+final public class MessageKeyResolvers {
+
+    private MessageKeyResolvers() {
+    }
+
+    /**
+     * {@link MessageKeyResolver} that provides the previously existing 
functionality of treating the value of
+     * the message key field as the name of a property in the (flat) value 
record.
+     * The value of this property is extracted and returned as a String.
+     * <P/>
+     * <B>Note:</B> Nested properties are not supported!
+     */
+    public static MessageKeyResolver MESSAGE_VALUE_PROPERTY_RESOLVER = 
(flowFile, record, messageKeyField) ->
+            record.getAsString(messageKeyField.getValue());
+
+    /**
+     * {@link MessageKeyResolver} that treats the value of the message key 
field as a {@link RecordPath} into
+     * the value record. The value of this property is extracted and returned 
as a String.
+     * <P/>
+     * <pre>
+     * Example:
+     *  message key field config value = '/address/zip'
+     *  value record =
+     *    {
+     *        "id": "10",
+     *        "address": {
+     *            "zip": "8804"
+     *        }
+     *    }
+     *  resulting message key = "8804"
+     *  </pre>
+     */
+    public static MessageKeyResolver 
RECORD_PATH_RESOLVER_FACTORY(RecordPathCache recordPathCache) {
+        return (FlowFile flowFile, Record record, PropertyValue 
messageKeyField) -> {
+            final RecordPathResult result = 
recordPathCache.getCompiled(messageKeyField.getValue()).evaluate(record);
+            return result.getSelectedFields().findFirst().flatMap( v -> 
Optional.ofNullable(v.getValue()))
+                    .map(Object::toString).orElse(null);
+        };
+    }
+
+    /**
+     * {@link MessageKeyResolver} that treats the value of the message key 
field as message key to use. It could refer to
+     * existing flow file attributes, e.g. {@code ${kafka.key}} if processing 
and publishing a record that has been
+     * consumed from Kafka.
+     */
+    public static MessageKeyResolver SIMPLE_VALUE_RESOLVER = (flowFile, 
record, messageKeyField) -> messageKeyField.getValue();

Review comment:
       Would also recommend calling this `PropertyValueResolver` as that's what 
it's really doing, is simply evaluating the property value.

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/PublishKafkaRecord_2_6.java
##########
@@ -124,6 +125,14 @@
         "Interprets the <Partition> property as Expression Language that will 
be evaluated against each FlowFile. This Expression will be evaluated once 
against the FlowFile, " +
             "so all Records in a given FlowFile will go to the same 
partition.");
 
+    static final AllowableValue MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION = new 
AllowableValue("MessageValuePropertyResolver",

Review comment:
       Rather than a naming convention of MessageValuePropertyResolver, 
RecordPathResolver, etc. remember: this is a user-facing configuration option. 
So would stick with more natural language: "Use Field in Record", "Use 
RecordPath", "Use Literal Value" (or "Use Expression Language")

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/test/java/org/apache/nifi/processors/kafka/pubsub/TestPublishKafkaRecord_2_6.java
##########
@@ -364,6 +369,98 @@ public Object answer(final InvocationOnMock 
invocationOnMock) throws Throwable {
         assertEquals(partitionsForAge48.get(0), partitionsForAge48.get(1));
     }
 
+    @Test
+    public void testMessagePropertyKeyResolversWithAttribute() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", myAttributeValue);
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher(myAttributeValue)), any(String.class), 
nullable(Function.class), 
eq(MessageKeyResolvers.MESSAGE_VALUE_PROPERTY_RESOLVER));
+    }
+
+    @Test
+    public void testSimpleValueKeyResolversWithAttribute() throws IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.SIMPLE_VALUE_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", myAttributeValue);
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher(myAttributeValue)), any(String.class), 
nullable(Function.class), eq(MessageKeyResolvers.SIMPLE_VALUE_RESOLVER));
+    }
+
+    @Test
+    public void testSimpleValueKeyResolversWithExpressionLanguage() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key:toUpper()}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.SIMPLE_VALUE_KEY_RESOLVER_SELECTION);
+
+        final HashMap<String, String> attributes = new HashMap<>();

Review comment:
       Should probably be `final Map<String, String>`

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/test/java/org/apache/nifi/processors/kafka/pubsub/TestPublishKafkaRecord_2_6.java
##########
@@ -364,6 +369,98 @@ public Object answer(final InvocationOnMock 
invocationOnMock) throws Throwable {
         assertEquals(partitionsForAge48.get(0), partitionsForAge48.get(1));
     }
 
+    @Test
+    public void testMessagePropertyKeyResolversWithAttribute() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", myAttributeValue);
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher(myAttributeValue)), any(String.class), 
nullable(Function.class), 
eq(MessageKeyResolvers.MESSAGE_VALUE_PROPERTY_RESOLVER));
+    }
+
+    @Test
+    public void testSimpleValueKeyResolversWithAttribute() throws IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.SIMPLE_VALUE_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", myAttributeValue);
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher(myAttributeValue)), any(String.class), 
nullable(Function.class), eq(MessageKeyResolvers.SIMPLE_VALUE_RESOLVER));
+    }
+
+    @Test
+    public void testSimpleValueKeyResolversWithExpressionLanguage() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key:toUpper()}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.SIMPLE_VALUE_KEY_RESOLVER_SELECTION);
+
+        final HashMap<String, String> attributes = new HashMap<>();
+        attributes.put("my.key", "abc");
+
+        doRunWithAttributes(attributes);
+
+        verify(mockLease, times(1)).publish(any(FlowFile.class), 
any(RecordSet.class), any(RecordSetWriterFactory.class),
+            nullable(RecordSchema.class), argThat(new 
PropertyValueMatcher("ABC")), any(String.class), nullable(Function.class), 
eq(MessageKeyResolvers.SIMPLE_VALUE_RESOLVER));
+    }
+
+    @Test
+    public void testRecordPathResolverWithExpresionLanguage() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"/${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.RECORD_PATH_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();

Review comment:
       Should probably be `final Map<String, String>`

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/test/java/org/apache/nifi/processors/kafka/pubsub/TestPublishKafkaRecord_2_6.java
##########
@@ -364,6 +369,98 @@ public Object answer(final InvocationOnMock 
invocationOnMock) throws Throwable {
         assertEquals(partitionsForAge48.get(0), partitionsForAge48.get(1));
     }
 
+    @Test
+    public void testMessagePropertyKeyResolversWithAttribute() throws 
IOException {
+        runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_FIELD, 
"${my.key}");
+        
runner.setProperty(PublishKafkaRecord_2_6.MESSAGE_KEY_RESOLVER_STRATEGY, 
PublishKafkaRecord_2_6.MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION);
+
+        final String myAttributeValue = "abc";
+        final HashMap<String, String> attributes = new HashMap<>();

Review comment:
       Should probably be `final Map<String, String>`

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/PublisherLease.java
##########
@@ -221,6 +222,19 @@ void publish(final FlowFile flowFile, final RecordSet 
recordSet, final RecordSet
         }
     }
 
+    private String resolveMessageKeyIdNeeded(MessageKeyResolver keyResolver, 
FlowFile flowFile, Record record, PropertyValue messageKeyField) {
+        if(keyResolver != null) {
+            final String key = keyResolver.apply(flowFile, record, 
messageKeyField);
+            if(key != null) {
+                logger.debug("Message key resolved: key={}, 
messageKeyField={}", key, messageKeyField);
+                return key;
+            } else {
+                logger.warn("Message key resolver configured, but no key 
resolved! messageKeyField={}, record={}", messageKeyField, record);

Review comment:
       I think I would avoid making this a WARN message and just make it DEBUG. 
I can see a use case where a null value is intended as a key. In this case, 
logs would be spammed with non-stop WARN logs and the processor may always show 
bulletins. 

##########
File path: 
nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-2-6-processors/src/main/java/org/apache/nifi/processors/kafka/pubsub/PublishKafkaRecord_2_6.java
##########
@@ -124,6 +125,14 @@
         "Interprets the <Partition> property as Expression Language that will 
be evaluated against each FlowFile. This Expression will be evaluated once 
against the FlowFile, " +
             "so all Records in a given FlowFile will go to the same 
partition.");
 
+    static final AllowableValue MESSAGE_PROPERTY_KEY_RESOLVER_SELECTION = new 
AllowableValue("MessageValuePropertyResolver",
+            "MessageValuePropertyResolver", "The <Message Key Field> property 
will be interpreted as the name of a simple, " +
+            "non-nested property of the message value. This reflects the 
previously existing behaviour.");
+    static final AllowableValue RECORD_PATH_KEY_RESOLVER_SELECTION = new 
AllowableValue("RecordPathResolver",

Review comment:
       It's probably worth noting here that if the RecordPath matches more than 
one element, the first one will be chosen. And that the value will be simply 
converted to a String and is expected to be a primitive value, not an embedded 
record. I can definitely envision someone expecting to use this with a 
RecordPath that points to a complex JSON object or a complex Avro record, for 
instance.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to