Skip to content

Instantly share code, notes, and snippets.

@yukim
Created May 26, 2018 04:08
Show Gist options
  • Save yukim/6e914f92e3dea4b6ad954e278b273167 to your computer and use it in GitHub Desktop.
Save yukim/6e914f92e3dea4b6ad954e278b273167 to your computer and use it in GitHub Desktop.
JJUG CCC Spring 2018 demo code
package demo;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.apache.tinkerpop.gremlin.process.traversal.P.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Order.decr;
import static org.apache.tinkerpop.gremlin.process.traversal.Scope.local;
import static org.apache.tinkerpop.gremlin.structure.Column.*;
public class Main {
public static void main(String[] args) {
// Tinkerpopサーバーへの接続
Cluster cluster;
try {
cluster = Cluster.build(new File("remote-objects.yaml")).create();
} catch (FileNotFoundException e) {
throw new IOError(e);
}
// 方法1: Gremlinクエリを文字列で投げる
Client client = cluster.connect();
String gremlinQuery = "g.V().hasLabel('customer').sample(5).valueMap('customerId', 'name')";
try {
client.submit(gremlinQuery).all()
.thenAccept(rs -> rs.forEach(r -> {
Map<String, Object> data = (Map<String, Object>) r.getObject();
System.out.println("Customer ID: " + data.get("customerId") + ", Name: " + data.get("name"));
})).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
client.close();
}
// 方法2: トラバーサルを直接実行
Console cons;
String customerId;
if ((cons = System.console()) != null) {
do {
customerId = cons.readLine( "Customer ID? ");
} while (customerId == null || customerId.isEmpty());
System.out.println("Recommendation for " + customerId);
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
GraphTraversal t = g.V().has("customer", "customerId", customerId).as("customer").
out("ordered").out("contains").out("is").aggregate("products").
in("is").in("contains").in("ordered").where(neq("customer")).
out("ordered").out("contains").out("is").where(without("products")).
groupCount().order(local).by(values, decr).
select(keys).limit(local, 5).unfold().values("name");
while (t.hasNext()) {
System.out.println("> " + t.next());
}
try {
g.close();
} catch (Exception e) {
// ignore
}
}
cluster.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</groupId>
<artifactId>tinkerpop-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<tinkerpop.version>3.3.3</tinkerpop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment