From 955369fdd8e0671cacf4fd641bf092d20c54daaa Mon Sep 17 00:00:00 2001 From: Steve Nyemba Date: Wed, 10 Jul 2024 17:33:34 -0500 Subject: [PATCH] aws s3 notebook, brief example --- notebooks/s3.ipynb | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 notebooks/s3.ipynb diff --git a/notebooks/s3.ipynb b/notebooks/s3.ipynb new file mode 100644 index 0000000..1009120 --- /dev/null +++ b/notebooks/s3.ipynb @@ -0,0 +1,131 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Writing to AWS S3\n", + "\n", + "We have setup our demo environment with the label **aws** passed to reference our s3 access_key and secret_key and file. In the cell below we will write the data to our aws s3 bucket named **com.phi.demo**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.1\n" + ] + } + ], + "source": [ + "#\n", + "# Writing to mongodb database\n", + "#\n", + "import transport\n", + "from transport import providers\n", + "import pandas as pd\n", + "_data = pd.DataFrame({\"name\":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})\n", + "mgw = transport.get.writer(label='aws',file='friends.csv',bucket='com.phi.demo')\n", + "mgw.write(_data)\n", + "print (transport.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Reading from AWS S3\n", + "\n", + "The cell below reads the data that has been written by the cell above and computes the average age within a mongodb pipeline. The code in the background executes an aggregation using\n", + "\n", + "- Basic read of the designated file **friends.csv**\n", + "- Compute average age using standard pandas functions\n", + "\n", + "**NOTE**\n", + "\n", + "By design **read** object are separated from **write** objects in order to avoid accidental writes to the database.\n", + "Read objects are created with **transport.get.reader** whereas write objects are created with **transport.get.writer**" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " bname age\n", + "0 James Bond 55\n", + "1 Steve Rogers 150\n", + "2 Steve Nyemba 44\n", + "--------- STATISTICS ------------\n", + "83.0\n" + ] + } + ], + "source": [ + "\n", + "import transport\n", + "from transport import providers\n", + "import pandas as pd\n", + "\n", + "def cast(stream) :\n", + " print (stream)\n", + " return pd.DataFrame(str(stream))\n", + "mgr = transport.get.reader(label='aws', bucket='com.phi.demo',file='friends.csv')\n", + "_df = mgr.read()\n", + "print (_df)\n", + "print ('--------- STATISTICS ------------')\n", + "print (_df.age.mean())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "An **auth-file** is a file that contains database parameters used to access the database. \n", + "For code in shared environments, we recommend \n", + "\n", + "1. Having the **auth-file** stored on disk \n", + "2. and the location of the file is set to an environment variable.\n", + "\n", + "To generate a template of the **auth-file** open the **file generator wizard** found at visit https://healthcareio.the-phi.com/data-transport" + ] + }, + { + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}