Skip to content

Commit 4366f0e

Browse files
committed
feat: example cypher querries in jupyter notebook
1 parent 326a6a1 commit 4366f0e

1 file changed

Lines changed: 99 additions & 7 deletions

File tree

src/neo4j-quickstart/quickstart.ipynb

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
},
1313
{
1414
"cell_type": "markdown",
15-
"id": "14bf5bb2",
15+
"id": "21ad21b2",
1616
"metadata": {},
1717
"source": [
18-
"## Downloading data from Neo4j \n",
18+
"## Connecting to Neo4J \n",
1919
"\n",
20-
"Here is an example of using the Neo4JDownloader to retrieve all nodes from the graph. \n",
21-
"Adapt the Neo4JDownloader class as you see fit for retrieving information. "
20+
"Here are some settings that allow to connect to Neo4J. Do not edit unless you know what you are doing. "
2221
]
2322
},
2423
{
2524
"cell_type": "code",
2625
"execution_count": null,
27-
"id": "ea28c628",
26+
"id": "4545c948",
2827
"metadata": {},
2928
"outputs": [],
3029
"source": [
@@ -53,7 +52,7 @@
5352
{
5453
"cell_type": "code",
5554
"execution_count": null,
56-
"id": "1a349b96",
55+
"id": "d799a464",
5756
"metadata": {},
5857
"outputs": [],
5958
"source": [
@@ -71,8 +70,101 @@
7170
"\n",
7271
" return Neo4JDownloader(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD, NEO4J_DATABASE)\n",
7372
"\n",
74-
"def extract_data(nodes, relationships):\n",
73+
"def connect_neo4j(): \n",
7574
" downloader = get_downloader()\n",
75+
" return downloader"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"id": "d3707369",
81+
"metadata": {},
82+
"source": [
83+
"## Neo4J Querrying: Examples of CYPHER Querries\n",
84+
"\n",
85+
"Here is an example of using the Neo4J Downloader to run some custom querries on the graph."
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "02f8b4a2",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"downloader = connect_neo4j()\n",
96+
"\n",
97+
"# QUERY: Get all nodes that have the EPFL string in their name (whether they are user, repository or org)\n",
98+
"\n",
99+
"epfl_query =\"\"\"\n",
100+
"MATCH (n)\n",
101+
"WHERE toLower(n.name) CONTAINS 'epfl'\n",
102+
"RETURN n.name AS name, labels(n) AS node_type;\n",
103+
"\"\"\"\n",
104+
"\n",
105+
"epfl_nodes = downloader.run_custom_query(epfl_query)\n",
106+
"print(\"RESULTS: all nodes that match the epfl string: \",epfl_nodes)\n",
107+
"\n",
108+
"# QUERY: Get all organizations\n",
109+
"\n",
110+
"orgs_query = \"\"\"\n",
111+
"MATCH (o:org)\n",
112+
"RETURN o.name AS organization;\n",
113+
"\"\"\"\n",
114+
"organizations = downloader.run_custom_query(orgs_query)\n",
115+
"print(\"RESULTS: all organizations in the graph: \",organizations)\n",
116+
"\n",
117+
"# QUERY : Get all users for an organization \n",
118+
"\n",
119+
"users_of_org_query = \"\"\"\n",
120+
"MATCH (u:user)-[:member_of]->(o:org)\n",
121+
"WHERE o.name = $org_name\n",
122+
"RETURN u.name AS user\n",
123+
"ORDER BY user;\n",
124+
"\"\"\"\n",
125+
"parameters = {\n",
126+
" \"org_name\": \"SwissDataScienceCenter\"\n",
127+
"}\n",
128+
"users_in_org = downloader.run_custom_query(users_of_org_query, parameters)\n",
129+
"print(\"RESULTS: users inside the organization: \", users_in_org)\n",
130+
"\n",
131+
"# QUERY: For a list of users get all their repositories\n",
132+
"\n",
133+
"# Here we base the query on the Contributor of edge.\n",
134+
"repos_of_users_query= \"\"\"\n",
135+
"MATCH (u:user)-[:contributor_of]->(repo:repo)\n",
136+
"WHERE u.name IN $user_list\n",
137+
"RETURN u.name AS user,\n",
138+
" repo.name AS repository\n",
139+
"ORDER BY user, repository;\n",
140+
"\"\"\"\n",
141+
"parameters = {\n",
142+
" \"user_list\": [\"yousra-elbachir\", \"Victor2175\", \"williamaeberhard\"]\n",
143+
"}\n",
144+
"users_and_their_repos = downloader.run_custom_query(repos_of_users_query, parameters)\n",
145+
"print(\"RESULTS: users and their repositories: \", users_and_their_repos)"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"id": "14bf5bb2",
151+
"metadata": {},
152+
"source": [
153+
"## Downloading data from Neo4j \n",
154+
"\n",
155+
"Here is an example of using the Neo4JDownloader to retrieve all nodes from the graph. \n",
156+
"Adapt the Neo4JDownloader class as you see fit for retrieving information. "
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"id": "1a349b96",
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"def extract_data(nodes, relationships):\n",
167+
" downloader = connect_neo4j()\n",
76168
"\n",
77169
" try:\n",
78170
" nodes_ids, nodes_features = downloader.retrieve_nodes(nodes)\n",

0 commit comments

Comments
 (0)