{ "cells": [ { "cell_type": "markdown", "source": [ "## This notebook shows some example gremlin queries using nodejs gremlin driver. Driver details are here: https://www.npmjs.com/package/gremlin. \n", "\n", "## You will use PixieDust extension that enables a Jupyter Notebook user to invoke Node.js commands.\n", "\n" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Install pixiedust_node" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "!pip install pixiedust_node" ], "outputs": [], "metadata": { "scrolled": true } }, { "cell_type": "markdown", "source": [ "### Import pixiedust_node" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "import pixiedust_node" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Install gremlin javascript driver" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# install node packages\n", "npm.install('gremlin')\n", "npm.install('uuid')" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node \n", "\n", "const { v4: uuidv4 } = require('uuid');\n", "const gremlin = require('gremlin');\n", "const traversal = gremlin.process.AnonymousTraversalSource.traversal;\n", "const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection\n", "\n", "const { cardinality: { single } } = gremlin.process;\n", "const __ = gremlin.process.statics;\n", "const column = gremlin.process.column\n", "const direction = gremlin.process.direction\n", "const p = gremlin.process.P\n", "const textp = gremlin.process.TextP\n", "const pick = gremlin.process.pick\n", "const pop = gremlin.process.pop\n", "const order = gremlin.process.order\n", "const scope = gremlin.process.scope\n", "const t = gremlin.process.t\n", "\n", "const ClusterEndpoint = \"\";\n", "const Port = \"Port\";\n", "\n", "const g = traversal().withRemote(new DriverRemoteConnection(`wss://${ClusterEndpoint}:${Port}/gremlin`));\n" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Create Single Vertex" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "var personId = uuidv4();\n", "\n", "try {\n", " const queryPromise = g.addV('person').property(t.id, personId)\n", " .property('email','user@domain.com')\n", " .property(single, 'firstname', 'firstname')\n", " .property(single, 'lastname', 'lastname')\n", " .next();\n", " \n", " queryPromise.then(data => {\n", " console.log(data);\n", " })\n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}\n" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "var movieId = uuidv4();\n", "\n", "try {\n", " const queryPromise = g.addV('movie')\n", " .property(t.id, movieId)\n", " .property(single, 'title', 'movietitle')\n", " .next();\n", " \n", " queryPromise.then(data => {\n", " console.log(JSON.stringify(data));\n", " })\n", "\n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}\n" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Add edges between existing vertices" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "try {\n", " const queryPromise = g.addE(\"watched\")\n", " .from_(__.V(personId)).to(__.V(movieId))\n", " .next();\n", " \n", " queryPromise.then(data => {\n", " console.log(data.value);\n", " })\n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Add multiple vertices and edges" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "try {\n", " const queryPromise = g.addV('person').property(t.id, uuidv4())\n", " .property('email', \"user@domain.com\")\n", " .property(single, 'firstname', 'firstname')\n", " .property(single, 'lastname', 'lastname')\n", " .as(\"user\")\n", " .addV('movie')\n", " .property(t.id, uuidv4())\n", " .property(single, 'title', 'movietitle')\n", " .as(\"movie\")\n", " .addE(\"watched\")\n", " .from_('user').to('movie')\n", " .next();\n", " \n", " queryPromise.then(data => {\n", " console.log(data);\n", " })\n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}\n" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Read vertex and its attributes" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "try {\n", " \n", " const getPersonPromise= g.V().hasLabel(\"movie\").valueMap(true).limit(10).toList();\n", " \n", " console.log(\"\\n\");\n", " console.log(\"id|label|title\");\n", " \n", " getPersonPromise.then(data => {\n", " data.map(item => {\n", " console.log(item.get(t.id) + \"|\" + item.get(t.label) + \"|\" + item.get(\"title\"))\n", " });\n", " });\n", " \n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Read vertex connections" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "try {\n", " \n", " const getPersonPromise= g.V(personId).out(\"watched\").valueMap(true).limit(10).toList();\n", " \n", " getPersonPromise.then(data => {\n", " data.forEach(item => {\n", " console.log(item);\n", " });\n", " });\n", " \n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}" ], "outputs": [], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "%%node\n", "\n", "function formatAndPrint(data){\n", " console.log(data.get(\"personid\").padEnd(20,\" \") + data.get(\"movies\")[0].get(\"title\"));\n", "}\n", "\n", "try {\n", " \n", " const getPersonPromise = g.V(personId).project('personid','movies')\n", " .by(\"email\").by(__.out(\"watched\").valueMap(\"title\").fold())\n", " .limit(10).toList();\n", " console.log(\"\\n\");\n", " console.log(\"Email\".padEnd(20,\" \") + \"Title\".padEnd(20,\" \"));\n", " console.log(\"\".padEnd(50,\"_\"));\n", " \n", " getPersonPromise.then(data => {\n", " data.forEach(item => { \n", " formatAndPrint(item);\n", " }); \n", " }); \n", "}\n", "catch(ex){\n", " console.log(ex);\n", "}" ], "outputs": [], "metadata": {} } ], "metadata": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" }, "kernelspec": { "name": "python3", "display_name": "Python 3.7.7 64-bit" }, "language_info": { "name": "python", "version": "3.7.7", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" } }, "nbformat": 4, "nbformat_minor": 5 }