{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import redis\n", "import os\n", "from neptune_python_utils.gremlin_utils import GremlinUtils\n", "import ipywidgets as widgets\n", "from IPython.display import display, HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nest_asyncio\n", "nest_asyncio.apply()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create Neptune client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "GremlinUtils.init_statics(globals())\n", "\n", "gremlin = GremlinUtils()\n", "\n", "neptune_connection = gremlin.remote_connection()\n", "g = gremlin.traversal_source(connection=neptune_connection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create ElastiCache client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ec = redis.StrictRedis(host=os.environ['ELASTICACHE_ENDPOINT'], port=6379)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Query ElastiCache and Neptune" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class HtmlTable:\n", " def __init__(self):\n", " self.html = ''\n", " self.empty_row = ''\n", " \n", " def add_header_row(self, cols):\n", " self.html += ''\n", " for col in cols:\n", " self.html += ''.format(col)\n", " self.html += ''\n", " \n", " def add_row(self, cols):\n", " self.empty_row = ''\n", " self.html += ''\n", " for col in cols:\n", " self.html += ''.format(col)\n", " self.html += ''\n", " \n", " def get_html(self):\n", " result = self.html + self.empty_row + '
No results
{}
{}
'\n", " return result\n", " \n", "most_popular = ec.zrevrange('degree_centrality', 0, 2, withscores=True)\n", "\n", "vertex_ids = []\n", "centralities = []\n", "\n", "for (vertex_id, centrality) in most_popular:\n", " vertex_ids.append(vertex_id.decode(\"utf-8\") )\n", " centralities.append(int(centrality))\n", " \n", "users = g.V(vertex_ids).valueMap(True).toList()\n", "\n", "tbl = HtmlTable()\n", "tbl.add_header_row(['First Name', 'Last Name', 'Age', 'Centrality'])\n", "\n", "for (user, centrality) in zip(users, centralities):\n", " tbl.add_row([user['firstname'][0], user['lastname'][0], user['age'][0], centrality])\n", " \n", "display(HTML(tbl.get_html()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Close Neptune client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "neptune_connection.close()" ] } ], "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.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }