Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alonsosilvaallende/1b2edb0ef193d5e25b51d85003d09d9a to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/1b2edb0ef193d5e25b51d85003d09d9a to your computer and use it in GitHub Desktop.
Kuzu-add-nodes-and-edges-simple.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO1BObLxN8Js43Pb5eRS6nq",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/alonsosilvaallende/1b2edb0ef193d5e25b51d85003d09d9a/kuzu-add-nodes-and-edges-simple.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "CUSbw5gEXo68"
},
"outputs": [],
"source": [
"%pip install --upgrade --quiet kuzu"
]
},
{
"cell_type": "code",
"source": [
"import kuzu"
],
"metadata": {
"id": "mPE6T7toXrhR"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import shutil\n",
"\n",
"shutil.rmtree(\"./test\", ignore_errors=True)"
],
"metadata": {
"id": "glm5_0V9XtXr"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Initialize database\n",
"db = kuzu.Database(\"./test\")\n",
"conn = kuzu.Connection(db)"
],
"metadata": {
"id": "-rVLvttnXvwy"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Create schema\n",
"conn.execute(\"CREATE NODE TABLE Actor(name STRING, PRIMARY KEY (name))\")\n",
"conn.execute(\"CREATE NODE TABLE Movie(title STRING, PRIMARY KEY (title))\")\n",
"conn.execute(\"CREATE REL TABLE ActsIn(FROM Actor TO Movie, role STRING)\");"
],
"metadata": {
"id": "ABi97gp-XyjA"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Insert data\n",
"conn.execute(\"CREATE (neo:Actor {name: 'Keanu Reeves'})\")\n",
"conn.execute(\"CREATE (matrix:Movie {title: 'The Matrix'})\")\n",
"conn.execute(\n",
" \"\"\"\n",
" MATCH (a:Actor), (m:Movie) WHERE a.name = 'Keanu Reeves' AND m.title = 'The Matrix'\n",
" CREATE (a)-[:ActsIn {role: 'Neo'}]->(m)\n",
" \"\"\"\n",
");"
],
"metadata": {
"id": "GgWD-im1X13g"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Execute Cypher query\n",
"response = conn.execute(\n",
" \"\"\"\n",
" MATCH (a:Actor)-[r:ActsIn]->(m:Movie)\n",
" RETURN a.name, m.title, r.role;\n",
" \"\"\"\n",
");"
],
"metadata": {
"id": "F5v0vml0YYuy"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Show response\n",
"while response.has_next():\n",
" print(response.get_next())"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nM2W-6_mYgTl",
"outputId": "534fd2ce-4513-4e2e-8120-dba3768f2c7d"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['Keanu Reeves', 'The Matrix', 'Neo']\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Get response as a pandas dataframe\n",
"df_response = response.get_as_df()\n",
"df_response"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 89
},
"id": "rkxKE4A8Yi3Y",
"outputId": "88aafcf6-cca7-4057-80d1-87c4bc39fcae"
},
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" a.name m.title r.role\n",
"0 Keanu Reeves The Matrix Neo"
],
"text/html": [
"\n",
" <div id=\"df-28d07bcc-2695-4b1b-863b-6a65186f4b7d\" class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a.name</th>\n",
" <th>m.title</th>\n",
" <th>r.role</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Keanu Reeves</td>\n",
" <td>The Matrix</td>\n",
" <td>Neo</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-28d07bcc-2695-4b1b-863b-6a65186f4b7d')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-28d07bcc-2695-4b1b-863b-6a65186f4b7d button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-28d07bcc-2695-4b1b-863b-6a65186f4b7d');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
" <div id=\"id_9d4ebd56-669e-442c-b5c4-378371013abe\">\n",
" <style>\n",
" .colab-df-generate {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-generate:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-generate:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
" <button class=\"colab-df-generate\" onclick=\"generateWithVariable('df_response')\"\n",
" title=\"Generate code using this dataframe.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n",
" </svg>\n",
" </button>\n",
" <script>\n",
" (() => {\n",
" const buttonEl =\n",
" document.querySelector('#id_9d4ebd56-669e-442c-b5c4-378371013abe button.colab-df-generate');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" buttonEl.onclick = () => {\n",
" google.colab.notebook.generateWithVariable('df_response');\n",
" }\n",
" })();\n",
" </script>\n",
" </div>\n",
"\n",
" </div>\n",
" </div>\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "df_response",
"summary": "{\n \"name\": \"df_response\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": \"a.name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"Keanu Reeves\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"m.title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"The Matrix\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"r.role\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"Neo\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
}
},
"metadata": {},
"execution_count": 9
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment