Skip to content

Commit

Permalink
feat: init neo4j templates
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed May 23, 2024
1 parent a77692b commit 39bf5a7
Show file tree
Hide file tree
Showing 3 changed files with 863 additions and 0 deletions.
312 changes: 312 additions & 0 deletions Neo4j/Neo4j_Create_Node.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "143c505b",
"metadata": {
"execution": {
"iopub.execute_input": "2021-02-23T14:22:16.610471Z",
"iopub.status.busy": "2021-02-23T14:22:16.610129Z",
"iopub.status.idle": "2021-02-23T14:22:16.627784Z",
"shell.execute_reply": "2021-02-23T14:22:16.626866Z",
"shell.execute_reply.started": "2021-02-23T14:22:16.610384Z"
},
"papermill": {},
"tags": []
},
"source": [
"<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%\">"
]
},
{
"cell_type": "markdown",
"id": "bafb077c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Neo4j - Create Node"
]
},
{
"cell_type": "markdown",
"id": "cb407ea8",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #neo4j #knowledgegraph #node #snippet"
]
},
{
"cell_type": "markdown",
"id": "3fa65002",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)"
]
},
{
"cell_type": "markdown",
"id": "c7851585",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2024-05-22 (Created: 2024-05-22)"
]
},
{
"cell_type": "markdown",
"id": "188a2da0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook creates a node with properties in Neo4j using a Cypher query."
]
},
{
"cell_type": "markdown",
"id": "498e71dd",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "d2366b9a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61e8eabc-4c9f-4314-bc1e-3c09b4c3266c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from neo4j import GraphDatabase\n",
"from datetime import datetime"
]
},
{
"cell_type": "markdown",
"id": "4acf67a5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `url`: Database connection URL\n",
"- `user`: Database username\n",
"- `password`: Database password\n",
"- `node_type`: Node type to be updated\n",
"- `node_id`: Identifier of the node to be updated\n",
"- `node_id_label`: Label for the node identifier\n",
"- `properties`: Custom properties for the node\n",
"- `output_file_path`: Path for the output file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e98a53eb",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"url = \"bolt://localhost:7687\"\n",
"user = \"neo4j\"\n",
"password = \"password\"\n",
"node_type = \"Person\" # Type of node to update\n",
"node_id = \"John\" # Identifier of node to update\n",
"node_id_label = \"id\"\n",
"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",
"output_file_path = 'query_output.txt' # Output file path"
]
},
{
"cell_type": "markdown",
"id": "f91368ae",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "f1fa2ed4-5944-41e1-99f9-0fb2288e0683",
"metadata": {},
"source": [
"### Connect to GraphDatabase"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4f3bf28-1661-4a17-98c0-02140aead632",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"driver = GraphDatabase.driver(url, auth=(username, password))"
]
},
{
"cell_type": "markdown",
"id": "6223fb35",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Create a node with custom type and properties in Neo4j"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c164f0e3-3c2d-4c5d-ae5c-71df38cb4223",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def create_node(\n",
" driver,\n",
" node_type,\n",
" node_id,\n",
" node_id_label=\"id\",\n",
" properties=[],\n",
" output_file_path='cypher_query.txt'\n",
"):\n",
" # Function to create a node with custom type and properties in Neo4j\n",
" def create_custom_node(tx, node_type, node_id, node_id_label, properties):\n",
" # Prepare properties string for Cypher query\n",
" properties_list = []\n",
" for key, value in properties.items():\n",
" if 'date' in key:\n",
" properties_list.append(f'n.{key} = datetime(\"{value}\")')\n",
" elif isinstance(value, int) or isinstance(value, float):\n",
" properties_list.append(f'n.{key} = {value}')\n",
" else:\n",
" properties_list.append(f'n.{key} = \"{value}\"')\n",
" properties_str = ', '.join(properties_list)\n",
" \n",
" # Create cypher query\n",
" cypher_query = f\"MERGE (n:{node_type} {{{node_id_label}: '{node_id}'}}) SET {properties_str}\"\n",
" tx.run(cypher_query)\n",
" with open(output_file_path, 'w') as f: # Open the output file in write mode\n",
" f.write(cypher_query) # Write the query to the output file\n",
"\n",
" ### Use the driver to create a session and run the function\n",
" with driver.session() as session:\n",
" session.execute_write(create_custom_node, node_type, node_id, node_id_label, properties)\n",
"\n",
" ## Output\n",
" ### Print success message\n",
" print(\"Node created successfully.\")\n",
" print(f\"Cypher query stored in {output_file_path}.\")\n",
" \n",
"create_node(\n",
" driver,\n",
" node_type,\n",
" node_id,\n",
" node_id_label,\n",
" properties,\n",
" output_file_path\n",
")"
]
},
{
"cell_type": "markdown",
"id": "8dfea0ee",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "cde6062b-7b01-473e-a2e7-2c76937e257f",
"metadata": {},
"source": [
"### Display cypher query"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ca6b668-9c69-4d00-bc17-fbe47c3dfd22",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"with open(output_file_path, 'r') as f:\n",
" data = f.read()\n",
"print(f\"Cypher query: {data}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"naas": {
"notebook_id": "0ee0ab05a4a7ec68df589076942e71a2dd9a2548b6324496dae89565be89e92a",
"notebook_path": "LangChain/LangChain_Create_Agent.ipynb"
},
"papermill": {
"default_parameters": {},
"environment_variables": {},
"parameters": {},
"version": "2.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 39bf5a7

Please sign in to comment.