{ "cells": [ { "cell_type": "markdown", "id": "3e4eb758", "metadata": {}, "source": [ "## Connecting a local database" ] }, { "cell_type": "markdown", "id": "2460ecfc", "metadata": {}, "source": [ "In this demo notebook we will showcase connecting a local database" ] }, { "cell_type": "markdown", "id": "8e0dbccf", "metadata": {}, "source": [ "### 1. Create an API session" ] }, { "cell_type": "code", "execution_count": 132, "id": "d96cfcaf", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"res\":\"ok\",\"data\":\"pong 1674727453166\"}\n" ] } ], "source": [ "import pandas as pd\n", "\n", "from weaveapi.records import *\n", "from weaveapi.options import *\n", "from weaveapi.filter import *\n", "from weaveapi.weaveh import *\n", "\n", "WEAVE_CONFIG = \"config/demo_client_local.config\"\n", "nodeApi, session = connect_weave_api(WEAVE_CONFIG)\n", "\n", "data_collection = \"localdb\"\n", "table = \"oncology_data\"" ] }, { "cell_type": "markdown", "id": "ae15388d", "metadata": {}, "source": [ "### 2. Install a local database (if not already having one)" ] }, { "cell_type": "markdown", "id": "adca4620", "metadata": {}, "source": [ "- it can be any database or file storage from the ones [supported](https://www.weavechain.com/integrations)\n", "- for the example we will assume a local postgres server is installed\n", "- or you can start a new postgres instance as a docker following the \"How to use this image\" step from [here](https://hub.docker.com/_/postgres). Sample:\n", "```\n", " docker run --name some-postgres -p 0.0.0.0:5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres\n", "```" ] }, { "cell_type": "markdown", "id": "2c71c819", "metadata": {}, "source": [ "#### Create a table and populate it with data" ] }, { "cell_type": "markdown", "id": "6e0f0073", "metadata": {}, "source": [ "- go to the folder where the node is installed\n", "- download a sample file\n", "```\n", " curl -O https://public.weavechain.com/file/sample.csv\n", "```\n", "- start the psql command prompt\n", "```\n", " psql -U postgres -d postgres -h localhost -p 5432\n", "```\n", "- if psql is not available, install it (see [this](https://www.timescale.com/blog/how-to-install-psql-on-mac-ubuntu-debian-windows/) guide from timescale) or run it from docker\n", "```\n", " docker run -it postgres /bin/bash -c \"psql -U postgres -d postgres -h host.docker.internal -p 5432\"\n", "```\n", "- ideally create a new database and user, but to simplify the steps we'll use the default *postgres* database and user\n", "- in the psql prompt, create a new table\n", "```\n", " CREATE TABLE \"oncology_data\" (\"id\" BIGINT NOT NULL,\"name\" TEXT,\"age\" NUMERIC,\"gender\" NUMERIC,\"air_pollution\" NUMERIC,\"alcohol_use\" NUMERIC,\"dust_allergy\" NUMERIC,\"occupational_hazards\" NUMERIC,\"genetic_risk\" NUMERIC,\"chronic_lung_disease\" NUMERIC,\"balanced_diet\" NUMERIC,\"obesity\" NUMERIC,\"smoking\" NUMERIC,\"passive_smoker\" NUMERIC,\"chest_pain\" NUMERIC,\"coughing_of_blood\" NUMERIC,\"fatigue\" NUMERIC,\"weight_loss\" NUMERIC,\"shortness_of_breath\" NUMERIC,\"wheezing\" NUMERIC,\"swallowing_difficulty\" NUMERIC,\"clubbing_of_fingernails\" NUMERIC,\"frequent_cold\" NUMERIC,\"dry_cough\" NUMERIC,\"snoring\" NUMERIC,\"level\" NUMERIC, CONSTRAINT pk_oncology_data PRIMARY KEY (\"id\"));\n", "```\n", "- and, in the same psql prompt, populate it with data (keep the \\ at the beginning)\n", "```\n", " \\COPY oncology_data FROM 'sample.csv' DELIMITER ',' CSV HEADER;\n", "```" ] }, { "cell_type": "markdown", "id": "ac907012", "metadata": {}, "source": [ "### 3. Add a new connection in the configuration file" ] }, { "cell_type": "markdown", "id": "7b4a5e29", "metadata": {}, "source": [ "- go to the node installation folder and edit config/demo.config\n", "- add a new item in the **databases** section, right before the existing **shared** subsection:\n", "```\n", " 'localdb': {\n", " 'connectionAdapterType': 'pgsql',\n", " 'replication': {\n", " 'type': 'none',\n", " 'allowedCachingIntervalSec': 604800\n", " },\n", " 'jdbcConfig': {\n", " 'host': 'host.docker.internal',\n", " 'port': 5432,\n", " 'schema': 'public',\n", " 'database': 'postgres',\n", " 'user': 'postgres',\n", " 'pass': 'mysecretpassword'\n", " }\n", " },\n", "```\n", "- If the node was started from docker, the address must point to the docker host machine, **host.docker.internal** will work on MacOS and windows. Use **localhost** if the node was started as standalone rather than a docker. Or the internal IP obtained via ipconfig/ifconfig. On linux 172.17.0.1 can also be used most of the time (usually assigned for the host machine to be visible from the docker if it's the single docker network interface)" ] }, { "cell_type": "markdown", "id": "512c99f6", "metadata": {}, "source": [ "#### Flag the node to reload the configuration file from disk" ] }, { "cell_type": "code", "execution_count": 133, "id": "17b212cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'res': 'ok', 'data': 'weaveconfig/demo.config'}\n" ] } ], "source": [ "reply = nodeApi.resetConfig(session).get()\n", "print(reply)" ] }, { "cell_type": "markdown", "id": "1e9b4f93", "metadata": {}, "source": [ "#### Restart the node" ] }, { "cell_type": "markdown", "id": "317e474b", "metadata": {}, "source": [ "- run from the command prompt\n", "```\n", " docker stop weave_node\n", " docker start weave_node\n", "```" ] }, { "cell_type": "markdown", "id": "b37753f1", "metadata": {}, "source": [ "### 4. Read data from the newly added table" ] }, { "cell_type": "code", "execution_count": 138, "id": "b6ebc27d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"res\":\"ok\",\"data\":\"pong 1674727467726\"}\n" ] }, { "data": { "text/html": [ "
\n", " | id | \n", "name | \n", "age | \n", "gender | \n", "air_pollution | \n", "alcohol_use | \n", "dust_allergy | \n", "occupational_hazards | \n", "genetic_risk | \n", "chronic_lung_disease | \n", "... | \n", "fatigue | \n", "weight_loss | \n", "shortness_of_breath | \n", "wheezing | \n", "swallowing_difficulty | \n", "clubbing_of_fingernails | \n", "frequent_cold | \n", "dry_cough | \n", "snoring | \n", "level | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "Lorenzo Rasmussen | \n", "33 | \n", "1 | \n", "2 | \n", "4 | \n", "5 | \n", "4 | \n", "3 | \n", "2 | \n", "... | \n", "3 | \n", "4 | \n", "2 | \n", "2 | \n", "3 | \n", "1 | \n", "2 | \n", "3 | \n", "4 | \n", "1 | \n", "
1 | \n", "2 | \n", "Zechariah Gallegos | \n", "17 | \n", "1 | \n", "3 | \n", "1 | \n", "5 | \n", "3 | \n", "4 | \n", "2 | \n", "... | \n", "1 | \n", "3 | \n", "7 | \n", "8 | \n", "6 | \n", "2 | \n", "1 | \n", "7 | \n", "2 | \n", "2 | \n", "
2 | \n", "3 | \n", "Lukas Jenkins | \n", "35 | \n", "1 | \n", "4 | \n", "5 | \n", "6 | \n", "5 | \n", "5 | \n", "4 | \n", "... | \n", "8 | \n", "7 | \n", "9 | \n", "2 | \n", "1 | \n", "4 | \n", "6 | \n", "7 | \n", "2 | \n", "3 | \n", "
3 | \n", "4 | \n", "Trey Holden | \n", "37 | \n", "1 | \n", "7 | \n", "7 | \n", "7 | \n", "7 | \n", "6 | \n", "7 | \n", "... | \n", "4 | \n", "2 | \n", "3 | \n", "1 | \n", "4 | \n", "5 | \n", "6 | \n", "7 | \n", "5 | \n", "3 | \n", "
4 | \n", "5 | \n", "Branson Rivera | \n", "46 | \n", "1 | \n", "6 | \n", "8 | \n", "7 | \n", "7 | \n", "7 | \n", "6 | \n", "... | \n", "3 | \n", "2 | \n", "4 | \n", "1 | \n", "4 | \n", "2 | \n", "4 | \n", "2 | \n", "3 | \n", "3 | \n", "
5 rows × 26 columns
\n", "