{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive Query with Gremlin\n", "Gremlin is one of the most popular query languages in graph databases, as SQL in relational databases. Here, we will give some examples to illustrate how gremlin helps navigate the vertices and edges of a graph." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install graphscope package if you are NOT in the Playground\n", "\n", "!pip3 install graphscope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dataset\n", "MODERN, a toy graph from [tinkerpop](https://tinkerpop.apache.org/docs/current/reference/), which consists of 6 vertices and 6 edges. Here are the edges:\n", "\n", "\\[(1,3),(1,2),(1,4),(4,5),(4,3),(6,3)\\]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import the graphscope module and load the modern graph\n", "\n", "import graphscope\n", "from graphscope.dataset import load_modern_graph\n", "\n", "graphscope.set_option(show_log=False) # enable logging\n", "\n", "modern_graph = load_modern_graph()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create GIE engine for Gremlin\n", "\n", "interactive = graphscope.gremlin(modern_graph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Between Vertices\n", "Traversals between two particular vertices is quite common situations in graph databases. For example, to figure out the relationships between v1 and v2/v3, a gremlin query can be written like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q1 = interactive.execute(\n", " 'g.V().has(\"id\", 1).as(\"u\").out().has(\"id\", eq(2).or(eq(3))).as(\"v\").select(\"u\", \"v\").by(\"id\")'\n", ").all().result()\n", "\n", "for p in q1:\n", " print(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example which is popular in social network scenarios, such as finding common features between two different people, one called \"marko\" while another called \"peter\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q2 = interactive.execute(\n", " 'g.V().has(\"name\", \"marko\").out().where(__.in().has(\"name\", \"peter\")).valueMap()'\n", ").all().result()\n", "\n", "for p in q2:\n", " print(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Degree Centrality\n", "Degree centrality is a measure of the number of edges associated to each vertex, which is of statistical significance in large-scale data processing. Here are some examples:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q3 = interactive.execute(\"g.V().as('a').bothE().groupCount().by(select('a'))\").all()\n", "for p in q3:\n", " print(p)\n", "\n", "q4 = interactive.execute(\"g.V().as('a').inE().groupCount().by(select('a'))\").all()\n", "for p in q4:\n", " print(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cycle Detection\n", "Cycle detection is another important application of graph query in commerce area where cycles are usually considered as fraudulent patterns. Here is an example illustrating how gremlin helps detect cycles in a graph." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q5 = interactive.execute(\n", " 'g.V().as(\"u\").out('2..3').with('PATH_OPT', 'SIMPLE').endV().where(out().where(eq(\"u\"))).count()'\n", ")\n", "print(q5.one())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }