Skip to content

Instantly share code, notes, and snippets.

@yukim
Last active April 27, 2018 06:14
Show Gist options
  • Save yukim/d32c9de8d0974ca20a7b52c2aa5581bf to your computer and use it in GitHub Desktop.
Save yukim/d32c9de8d0974ca20a7b52c2aa5581bf to your computer and use it in GitHub Desktop.
Tinkerpop driver/server behavior
package com.datastax.demo;
import com.datastax.driver.dse.DseCluster;
import com.datastax.driver.dse.DseSession;
import com.datastax.driver.dse.graph.GraphNode;
import com.datastax.driver.dse.graph.GraphOptions;
import com.datastax.driver.dse.graph.GraphStatement;
import com.datastax.dse.graph.api.DseGraph;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
/**
* Using DSE driver you get consistent result.
*/
public class DseMain {
public static void main(String[] args) {
GraphOptions graphOptions = new GraphOptions().setGraphName("test");
DseCluster cluster = DseCluster.builder().addContactPoint("localhost").withGraphOptions(graphOptions).build();
DseSession session = cluster.connect();
GraphTraversalSource g = DseGraph.traversal(session);
try {
GraphTraversal t = g.V().has("v", "name", "foo").values("name");
while(t.hasNext()) {
System.out.println("1> " + t.next());
}
GraphStatement stmt = DseGraph.statementFromTraversal(t);
for (GraphNode gn : session.executeGraph(stmt)) {
System.out.println("2> " + gn.asString());
}
for (GraphNode gn : session.executeGraph("g.V().has('v', 'name', 'foo').values('name')")) {
System.out.println("3> " + gn.asString());
}
} finally {
cluster.close();
}
}
}
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.File;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.util.concurrent.ExecutionException;
public class Main {
public static void main(String[] args) {
Cluster cluster;
try {
cluster = Cluster.build(new File("remote-objects.yaml")).create();
} catch (FileNotFoundException e) {
throw new IOError(e);
}
Client client = cluster.connect();
Client aliased = client.alias("test.g");
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "test.g"));
try {
GraphTraversal t = g.V().has("v", "name", "foo").values("name");
while(t.hasNext()) {
System.out.println("1> " + t.next());
}
aliased.submit(t).all()
.thenAccept(rs -> rs.forEach(r -> System.out.println("2> " + r.getString()))).get();
aliased.submit("g.V().has('v', 'name', 'foo').values('name')").all()
.thenAccept(rs -> rs.forEach(r -> System.out.println("3> " + r.getString()))).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
cluster.close();
}
}
}
system.graph("test").ifNotExists().create()
:remote config alias g test.g
schema.propertyKey("id").Int().create()
schema.propertyKey("name").Text().create()
schema.vertexLabel("v").partitionKey("id").properties("name").create()
schema.vertexLabel("v").index("byName").materialized().by("name").add()
g.addV("v").property("id", 1).property("name", "foo").iterate()
g.addV("v").property("id", 2).property("name", "foo").iterate()
1> foo
1> foo
2> foo
2> foo
3> foo
3> foo
1> foo
1> foo
2> foo
3> foo
3> foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment