Created
January 21, 2019 15:45
-
-
Save x7c1/c721bfb0a34618a87bdeb477f8d578ea to your computer and use it in GitHub Desktop.
connect to running gremlin server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection | |
| import org.apache.tinkerpop.gremlin.driver.{Client, Cluster} | |
| import org.apache.tinkerpop.gremlin.process.remote.RemoteConnection | |
| import org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource | |
| import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.{GraphTraversal, GraphTraversalSource} | |
| import org.apache.tinkerpop.gremlin.structure.Vertex | |
| import scala.collection.JavaConverters.asScalaBufferConverter | |
| import scala.util.control.NonFatal | |
| object GremlinSampleRunner { | |
| def main(args: Array[String]): Unit = { | |
| println("start") | |
| val cluster = Cluster.build | |
| .addContactPoint("localhost") | |
| .port(8182) | |
| .create() | |
| try { | |
| val connection: RemoteConnection = { | |
| val client: Client = cluster.connect() | |
| DriverRemoteConnection.using(client, "g") | |
| } | |
| val g: GraphTraversalSource = | |
| AnonymousTraversalSource | |
| .traversal() | |
| .withRemote(connection) | |
| run(g) | |
| } catch { | |
| case NonFatal(e) => | |
| println("unexpected error", e) | |
| } | |
| println("closing...", cluster) | |
| cluster.close() | |
| } | |
| def run(g: GraphTraversalSource): Unit = { | |
| val marko: Vertex = | |
| g.addV("person") | |
| .property("name", "marko") | |
| .property("age", 29) | |
| .next | |
| val lop: Vertex = | |
| g.addV("software") | |
| .property("name", "lop") | |
| .property("lang", "java") | |
| .property("sample-value", 345) | |
| .next | |
| g.addE("created") | |
| .from(marko) | |
| .to(lop) | |
| .property("weight", 0.6d) | |
| .iterate | |
| val traversal: GraphTraversal[Vertex, Int] = g | |
| .V() | |
| .has("name", "marko") | |
| .out("created") | |
| .values("sample-value") | |
| println("putting...", traversal) | |
| val collection: Seq[Int] = traversal.toList.asScala | |
| collection.zipWithIndex.foreach { | |
| case (v, k) => | |
| println(s"$k : $v")// 0 : 345 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment