Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. # start GremlinServer
  2. # bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.2.2-SNAPSHOT
  3. # bin/gremlin-server.sh conf/gremlin-server-modern-py.yaml
  4.  
  5. from gremlin_python.process.graph_traversal import GraphTraversal
  6. from gremlin_python.process.graph_traversal import GraphTraversalSource
  7. from gremlin_python.process.graph_traversal import __
  8. from gremlin_python.process.traversal import Operator
  9.  
  10. from gremlin_python.structure.io.graphson import GraphSONReader
  11. from gremlin_python.structure.io.graphson import serializers
  12. from gremlin_python.process.traversal import Bytecode
  13. from gremlin_python.process.traversal import Bindings
  14. from gremlin_python.process.traversal import P
  15.  
  16. from gremlin_python.structure.io.graphson import GraphSONWriter
  17.  
  18. # in practice, you really only need the 3 imports below
  19.  
  20. from gremlin_python import statics
  21. from gremlin_python.structure.graph import Graph
  22. from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
  23.  
  24.  
  25. # this allows us to do g.V().repeat(out()) instead of g.V().repeat(__.out())-type traversals
  26.  
  27. statics.load_statics(globals())
  28.  
  29. # create a remote connection using RemoteStrategy
  30.  
  31. graph = Graph()
  32. g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182','g'))
  33.  
  34. # the __repr__ (string) of traversal is its Python encoded bytecode (lowest level of compilation for that host)
  35.  
  36. g.V().repeat(out()).times(2).name
  37.  
  38. # you can see the GraphSON 2.0 bytecode for a traversal
  39.  
  40. GraphSONWriter.writeObject(g.V().repeat(out()).times(2).name)
  41.  
  42. # toList()/toSet()/next()/etc. are terminal/action methods that trigger evaluation
  43.  
  44. g.V().repeat(both()).times(2).name.toList()
  45. g.V().repeat(both()).times(2).name.toSet()
  46. g.V().repeat(out()).times(2).name.next()
  47. g.V().repeat(out()).times(2).name.nextTraverser()
  48.  
  49. # bindings ensure that cached traversal compilations can be reused
  50.  
  51. g.V().out(("a","knows"),"created").name
  52. g.V().out(("a","knows"),"created").name.bytecode
  53. g.V().out(("a","knows"),"created").name.bytecode.bindings
  54. g.V().out(("a","knows"),"created").name.toList()
  55.  
  56. # lambdas are solved via a lamda that returns a string lambda
  57.  
  58. g.V().out().map(lambda: "lambda x: (x.get().value('name'),len(x.get().value('name')))")
  59. g.V().out().map(lambda: "lambda x: x.get()").toList()
  60. g.V().out().map(lambda: "x: (x.get().value('name'),len(x.get().value('name')))").toList()
  61.  
  62. # all the source modulators work
  63.  
  64. g.withComputer().V().out('created').valueMap()
  65. g.withComputer().V().out('created').valueMap().toList()
  66. g.withSack(0).V().repeat(outE().sack(sum,'weight').inV()).times(2).project('a','b').by('name').by(sack()).toList()
  67.  
  68. # side-effects work too
  69.  
  70. g.V().repeat(groupCount('m').by('name').both()).times(10).cap('m').next()
  71. t = g.V().repeat(groupCount('m').by('name').both()).times(10).iterate()
  72. t.side_effects
  73. t.side_effects.keys()
  74. t.side_effects['m']
  75.  
  76. t = g.V().aggregate("m").iterate()
  77. t.side_effects
  78. t.side_effects.keys()
  79. t.side_effects['m']
  80.  
  81. t = g.withSideEffect('m',0).V().hasLabel('person').sideEffect(lambda : "x : x.sideEffects('m',x.get().value('age'))").groupCount('n').by('name')
  82. t.toList()
  83. t.side_effects
  84. t.side_effects.keys()
  85. t.side_effects['m']
  86. t.side_effects['n']
  87.  
  88. # with ssl authentification (be sure to add authentification to the gremlin-server-modern-py.yaml)
  89.  
  90. from gremlin_python import statics
  91. from gremlin_python.structure.graph import Graph
  92. from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
  93.  
  94. graph = Graph()
  95. g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182','g', username='stephen', password='password'))
  96. g.V().toList()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement