Skip to content

Commit 39bf5a7

Browse files
committed
feat: init neo4j templates
1 parent a77692b commit 39bf5a7

3 files changed

+863
-0
lines changed

Neo4j/Neo4j_Create_Node.ipynb

+312
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "143c505b",
6+
"metadata": {
7+
"execution": {
8+
"iopub.execute_input": "2021-02-23T14:22:16.610471Z",
9+
"iopub.status.busy": "2021-02-23T14:22:16.610129Z",
10+
"iopub.status.idle": "2021-02-23T14:22:16.627784Z",
11+
"shell.execute_reply": "2021-02-23T14:22:16.626866Z",
12+
"shell.execute_reply.started": "2021-02-23T14:22:16.610384Z"
13+
},
14+
"papermill": {},
15+
"tags": []
16+
},
17+
"source": [
18+
"<img width=\"8%\" alt=\"Neo4j.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Neo4j.png\" style=\"border-radius: 15%\">"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "bafb077c",
24+
"metadata": {
25+
"papermill": {},
26+
"tags": []
27+
},
28+
"source": [
29+
"# Neo4j - Create Node"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"id": "cb407ea8",
35+
"metadata": {
36+
"papermill": {},
37+
"tags": []
38+
},
39+
"source": [
40+
"**Tags:** #neo4j #knowledgegraph #node #snippet"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"id": "3fa65002",
46+
"metadata": {
47+
"papermill": {},
48+
"tags": []
49+
},
50+
"source": [
51+
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"id": "c7851585",
57+
"metadata": {
58+
"papermill": {},
59+
"tags": []
60+
},
61+
"source": [
62+
"**Last update:** 2024-05-22 (Created: 2024-05-22)"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"id": "188a2da0",
68+
"metadata": {
69+
"papermill": {},
70+
"tags": []
71+
},
72+
"source": [
73+
"**Description:** This notebook creates a node with properties in Neo4j using a Cypher query."
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"id": "498e71dd",
79+
"metadata": {
80+
"papermill": {},
81+
"tags": []
82+
},
83+
"source": [
84+
"## Input"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"id": "d2366b9a",
90+
"metadata": {
91+
"papermill": {},
92+
"tags": []
93+
},
94+
"source": [
95+
"### Import libraries"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c",
102+
"metadata": {
103+
"tags": []
104+
},
105+
"outputs": [],
106+
"source": [
107+
"from neo4j import GraphDatabase\n",
108+
"from datetime import datetime"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"id": "4acf67a5",
114+
"metadata": {
115+
"papermill": {},
116+
"tags": []
117+
},
118+
"source": [
119+
"### Setup variables\n",
120+
"- `url`: Database connection URL\n",
121+
"- `user`: Database username\n",
122+
"- `password`: Database password\n",
123+
"- `node_type`: Node type to be updated\n",
124+
"- `node_id`: Identifier of the node to be updated\n",
125+
"- `node_id_label`: Label for the node identifier\n",
126+
"- `properties`: Custom properties for the node\n",
127+
"- `output_file_path`: Path for the output file"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"id": "e98a53eb",
134+
"metadata": {
135+
"papermill": {},
136+
"tags": []
137+
},
138+
"outputs": [],
139+
"source": [
140+
"url = \"bolt://localhost:7687\"\n",
141+
"user = \"neo4j\"\n",
142+
"password = \"password\"\n",
143+
"node_type = \"Person\" # Type of node to update\n",
144+
"node_id = \"John\" # Identifier of node to update\n",
145+
"node_id_label = \"id\"\n",
146+
"properties = {\"name\": \"John\", \"age\": 30, \"city\": \"New York\", \"birthdate\": datetime(1990, 5, 17).strftime(\"%Y-%m-%dT%H:%M:%S\")} # Custom properties for node\n",
147+
"output_file_path = 'query_output.txt' # Output file path"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"id": "f91368ae",
153+
"metadata": {
154+
"papermill": {},
155+
"tags": []
156+
},
157+
"source": [
158+
"## Model"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"id": "f1fa2ed4-5944-41e1-99f9-0fb2288e0683",
164+
"metadata": {},
165+
"source": [
166+
"### Connect to GraphDatabase"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"id": "c4f3bf28-1661-4a17-98c0-02140aead632",
173+
"metadata": {
174+
"tags": []
175+
},
176+
"outputs": [],
177+
"source": [
178+
"driver = GraphDatabase.driver(url, auth=(username, password))"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"id": "6223fb35",
184+
"metadata": {
185+
"papermill": {},
186+
"tags": []
187+
},
188+
"source": [
189+
"### Create a node with custom type and properties in Neo4j"
190+
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": null,
195+
"id": "c164f0e3-3c2d-4c5d-ae5c-71df38cb4223",
196+
"metadata": {
197+
"tags": []
198+
},
199+
"outputs": [],
200+
"source": [
201+
"def create_node(\n",
202+
" driver,\n",
203+
" node_type,\n",
204+
" node_id,\n",
205+
" node_id_label=\"id\",\n",
206+
" properties=[],\n",
207+
" output_file_path='cypher_query.txt'\n",
208+
"):\n",
209+
" # Function to create a node with custom type and properties in Neo4j\n",
210+
" def create_custom_node(tx, node_type, node_id, node_id_label, properties):\n",
211+
" # Prepare properties string for Cypher query\n",
212+
" properties_list = []\n",
213+
" for key, value in properties.items():\n",
214+
" if 'date' in key:\n",
215+
" properties_list.append(f'n.{key} = datetime(\"{value}\")')\n",
216+
" elif isinstance(value, int) or isinstance(value, float):\n",
217+
" properties_list.append(f'n.{key} = {value}')\n",
218+
" else:\n",
219+
" properties_list.append(f'n.{key} = \"{value}\"')\n",
220+
" properties_str = ', '.join(properties_list)\n",
221+
" \n",
222+
" # Create cypher query\n",
223+
" cypher_query = f\"MERGE (n:{node_type} {{{node_id_label}: '{node_id}'}}) SET {properties_str}\"\n",
224+
" tx.run(cypher_query)\n",
225+
" with open(output_file_path, 'w') as f: # Open the output file in write mode\n",
226+
" f.write(cypher_query) # Write the query to the output file\n",
227+
"\n",
228+
" ### Use the driver to create a session and run the function\n",
229+
" with driver.session() as session:\n",
230+
" session.execute_write(create_custom_node, node_type, node_id, node_id_label, properties)\n",
231+
"\n",
232+
" ## Output\n",
233+
" ### Print success message\n",
234+
" print(\"Node created successfully.\")\n",
235+
" print(f\"Cypher query stored in {output_file_path}.\")\n",
236+
" \n",
237+
"create_node(\n",
238+
" driver,\n",
239+
" node_type,\n",
240+
" node_id,\n",
241+
" node_id_label,\n",
242+
" properties,\n",
243+
" output_file_path\n",
244+
")"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"id": "8dfea0ee",
250+
"metadata": {
251+
"papermill": {},
252+
"tags": []
253+
},
254+
"source": [
255+
"## Output"
256+
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"id": "cde6062b-7b01-473e-a2e7-2c76937e257f",
261+
"metadata": {},
262+
"source": [
263+
"### Display cypher query"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": null,
269+
"id": "3ca6b668-9c69-4d00-bc17-fbe47c3dfd22",
270+
"metadata": {
271+
"tags": []
272+
},
273+
"outputs": [],
274+
"source": [
275+
"with open(output_file_path, 'r') as f:\n",
276+
" data = f.read()\n",
277+
"print(f\"Cypher query: {data}\")"
278+
]
279+
}
280+
],
281+
"metadata": {
282+
"kernelspec": {
283+
"display_name": "Python 3",
284+
"language": "python",
285+
"name": "python3"
286+
},
287+
"language_info": {
288+
"codemirror_mode": {
289+
"name": "ipython",
290+
"version": 3
291+
},
292+
"file_extension": ".py",
293+
"mimetype": "text/x-python",
294+
"name": "python",
295+
"nbconvert_exporter": "python",
296+
"pygments_lexer": "ipython3",
297+
"version": "3.9.6"
298+
},
299+
"naas": {
300+
"notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a",
301+
"notebook_path": "LangChain/LangChain_Create_Agent.ipynb"
302+
},
303+
"papermill": {
304+
"default_parameters": {},
305+
"environment_variables": {},
306+
"parameters": {},
307+
"version": "2.6.0"
308+
}
309+
},
310+
"nbformat": 4,
311+
"nbformat_minor": 5
312+
}

0 commit comments

Comments
 (0)