{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Aerospike Hello World!\n", "\n", "Hello, World! in Python with Aerospike.\n", "
\n", "This notebook requires Aerospike datbase running on localhost and that python and the Aerospike python client have been installed (`pip install aerospike`). Visit [Aerospike notebooks repo](https://github.com/aerospike-examples/interactive-notebooks) for additional details and the docker container." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ensure database is running\n", "This notebook requires that Aerospike datbase is running." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Aerospike database is running!\r\n" ] } ], "source": [ "!asd >& /dev/null\n", "!pgrep -x asd >/dev/null && echo \"Aerospike database is running!\" || echo \"**Aerospike database is not running!**\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the module\n", "\n", "Import the client library." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Client module imported\n" ] } ], "source": [ "import aerospike\n", "print(\"Client module imported\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configure the client\n", "\n", "The configuration is for Aerospike database running on port 3000 of localhost (IP 127.0.0.1) which is the default. Modify config if your environment is different (Aerospike database running on a different host or different port)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Configuring with seed host: [('127.0.0.1', 3000)]\n" ] } ], "source": [ "config = {\n", " 'hosts': [ ('127.0.0.1', 3000) ]\n", "}\n", "print(\"Configuring with seed host:\", config['hosts'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create client object and connect to the cluster" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to the cluster\n" ] } ], "source": [ "try:\n", " client = aerospike.client(config).connect()\n", "except:\n", " import sys\n", " print(\"Failed to connect to the cluster with\", config['hosts'])\n", " sys.exit(1)\n", "print(\"Connected to the cluster\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Understand records are addressable via a tuple of (namespace, set, userkey) \n", "\n", "The three components namespace, set, and userkey (with set being optional) form the Primary Key (PK) or simply key, of the record. The key serves as a handle to the record, and using it, a record can be read or written. For a detailed description of the data model see the [Data Model overview](https://www.aerospike.com/docs/architecture/data-model.html)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Working with record key ('test', 'demo', 'foo')\n" ] } ], "source": [ "key = ('test', 'demo', 'foo')\n", "print('Working with record key ', key)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Write a record\n", "\n", "Aerospike is schema-less and records may be written without any other setup. Here the bins or fields: name, age and greeting, are being written to a record with the key as defined above. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Successfully written the record\n" ] } ], "source": [ "try:\n", " # Write a record\n", " client.put(key, {\n", " 'name': 'John Doe',\n", " 'age': 32,\n", " 'greeting': 'Hello, World!'\n", " })\n", "except Exception as e:\n", " import sys\n", " print(\"error: {0}\".format(e), file=sys.stderr)\n", " sys.exit(1)\n", "print('Successfully written the record')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read a record\n", "\n", "The record may be retrieved using the same key." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Read back the record\n" ] } ], "source": [ "(key, metadata, record) = client.get(key)\n", "print('Read back the record')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display result\n", "\n", "Print the record that was just retrieved. We are also printing: \n", "\n", "1. The components of the key which are: namespace, set, and userkey. By default userkey is not stored on server, only a hash (appearing as bytearray in the output below) which is the internal representation of the key is stored.\n", "1. The metadata with the time-to-live and the record's generation or version. \n", "1. The actual value of the record's bins. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Record contents are {'name': 'John Doe', 'age': 32, 'gpa': 4.3, 'greeting': 'Hello, World!'}\n", "Key's components are ('test', 'demo', None, bytearray(b'\\xf5~\\xc1\\x835\\xf7\\x10\\x0c\\x04X\\xf8\\xa6D\\xbc\\xbcvm\\x93G\\x1e'))\n", "Metadata is {'ttl': 2592000, 'gen': 2}\n" ] } ], "source": [ "print(\"Record contents are\", record)\n", "print(\"Key's components are\", key)\n", "print(\"Metadata is\", metadata)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clean up\n", "Finally close the client we created at the beginning." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connection closed.\n" ] } ], "source": [ "# Close the connection to the Aerospike cluster\n", "client.close()\n", "print('Connection closed.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Next steps\n", "\n", "Visit [Aerospike notebooks repo](https://github.com/aerospike-examples/interactive-notebooks) to run additional Aerospike notebooks. To run a different notebook, download the notebook from the repo to your local machine, and then click on File->Open, and select Upload.\n" ] } ], "metadata": { "file_extension": ".py", "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.6" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "version": 3 }, "nbformat": 4, "nbformat_minor": 2 }