Guest User

Untitled

a guest
Jan 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
  2. import org.apache.tinkerpop.gremlin.driver.{Client, Cluster}
  3. import org.apache.tinkerpop.gremlin.process.remote.RemoteConnection
  4. import org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource
  5. import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.{GraphTraversal, GraphTraversalSource}
  6. import org.apache.tinkerpop.gremlin.structure.Vertex
  7.  
  8. import scala.collection.JavaConverters.asScalaBufferConverter
  9. import scala.util.control.NonFatal
  10.  
  11. object GremlinSampleRunner {
  12.  
  13. def main(args: Array[String]): Unit = {
  14. println("start")
  15.  
  16. val cluster = Cluster.build
  17. .addContactPoint("localhost")
  18. .port(8182)
  19. .create()
  20.  
  21. try {
  22. val connection: RemoteConnection = {
  23. val client: Client = cluster.connect()
  24. DriverRemoteConnection.using(client, "g")
  25. }
  26. val g: GraphTraversalSource =
  27. AnonymousTraversalSource
  28. .traversal()
  29. .withRemote(connection)
  30.  
  31. run(g)
  32. } catch {
  33. case NonFatal(e) =>
  34. println("unexpected error", e)
  35. }
  36. println("closing...", cluster)
  37. cluster.close()
  38. }
  39.  
  40. def run(g: GraphTraversalSource): Unit = {
  41. val marko: Vertex =
  42. g.addV("person")
  43. .property("name", "marko")
  44. .property("age", 29)
  45. .next
  46.  
  47. val lop: Vertex =
  48. g.addV("software")
  49. .property("name", "lop")
  50. .property("lang", "java")
  51. .property("sample-value", 345)
  52. .next
  53.  
  54. g.addE("created")
  55. .from(marko)
  56. .to(lop)
  57. .property("weight", 0.6d)
  58. .iterate
  59.  
  60. val traversal: GraphTraversal[Vertex, Int] = g
  61. .V()
  62. .has("name", "marko")
  63. .out("created")
  64. .values("sample-value")
  65.  
  66. println("putting...", traversal)
  67. val collection: Seq[Int] = traversal.toList.asScala
  68. collection.zipWithIndex.foreach {
  69. case (v, k) =>
  70. println(s"$k : $v")// 0 : 345
  71. }
  72. }
  73. }
Add Comment
Please, Sign In to add comment