Skip to content

Commit f58ff45

Browse files
committed
Improve graph documentation for the core engine
1 parent 5cbbd1a commit f58ff45

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

docs/graph_fluent.rst

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ hard to maintain. This fluent API allows you to build Gremlin traversals and wri
2727
queries directly in Python. These native traversal queries can be executed explicitly, with
2828
a `Session` object, or implicitly::
2929

30-
g = DseGraph.traversal_source(session=dse_session)
30+
from cassandra.cluster import Cluster, EXEC_PROFILE_GRAPH_DEFAULT
31+
from cassandra.datastax.graph import GraphProtocol
32+
from cassandra.datastax.graph.fluent import DseGraph
33+
34+
# Create an execution profile, using GraphSON3 for Core graphs
35+
ep_graphson3 = DseGraph.create_execution_profile(
36+
'my_core_graph_name',
37+
graph_protocol=GraphProtocol.GRAPHSON_3_0)
38+
cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep_graphson3})
39+
session = cluster.connect()
40+
41+
# Execute a fluent graph query
42+
g = DseGraph.traversal_source(session=session)
3143
g.addV('genre').property('genreId', 1).property('name', 'Action').next()
3244

3345
# implicit execution caused by iterating over results
@@ -50,15 +62,24 @@ Configuring a Traversal Execution Profile
5062
The fluent api takes advantage of *configuration profiles* to allow
5163
different execution configurations for the various query handlers. Graph traversal
5264
execution requires a custom execution profile to enable Gremlin-bytecode as
53-
query language. Here is how to accomplish this configuration:
65+
query language. With Core graphs, it is important to use GraphSON3. Here is how
66+
to accomplish this configuration:
5467

5568
.. code-block:: python
5669
5770
from cassandra.cluster import Cluster, EXEC_PROFILE_GRAPH_DEFAULT
71+
from cassandra.datastax.graph import GraphProtocol
5872
from cassandra.datastax.graph.fluent import DseGraph
5973
60-
ep = DseGraph.create_execution_profile('graph_name')
61-
cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep})
74+
# Using GraphSON3 as graph protocol is a requirement with Core graphs.
75+
ep = DseGraph.create_execution_profile(
76+
'graph_name',
77+
graph_protocol=GraphProtocol.GRAPHSON_3_0)
78+
79+
# For Classic graphs, GraphSON1, GraphSON2 and GraphSON3 (DSE 6.8+) are supported.
80+
ep_classic = DseGraph.create_execution_profile('classic_graph_name') # default is GraphSON2
81+
82+
cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep, 'classic': ep_classic})
6283
session = cluster.connect()
6384
6485
g = DseGraph.traversal_source(session) # Build the GraphTraversalSource
@@ -71,27 +92,6 @@ If you want to change execution property defaults, please see the :doc:`Executio
7192
for a more generalized discussion of the API. Graph traversal queries use the same execution profile defined for DSE graph. If you
7293
need to change the default properties, please refer to the :doc:`DSE Graph query documentation page <graph>`
7394

74-
Configuring a Traversal Execution Profile for the Core graph engine
75-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76-
77-
To execute a traversal query with graphs that use the core engine, you need to configure
78-
a graphson3 execution profile:
79-
80-
.. code-block:: python
81-
from cassandra.cluster import Cluster, EXEC_PROFILE_GRAPH_DEFAULT
82-
from cassandra.datastax.graph import GraphProtocol
83-
from cassandra.datastax.graph.fluent import DseGraph
84-
85-
ep_graphson3 = DseGraph.create_execution_profile(
86-
'my_core_graph_name',
87-
graph_protocol=GraphProtocol.GRAPHSON_3_0
88-
)
89-
cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep_graphson3})
90-
91-
g = DseGraph.traversal_source(session)
92-
print g.V().toList()
93-
94-
9595
Explicit Graph Traversal Execution with a DSE Session
9696
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9797

@@ -101,19 +101,28 @@ Below is an example of explicit execution. For this example, assume the schema h
101101

102102
.. code-block:: python
103103
104+
from cassandra.cluster import Cluster, EXEC_PROFILE_GRAPH_DEFAULT
105+
from cassandra.datastax.graph import GraphProtocol
104106
from cassandra.datastax.graph.fluent import DseGraph
105107
from pprint import pprint
106108
107-
# create a tinkerpop graphson2 ExecutionProfile
108-
ep = DseGraph.create_execution_profile('graph_name')
109+
ep = DseGraph.create_execution_profile(
110+
'graph_name',
111+
graph_protocol=GraphProtocol.GRAPHSON_3_0)
109112
cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep})
110113
session = cluster.connect()
111114
112115
g = DseGraph.traversal_source(session=session)
116+
117+
Convert a traversal to a bytecode query for classic graphs::
118+
113119
addV_query = DseGraph.query_from_traversal(
114-
g.addV('genre').property('genreId', 1).property('name', 'Action')
120+
g.addV('genre').property('genreId', 1).property('name', 'Action'),
121+
graph_protocol=GraphProtocol.GRAPHSON_3_0
115122
)
116-
v_query = DseGraph.query_from_traversal(g.V())
123+
v_query = DseGraph.query_from_traversal(
124+
g.V(),
125+
graph_protocol=GraphProtocol.GRAPHSON_3_0)
117126

118127
for result in session.execute_graph(addV_query):
119128
pprint(result.value)
@@ -124,7 +133,6 @@ Converting a traversal to a bytecode query for core graphs require some more wor
124133
need the cluster context for UDT and tuple types:
125134

126135
.. code-block:: python
127-
g = DseGraph.traversal_source(session=session)
128136
context = {
129137
'cluster': cluster,
130138
'graph_name': 'the_graph_for_the_query'
@@ -135,6 +143,9 @@ need the cluster context for UDT and tuple types:
135143
context=context
136144
)
137145
146+
for result in session.execute_graph(addV_query):
147+
pprint(result.value)
148+
138149
Implicit Graph Traversal Execution with TinkerPop
139150
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140151

@@ -185,19 +196,18 @@ python `Future <https://docs.python.org/3/library/concurrent.futures.html#concur
185196
# do other stuff...
186197
187198
Specify the Execution Profile explicitly
188-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189200

190201
If you don't want to change the default graph execution profile (`EXEC_PROFILE_GRAPH_DEFAULT`), you can register a new
191202
one as usual and use it explicitly. Here is an example:
192203

193-
194204
.. code-block:: python
195205
196206
from cassandra.cluster import Cluster
197207
from cassandra.datastax.graph.fluent import DseGraph
198208
199209
cluster = Cluster()
200-
ep = DseGraph.create_execution_profile('graph_name')
210+
ep = DseGraph.create_execution_profile('graph_name', graph_protocol=GraphProtocol.GRAPHSON_3_0)
201211
cluster.add_execution_profile('graph_traversal', ep)
202212
session = cluster.connect()
203213
@@ -213,8 +223,8 @@ the same execution profile (for different graphs):
213223
g_movies = DseGraph.traversal_source(session, graph_name='movies', ep)
214224
g_series = DseGraph.traversal_source(session, graph_name='series', ep)
215225
216-
print g_movies.V().toList() # Traverse the movies Graph
217-
print g_series.V().toList() # Traverse the series Graph
226+
print(g_movies.V().toList()) # Traverse the movies Graph
227+
print(g_series.V().toList()) # Traverse the series Graph
218228
219229
Batch Queries
220230
~~~~~~~~~~~~~
@@ -231,8 +241,10 @@ the execution profile accordingly. Here is a example::
231241
from cassandra.cluster import Cluster
232242
from cassandra.datastax.graph.fluent import DseGraph
233243

234-
ep = DseGraph.create_execution_profile('graph_name')
235-
cluster = Cluster(execution_profiles={'graphson2': ep})
244+
ep = DseGraph.create_execution_profile(
245+
'graph_name',
246+
graph_protocol=GraphProtocol.GRAPHSON_3_0)
247+
cluster = Cluster(execution_profiles={'graphson3': ep})
236248
session = cluster.connect()
237249

238250
g = DseGraph.traversal_source()
@@ -247,13 +259,13 @@ the batch to a GraphStatement::
247259
batch.add(
248260
g.addV('genre').property('genreId', 2).property('name', 'Drama')) # Don't use `.next()` with a batch
249261

250-
graph_statement = batch.as_graph_statement()
262+
graph_statement = batch.as_graph_statement(graph_protocol=GraphProtocol.GRAPHSON_3_0)
251263
graph_statement.is_idempotent = True # configure any Statement parameters if needed...
252-
session.execute_graph(graph_statement, execution_profile='graphson2')
264+
session.execute_graph(graph_statement, execution_profile='graphson3')
253265

254266
To execute the batch using :meth:`TraversalBatch.execute <.datastax.graph.fluent.query.TraversalBatch.execute>`, you need to bound the batch to a DSE session::
255267

256-
batch = DseGraph.batch(session, 'graphson2') # bound the session and execution profile
268+
batch = DseGraph.batch(session, 'graphson3') # bound the session and execution profile
257269

258270
batch.add(
259271
g.addV('genre').property('genreId', 1).property('name', 'Action'))

0 commit comments

Comments
 (0)