Skip to content

Instantly share code, notes, and snippets.

@vtslab
Last active February 20, 2019 22:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtslab/676419e6f205672aa935bb3dbfe2d1d8 to your computer and use it in GitHub Desktop.
Save vtslab/676419e6f205672aa935bb3dbfe2d1d8 to your computer and use it in GitHub Desktop.
Connecting to TinkerPop's gremlin server from R Studio with a java wrapper
package rgremlin;
import org.apache.tinkerpop.gremlin.driver.*;
import org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* rJava does not seem to work for classes like org.apache.tinkerpop.gremlin.driver.Cluster,
* which cannot be instantiated using the default constructor.
* Therefore this class wraps the gremlin client to allow for easy instantiating in rJava.
*/
public class RGremlin {
protected Client client;
protected int defaultResults = 20;
RGremlin(String hostname) throws Exception {
client = connect(hostname);
}
protected Client connect(String hostname) throws Exception {
GryoMapper.Builder kryo = GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance());
MessageSerializer serializer = new GryoMessageSerializerV1d0(kryo);
Cluster cluster = Cluster.build().addContactPoint(hostname).serializer(serializer).create();
return cluster.connect().init();
}
public String submit(String query) throws Exception {
return submit(query, defaultResults);
}
/**
* @param query: gremlin query string
* @param numResults: default = 20, specify 0 or -1 to get all results
* @return: json string with array of results
*/
public String submit(String query, int numResults) throws Exception {
ResultSet results = client.submit(query);
List<Result> items;
if (numResults > 0) {
items = results.some(numResults).get();
} else {
items = results.all().get();
}
List<Object> objects = new ArrayList<>();
for (Result item: items) {
objects.add(item.getObject());
}
JSONObject obj = new JSONObject();
obj.put("result", objects);
return obj.toJSONString();
}
}
# install.packages("rJava") # Ignore the "already running" message
# install.packages("rjson")
library(rJava)
library(rjson)
params <- c("-Dgremlin.log4j.level=INFO")
.jinit(parameters=params)
.jaddClassPath(dir("/some/path/lib/janusgraph-0.2.1-hadoop2/lib", full.names=TRUE))
.jclassPath()
client <- .jnew("rgremlin.RGremlin", "some.gremlin.server")
jsonStr <- .jcall(
client, "Ljava/lang/String;",
"submit", "g.V().id()")
result <- fromJSON(jsonStr)
print(result)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rgremlin</groupId>
<artifactId>rgremlin</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>rGremlin</name>
<properties>
<tinkerpop.version>3.2.9</tinkerpop.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<verbose>true</verbose>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.janusgraph</groupId>
<artifactId>janusgraph-core</artifactId>
<version>0.2.1</version>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
@vtslab
Copy link
Author

vtslab commented Aug 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment