SavtechSolutions commented on a change in pull request #3268: NIFI-5947 
Elasticsearch lookup service compatible with LookupAttribute
URL: https://github.com/apache/nifi/pull/3268#discussion_r247727219
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-client-service/src/main/java/org/apache/nifi/elasticsearch/ElasticSearchStringLookupService.java
 ##########
 @@ -0,0 +1,96 @@
+/*
+ * 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.elasticsearch;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.lookup.LookupFailureException;
+import org.apache.nifi.lookup.StringLookupService;
+import org.apache.nifi.processor.util.StandardValidators;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+public class ElasticSearchStringLookupService extends 
AbstractControllerService implements StringLookupService {
+    public static final PropertyDescriptor CLIENT_SERVICE = new 
PropertyDescriptor.Builder()
+            .name("el-rest-client-service")
+            .displayName("Client Service")
+            .description("An ElasticSearch client service to use for running 
queries.")
+            .identifiesControllerService(ElasticSearchClientService.class)
+            .required(true)
+            .build();
+    public static final PropertyDescriptor INDEX = new 
PropertyDescriptor.Builder()
+            .name("el-lookup-index")
+            .displayName("Index")
+            .description("The name of the index to read from")
+            .required(true)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor TYPE = new 
PropertyDescriptor.Builder()
+            .name("el-lookup-type")
+            .displayName("Type")
+            .description("The type of this document (used by Elasticsearch for 
indexing and searching)")
+            .required(false)
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .build();
+    private static final List<PropertyDescriptor> DESCRIPTORS = 
Arrays.asList(CLIENT_SERVICE, INDEX, TYPE);
+    private static final ObjectMapper mapper = new ObjectMapper();
+    private static final String ID = "id";
+    private ElasticSearchClientService esClient;
+    private String index;
+    private String type;
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return DESCRIPTORS;
+    }
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        esClient = 
context.getProperty(CLIENT_SERVICE).asControllerService(ElasticSearchClientService.class);
+        index = 
context.getProperty(INDEX).evaluateAttributeExpressions().getValue();
+        type = 
context.getProperty(TYPE).evaluateAttributeExpressions().getValue();
+    }
+
+    @Override
+    public Optional<String> lookup(Map<String, Object> coordinates) throws 
LookupFailureException {
+        try {
+            final String id = (String) coordinates.get(ID);
+            final Map<String, Object> enums = esClient.get(index, type, id);
 
 Review comment:
   Currently, only index lookups are offered. Index and type are configured as 
service properties, and document id is passed as a property of LookupAttribute 
processor. I'd pass all three from the processor if I could, but 
LookupAttribute has a limitation of only one dynamic property being allowed. 
Alternatively, all three can be passed as one string 
(e.g."[index]/[type]/[id]", same as the ES document URL), and then the string 
could be split for the index lookup - I entertained that idea for a bit, but 
decided this approach to be too "magical", albeit more flexible. What are your 
thoughts on this?

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


With regards,
Apache Git Services

Reply via email to