{ "cells": [ { "cell_type": "markdown", "id": "22d93371-d72a-498a-b25e-affb11659f0d", "metadata": {}, "source": [ "## NeoSchema library - Tutorial 1 : basic Schema operations (Classes, Properties, Data Nodes)\n", "#### This is a Schema-layer version of the tutorial `tutorial_neoaccess_1`\n", "If you need to first clear out your test database, one of the cells below (currently commented out) will conveniently let you do it\n", "\n", "#### [Article](https://julianspolymathexplorations.blogspot.com/2022/11/schema-graph-databases-neo4j.html) to accompany this tutorial" ] }, { "cell_type": "code", "execution_count": 1, "id": "910c294a-eb6b-43d7-9369-980f20974e12", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 'D:\\Docs\\- MY CODE\\Brain Annex\\BA-Win7' to sys.path\n" ] } ], "source": [ "import set_path # Importing this module will add the project's home directory to sys.path" ] }, { "cell_type": "code", "execution_count": 2, "id": "e00686a6-c019-414e-92be-a44d32cfe138", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import getpass\n", "\n", "from neoaccess import NeoAccess\n", "from BrainAnnex.modules.neo_schema.neo_schema import NeoSchema" ] }, { "cell_type": "code", "execution_count": null, "id": "62138c8f-f546-482a-a2b6-fe6d3623619c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "be1fb174-5bb9-4dee-a920-0ac5dcfb74a5", "metadata": {}, "source": [ "# Connect to the database\n", "#### using the `NeoAccess` library\n", "#### You can use a free local install of the Neo4j database, or a remote one on a virtual machine under your control, or a hosted solution, or simply the FREE \"Sandbox\" : [instructions here](https://julianspolymathexplorations.blogspot.com/2023/03/neo4j-sandbox-tutorial-cypher.html)\n", "NOTE: This tutorial is tested on version 4 of the Neo4j database, but will probably also work on the new version 5" ] }, { "cell_type": "code", "execution_count": 3, "id": "6d6452fe-06a6-486a-b4f1-1568e6bde8d5", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Save your credentials here (and skip the next cell!) - or use the prompts given by the next cell\n", "#host = \"\" # EXAMPLES: bolt://123.456.789.012 OR neo4j://localhost\n", "#password = \"\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "ef54730b-5fea-4da1-9084-9844bf5c92cc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To create a database connection, enter the host IP, but leave out the port number: (EXAMPLES: bolt://1.2.3.4 OR neo4j://localhost )\n", "\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "Enter host IP WITHOUT the port number. EXAMPLE: bolt://123.456.789.012 bolt://123.456.789.012\n", "Enter the database password: ········\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "=> Will be using: host='bolt://123.456.789.012:7687', username='neo4j', password=**********\n" ] } ], "source": [ "print(\"To create a database connection, enter the host IP, but leave out the port number: (EXAMPLES: bolt://1.2.3.4 OR neo4j://localhost )\\n\")\n", "\n", "host = input(\"Enter host IP WITHOUT the port number. EXAMPLE: bolt://123.456.789.012 \")\n", "host += \":7687\" # EXAMPLE of host value: \"bolt://123.456.789.012:7687\"\n", "\n", "password = getpass.getpass(\"Enter the database password:\")\n", "\n", "print(f\"\\n=> Will be using: host='{host}', username='neo4j', password=**********\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c3965805-0d5e-44d8-8d4d-6aaecde6497c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 4, "id": "7247f139-9f06-41d1-98e8-410ff7c9f177", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connection to Neo4j database established.\n" ] } ], "source": [ "db = NeoAccess(host=host,\n", " credentials=(\"neo4j\", password), debug=False) # Notice the debug option being OFF" ] }, { "cell_type": "code", "execution_count": 5, "id": "c96ece03-2b07-4a4d-ad6e-e41ccbf67251", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Version of the Neo4j driver: 4.4.11\n" ] } ], "source": [ "print(\"Version of the Neo4j driver: \", db.version())" ] }, { "cell_type": "code", "execution_count": null, "id": "20b1f218-cfba-4259-8ffd-6aee6f1ed589", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4ca98da0-f267-4efa-8302-624efbd1a744", "metadata": {}, "source": [ "# Examples of basic `NeoSchema` library operations" ] }, { "cell_type": "markdown", "id": "f5d03ea0-0282-4d95-9f4a-852d04cc8f57", "metadata": {}, "source": [ "### Initial setup" ] }, { "cell_type": "code", "execution_count": 6, "id": "c3e8506e-b904-48c1-a22a-2818807814cf", "metadata": {}, "outputs": [], "source": [ "# db.empty_dbase() # ****** Recommended for use with test databases. WARNING: USE WITH CAUTION!!! ******" ] }, { "cell_type": "code", "execution_count": 7, "id": "48e9ff90-6698-48a1-b8d2-e715dc7f191b", "metadata": { "tags": [] }, "outputs": [], "source": [ "NeoSchema.set_database(db)" ] }, { "cell_type": "markdown", "id": "2410dc5c-f720-4462-a0e6-32423d1083cb", "metadata": {}, "source": [ "### Create the Schema" ] }, { "cell_type": "code", "execution_count": 8, "id": "91bc9913-6680-4738-9c89-a33b4ea1c670", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Create a \"Car\" Class node. Class nodes are part of the Schema\n", "\n", "car_class_id, car_class_uri = NeoSchema.create_class(name=\"Car\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "76c198a6-13c3-44c1-a6d0-b72cf9b52a1b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Declare various Properties (data fields) for the \"Car\" Class\n", "\n", "NeoSchema.add_properties_to_class(class_node = car_class_id, property_list = ['color', 'make'])" ] }, { "cell_type": "code", "execution_count": 10, "id": "52bafa2c-1957-4285-ac70-44aab9d10dc1", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(22390, 4)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Do a similar construction for a \"Person\" Class node - but this time do it all in just 1 step\n", "\n", "NeoSchema.create_class_with_properties(name=\"Person\", property_list=[\"name\"])" ] }, { "cell_type": "code", "execution_count": 11, "id": "276d160f-f703-420c-b9c2-1fe6bdd6308a", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Now add a relationship named \"OWNED_BY\", from the \"Car\" Class to the \"Person\" Class\n", "\n", "NeoSchema.create_class_relationship(from_class=\"Car\", to_class=\"Person\", rel_name=\"OWNED_BY\")" ] }, { "cell_type": "markdown", "id": "af53da3d-1679-4153-8561-cc99644c86c4", "metadata": {}, "source": [ "_This is what we have so far:_" ] }, { "cell_type": "markdown", "id": "bac35ca8-618e-4fde-a355-a5867fc98518", "metadata": {}, "source": [ "![Schema](../BrainAnnex/docs/schema_tutorial_1.jpg)" ] }, { "cell_type": "markdown", "id": "7dfe8ba5-035f-4381-8bba-4337febf6e45", "metadata": {}, "source": [ "### Adding the data" ] }, { "cell_type": "code", "execution_count": 12, "id": "05320df5-79e3-4eac-8dd2-b23bf0bc98a5", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Having set up the Schema, time for some actual data! Add a Data Node for a \"Car\", and one for a \"Person\"\n", "# Note that create_data_node() returns the internal database ID of the new node\n", "\n", "car_id = NeoSchema.create_data_node(class_node=\"Car\", properties={'color': 'white', 'make': 'Toyota'})\n", "\n", "person_id = NeoSchema.create_data_node(class_node=\"Person\", properties={'name': 'Julian'})" ] }, { "cell_type": "code", "execution_count": 13, "id": "aba94b6a-890e-4867-8e4d-842a8d9eabc6", "metadata": {}, "outputs": [], "source": [ "# Finally, add a relationship named \"OWNED_BY\", from the \"Car\" DATA node to the \"Person\" DATA node (as sanctioned in the Schema declared earlier)\n", "\n", "NeoSchema.add_data_relationship(from_id=car_id, to_id=person_id, rel_name=\"OWNED_BY\")" ] }, { "cell_type": "markdown", "id": "408e376d-52fd-433e-bd7f-e21f933e46af", "metadata": {}, "source": [ "![Schema](../BrainAnnex/docs/schema_tutorial_2.jpg)" ] }, { "cell_type": "markdown", "id": "e705e78d-6bc9-42a9-98e8-bf3b1f82d842", "metadata": {}, "source": [ "### We'll keep it simple in this tutorial! Data validation, URI's, etc, will be explored in later tutorials..." ] }, { "cell_type": "code", "execution_count": null, "id": "dfa9eb85-974d-4212-9906-0e938ce213f0", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }