mattyb149 commented on a change in pull request #4204: NIFI-7355 Added Gremlin 
bytecode client service.
URL: https://github.com/apache/nifi/pull/4204#discussion_r409678391
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-graph-bundle/nifi-other-graph-services/src/main/java/org/apache/nifi/graph/GremlinBytecodeClientService.java
 ##########
 @@ -0,0 +1,168 @@
+/*
+ * 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.graph;
+
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnDisabled;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.Validator;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.util.StringUtils;
+import org.apache.tinkerpop.gremlin.driver.Cluster;
+import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
+import org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource;
+import 
org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+
+import javax.script.Bindings;
+import javax.script.Compilable;
+import javax.script.CompiledScript;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+@CapabilityDescription("A client service that provides a scriptable interface 
to open a remote connection/travseral " +
+        "against a Gremlin Server and execute operations against it.")
+@Tags({ "graph", "database", "gremlin", "tinkerpop" })
+public class GremlinBytecodeClientService extends 
AbstractTinkerpopClientService implements GraphClientService {
+    private static final List<PropertyDescriptor> NEW_DESCRIPTORS;
+
+    public static final PropertyDescriptor TRAVERSAL_SOURCE_NAME = new 
PropertyDescriptor.Builder()
+            .name("gremlin-traversal-source-name")
+            .displayName("Traversal Source Name")
+            .description("An optional property that lets you set the name of 
the remote traversal instance. " +
+                    "This can be really important when working with databases 
like JanusGraph that support " +
+                    "multiple backend traversal configurations 
simultaneously.")
+            
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
+            .addValidator(Validator.VALID)
+            .build();
+
+    static {
+        List<PropertyDescriptor> _temp = new ArrayList<>();
+        _temp.addAll(DESCRIPTORS);
+        _temp.add(TRAVERSAL_SOURCE_NAME);
+        NEW_DESCRIPTORS = Collections.unmodifiableList(_temp);
+    }
+
+
+    @Override
+    public List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return NEW_DESCRIPTORS;
+    }
+
+    private ScriptEngineManager MANAGER = new ScriptEngineManager();
+    private ScriptEngine engine;
+    private Map<String, CompiledScript> compiledCode;
+    private Cluster cluster;
+    private String traversalSourceName;
+
+    /**
+     * @param context
+     *            the configuration context
+     * @throws InitializationException
+     *             if unable to create a database connection
+     */
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) throws 
InitializationException {
+        cluster = buildCluster(context);
+
+        compiledCode = new ConcurrentHashMap<>();
+        engine = MANAGER.getEngineByName("groovy");
+
+        if (context.getProperty(TRAVERSAL_SOURCE_NAME).isSet()) {
+            traversalSourceName = 
context.getProperty(TRAVERSAL_SOURCE_NAME).evaluateAttributeExpressions()
+                    .getValue();
+        }
+    }
+
+    @OnDisabled
+    public void shutdown() {
+        try {
+            compiledCode = null;
+            engine = null;
+            cluster.close();
+        } catch (Exception e) {
+            throw new ProcessException(e);
+        }
+    }
+
+    @Override
+    public Map<String, String> executeQuery(String s, Map<String, Object> map, 
GraphQueryResultCallback graphQueryResultCallback) {
+        String hash = DigestUtils.md5Hex(s);
+        CompiledScript compiled;
+        GraphTraversalSource traversal;
+
+        try {
+            if (StringUtils.isEmpty(traversalSourceName)) {
+                traversal = 
AnonymousTraversalSource.traversal().withRemote(DriverRemoteConnection.using(cluster));
+            } else {
+                traversal = 
AnonymousTraversalSource.traversal().withRemote(DriverRemoteConnection.using(cluster,
 traversalSourceName));
+            }
+        } catch (Exception e) {
+            throw new ProcessException(e);
+        }
+
+        if (compiledCode.containsKey(hash)) {
+            compiled = compiledCode.get(hash);
+        } else {
+            try {
+                compiled = ((Compilable)engine).compile(s);
+                compiledCode.put(s, compiled);
+            } catch (ScriptException e) {
+                throw new ProcessException(e);
+            }
+        }
+
+        Bindings bindings = engine.createBindings();
+        bindings.putAll(map);
+        bindings.put("g", traversal);
 
 Review comment:
   Should probably mention this variable in the documentation somewhere if its 
intent is to be available to user scripts.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to