{ "cells": [ { "cell_type": "markdown", "id": "22d93371-d72a-498a-b25e-affb11659f0d", "metadata": {}, "source": [ "## Tests of connection to the Neo4j database, and basic NeoAccess library operations\n", "### using the `debug` option" ] }, { "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 BrainAnnex.modules.neo_access.neo_access import NeoAccess" ] }, { "cell_type": "code", "execution_count": 3, "id": "8ebe967e-3446-4ca5-8584-9603ec532a9b", "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://155.248.202.124\n", "Enter the database password: ········\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "=> Will be using: host='bolt://155.248.202.124: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": 4, "id": "7247f139-9f06-41d1-98e8-410ff7c9f177", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "~~~~~~~~~ Initializing NeoAccess object ~~~~~~~~~\n", "Attempting to connect to Neo4j host 'bolt://155.248.202.124:7687', with username 'neo4j'\n", "Connection to host 'bolt://155.248.202.124:7687' established\n" ] } ], "source": [ "db = NeoAccess(host=host,\n", " credentials=(\"neo4j\", password), debug=True)" ] }, { "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.3.9\n" ] } ], "source": [ "print(\"Version of the Neo4j driver: \", db.version())" ] }, { "cell_type": "code", "execution_count": 6, "id": "c3e8506e-b904-48c1-a22a-2818807814cf", "metadata": {}, "outputs": [], "source": [ "db.empty_dbase() # WARNING: USE WITH CAUTION!!!" ] }, { "cell_type": "code", "execution_count": 7, "id": "9b1f3651-f613-4966-b1ac-f2eddfbb2e79", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "In create_node(). Query:\n", " CREATE (n :`Car` {`color`: $par_1, `make`: $par_2}) RETURN n\n", "Data binding:\n", " {'par_1': 'white', 'par_2': 'Toyota'}\n", "\n", "\n", "In create_node(). Query:\n", " CREATE (n :`Person` {`name`: $par_1}) RETURN n\n", "Data binding:\n", " {'par_1': 'Julian'}\n", "\n" ] } ], "source": [ "neo_car = db.create_node(\"Car\", {'color': 'white', 'make': 'Toyota'})\n", "neo_person = db.create_node(\"Person\", {'name': 'Julian'})" ] }, { "cell_type": "code", "execution_count": 8, "id": "e3e52fdc-cef4-4efa-b402-9cae01c14b77", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "neo_car" ] }, { "cell_type": "code", "execution_count": 9, "id": "2e3236ee-b97e-4ffc-90bf-fd9949dcea1d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "neo_person" ] }, { "cell_type": "code", "execution_count": 10, "id": "5063cec5-e693-4002-aa84-c5c71240039d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In add_links()\n", " match_from: {'node': '(from)', 'where': 'id(from) = 30', 'data_binding': {}, 'dummy_node_name': 'from'}\n", " match_to: {'node': '(to)', 'where': 'id(to) = 0', 'data_binding': {}, 'dummy_node_name': 'to'}\n", "\n", "In add_links(). Query:\n", " \n", " MATCH (from), (to)\n", " WHERE (id(from) = 30 AND id(to) = 0)\n", " MERGE (from) -[:`OWNED_BY`]-> (to) \n", " \n", "\n", "In update_query(). Attributes of ResultSummary object:\n", " metadata -> {'query': '\\n MATCH (from), (to)\\n WHERE (id(from) = 30 AND id(to) = 0)\\n MERGE (from) -[:`OWNED_BY`]-> (to) \\n ', 'parameters': {}, 'server': , 't_first': 5, 'fields': [], 'bookmark': 'FB:kcwQxosFA855RdybwjuMf2O2J8l5R5A=', 'stats': {'relationships-created': 1}, 'type': 'w', 't_last': 0, 'db': 'neo4j', 'notifications': [{'severity': 'WARNING', 'description': 'If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (to))', 'code': 'Neo.ClientNotification.Statement.CartesianProductWarning', 'position': {'column': 1, 'offset': 13, 'line': 2}, 'title': 'This query builds a cartesian product between disconnected patterns.'}]}\n", " server -> \n", " database -> neo4j\n", " query -> \n", " MATCH (from), (to)\n", " WHERE (id(from) = 30 AND id(to) = 0)\n", " MERGE (from) -[:`OWNED_BY`]-> (to) \n", " \n", " parameters -> {}\n", " query_type -> w\n", " plan -> None\n", " profile -> None\n", " notifications -> [{'severity': 'WARNING', 'description': 'If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (to))', 'code': 'Neo.ClientNotification.Statement.CartesianProductWarning', 'position': {'column': 1, 'offset': 13, 'line': 2}, 'title': 'This query builds a cartesian product between disconnected patterns.'}]\n", " counters -> {'relationships_created': 1}\n", " result_available_after -> 5\n", " result_consumed_after -> 0\n", " RESULT of update_query in add_links(): {'relationships_created': 1, 'returned_data': []}\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number_added = db.add_links(neo_car, neo_person, rel_name=\"OWNED_BY\")\n", "\n", "number_added" ] }, { "cell_type": "code", "execution_count": 11, "id": "0731d9f7-f9f3-4659-83ad-8c88409d5c1e", "metadata": {}, "outputs": [], "source": [ "match_from = db.match(internal_id=neo_car)\n", "match_to = db.match(internal_id=neo_person)" ] }, { "cell_type": "code", "execution_count": 12, "id": "80f5e301-69af-4e86-b78b-bd8c222809ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'internal_id': 30,\n", " 'labels': None,\n", " 'key_name': None,\n", " 'key_value': None,\n", " 'properties': None,\n", " 'clause': None,\n", " 'dummy_node_name': None}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match_from" ] }, { "cell_type": "code", "execution_count": 13, "id": "67713a65-6c2f-485f-a972-9fe02dbc9e9d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'internal_id': 0,\n", " 'labels': None,\n", " 'key_name': None,\n", " 'key_value': None,\n", " 'properties': None,\n", " 'clause': None,\n", " 'dummy_node_name': None}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match_to" ] }, { "cell_type": "code", "execution_count": 14, "id": "2d21fdf8-9642-4395-ad3a-258040aeccba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In get_nodes()\n", " match_structure: {'node': '(n)', 'where': 'id(n) = 30', 'data_binding': {}, 'dummy_node_name': 'n'}\n", "\n", "In get_nodes(). Query:\n", " MATCH (n) WHERE (id(n) = 30) RETURN n\n", "\n" ] }, { "data": { "text/plain": [ "[{'color': 'white', 'make': 'Toyota'}]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrieve the car nodes\n", "db.get_nodes(neo_car)" ] }, { "cell_type": "code", "execution_count": 15, "id": "69c5abc3-8cde-4caf-be60-32ee27441040", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In get_nodes()\n", " match_structure: {'node': '(n)', 'where': 'id(n) = 30', 'data_binding': {}, 'dummy_node_name': 'n'}\n", "\n", "In get_nodes(). Query:\n", " MATCH (n) WHERE (id(n) = 30) RETURN n\n", "\n" ] }, { "data": { "text/plain": [ "'white'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrieve a single property of the car node (for situation when only 1 node is present)\n", "db.get_nodes(neo_car, single_cell=\"color\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "b073d441-1e4e-4ae8-864f-cca5654cb279", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In count_links()\n", " match_structure: {'node': '(n)', 'where': 'id(n) = 30', 'data_binding': {}, 'dummy_node_name': 'n'}\n", "\n", "In count_links(). Query:\n", " MATCH (n) - [:OWNED_BY] -> (neighbor )WHERE (id(n) = 30) RETURN count(neighbor) AS link_count\n", "\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How many owners does the car have?\n", "db.count_links(neo_car, rel_name=\"OWNED_BY\", rel_dir=\"OUT\") " ] }, { "cell_type": "code", "execution_count": 17, "id": "093da0e7-8dc4-4908-9347-d91b3168c556", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In follow_links()\n", " match_structure: {'node': '(n)', 'where': 'id(n) = 30', 'data_binding': {}, 'dummy_node_name': 'n'}\n", "\n", "In follow_links(). Query:\n", " MATCH (n) - [:OWNED_BY] -> (neighbor )WHERE (id(n) = 30) RETURN neighbor\n", "\n" ] }, { "data": { "text/plain": [ "[{'name': 'Julian'}]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Look up information about the car owner(s)\n", "db.follow_links(neo_car, rel_name=\"OWNED_BY\", rel_dir=\"OUT\") " ] } ], "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 }