Skip to content

Commit 0bcb978

Browse files
Merge pull request #61 from nvitucci/add-ga-docs
Add examples for Graph Analytics Cypher API and Python client
2 parents a444e55 + 68b5dcb commit 0bcb978

File tree

1 file changed

+156
-2
lines changed

1 file changed

+156
-2
lines changed

documentation/graph-analytics.adoc

Lines changed: 156 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,157 @@
1-
== Setup Instructions
1+
== Introduction
22

3-
See https://neo4j.com/docs/aura/graph-analytics/[documentation]
3+
link:https://neo4j.com/docs/aura/graph-analytics[Graph Analytics^] allows you to run graph algorithms on your data, for example to detect communities or shortest paths between nodes.
4+
5+
== Requirements
6+
7+
You must create link:https://neo4j.com/docs/aura/api/authentication/[Aura API credentials] to use Aura Graph Analytics.
8+
9+
== Graph Analytics with Cypher
10+
11+
Add data to your AuraDB instance using the following Cypher query.
12+
13+
[source, cypher, copy=true]
14+
----
15+
CREATE
16+
(a:User {name: 'Alice'}),
17+
(b:User {name: 'Bob'}),
18+
(c:User {name: 'Charlie'}),
19+
(d:User {name: 'Dan'}),
20+
(e:User {name: 'Eve'}),
21+
(f:User {name: 'Frank'}),
22+
(a)-[:LINK]->(b),
23+
(a)-[:LINK]->(c),
24+
(c)-[:LINK]->(e),
25+
(e)-[:LINK]->(d),
26+
(e)-[:LINK]->(f)
27+
----
28+
29+
Then, create a remote in-memory graph within an Aura Graph Analytics session using a _remote projection_.
30+
31+
[source, cypher, copy=true]
32+
----
33+
CYPHER runtime=parallel
34+
WITH gds.aura.api.credentials("<Aura API Client ID>", "<Aura API Client Secret>") AS credentials
35+
MATCH (n)
36+
OPTIONAL MATCH (n)-->(m)
37+
RETURN gds.graph.project('myGraph', n, m, {}, {memory: '4GB'})
38+
----
39+
40+
Once the graph is created, you can run all supported graph algorithms.
41+
For example, run PageRank to find the central nodes in the graph and see their score.
42+
43+
[source, cypher, copy=true]
44+
----
45+
WITH gds.aura.api.credentials("<Aura API Client ID>", "<Aura API Client Secret>") AS credentials
46+
CALL gds.pageRank.stream('myGraph')
47+
YIELD nodeId, score
48+
RETURN gds.util.asNode(nodeId).name, score
49+
----
50+
51+
Finally, drop the graph to delete the session when you no longer need it.
52+
53+
[source, cypher, copy=true]
54+
----
55+
WITH gds.aura.api.credentials("<Aura API Client ID>", "<Aura API Client Secret>") AS credentials
56+
CALL gds.graph.drop('myGraph')
57+
YIELD graphName
58+
RETURN graphName
59+
----
60+
61+
For a more detailed example including the use of query parameters for the Aura API credentials, updates to the in-memory graph, and write-back to AuraDB, see the link:https://neo4j.com/docs/graph-data-science/current/aura-graph-analytics/quickstart/[Aura Graph Analytics quickstart].
62+
63+
For more information about the Graph Analytics Cypher API, see the link:https://neo4j.com/docs/graph-data-science/current/aura-graph-analytics/cypher[Graph Data Science documentation].
64+
65+
== Graph Analytics with the Python client
66+
67+
Compared to the Cypher API, the Python client adds the option to run algorithms on data from non-Neo4j data sources such as Pandas DataFrames.
68+
69+
To use the Graph Analytics features in Neo4j Aura with Python, you need to install the `graphdatascience` package.
70+
71+
[source, bash, copy=true]
72+
----
73+
pip install graphdatascience
74+
----
75+
76+
The entry point for managing Aura Graph Analytics sessions is the `GdsSessions` class.
77+
78+
[source, python, copy=true]
79+
----
80+
from graphdatascience.session import GdsSessions, AuraAPICredentials
81+
82+
CLIENT_ID = "<Aura API Client ID>"
83+
CLIENT_SECRET = "<Aura API Client Secret>"
84+
PROJECT_ID = None
85+
# Create a new GdsSessions object
86+
sessions = GdsSessions(api_credentials=AuraAPICredentials(CLIENT_ID, CLIENT_SECRET, PROJECT_ID))
87+
----
88+
89+
Use the `sessions` object to create an Aura Graph Analytics session.
90+
91+
In the following example, the session is attached to an AuraDB instance.
92+
Here the username and password are the credentials of your AuraDB instance, _not_ your Aura API credentials.
93+
94+
[source, python, copy=true]
95+
----
96+
from graphdatascience.session import DbmsConnectionInfo, SessionMemory
97+
98+
db_connection = DbmsConnectionInfo(
99+
uri="<AuraDB instance URI>",
100+
username="<AuraDB username>",
101+
password="<AuraDB password>"
102+
)
103+
104+
my_session = sessions.get_or_create(
105+
session_name="my-session",
106+
memory=SessionMemory.m_4GB,
107+
db_connection=db_connection,
108+
)
109+
----
110+
111+
With the session ready, add some data to the AuraDB instance with the `run_cypher` method.
112+
113+
[source, python, copy=true]
114+
----
115+
my_session.run_cypher("""
116+
CREATE
117+
(a:User {name: 'Alice'}),
118+
(b:User {name: 'Bob'}),
119+
(c:User {name: 'Charlie'}),
120+
(d:User {name: 'Dan'}),
121+
(e:User {name: 'Eve'}),
122+
(f:User {name: 'Frank'}),
123+
(a)-[:LINK]->(b),
124+
(a)-[:LINK]->(c),
125+
(c)-[:LINK]->(e),
126+
(e)-[:LINK]->(d),
127+
(e)-[:LINK]->(f)
128+
""")
129+
----
130+
131+
Then, create the remote graph `G` and run PageRank on it.
132+
133+
[source, python, copy=true]
134+
----
135+
G, result = my_session.graph.project(
136+
graph_name="my-graph",
137+
query="""
138+
CALL {
139+
MATCH (n)
140+
OPTIONAL MATCH (n)-->(m)
141+
RETURN n, m
142+
}
143+
RETURN gds.graph.project.remote(n, m, {})
144+
""",
145+
)
146+
147+
my_session.pageRank.stream(G)
148+
----
149+
150+
Finally, delete the session when you no longer need it.
151+
152+
[source, python, copy=true]
153+
----
154+
my_session.delete()
155+
----
156+
157+
For more details, see the Python client link:https://neo4j.com/docs/graph-data-science-client/current/graph-analytics-serverless/[docs] and the link:https://neo4j.com/docs/graph-data-science-client/current/tutorials/graph-analytics-serverless/[tutorial for AuraDB].

0 commit comments

Comments
 (0)