{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A Simple Put-Get Example\n", "\n", "A simple example of `put` and `get` calls in Aerospike.\n", "\n", "This notebook requires Aerospike datbase running locally 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": [ "## Import the module\n", "\n", "The Aerospike client must be imported." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import aerospike" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configure the client\n", "\n", "This configuration is for aerospike running on port 3000 of localhost which is the default. If your environment is different (Aerospike server running on a different host or different port, etc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "config = {\n", " 'hosts': [ ('127.0.0.1', 3000) ]\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a client and connect it to the cluster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Records are addressable via a tuple of (namespace, set, key) \n", "\n", "These three components (with set being optionsl) form the key. Using this key records may 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": null, "metadata": {}, "outputs": [], "source": [ "key = ('test', 'demo', 'foo')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing a record\n", "\n", "Aerospike is schema-less and records may be written without any other setup. Here a record with two bins (name and age) is being written to a record with they key defined above. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "try:\n", " # Write a record\n", " client.put(key, {\n", " 'name': 'John Doe',\n", " 'age': 32\n", " })\n", "except Exception as e:\n", " import sys\n", " print(\"error: {0}\".format(e), file=sys.stderr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading a record\n", "\n", "This same record may be retrieved using the same key." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(key, metadata, record) = client.get(key)\n" ] }, { "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, the set, a userkey (by default there is no user key), and a hash which is the internal representation of the key.\n", "1. The metadata with the time to live and the record's generation. \n", "1. The actual value of the record with two bins. \n", "\n", "Lastly it is important to clean up the client we created at the beginning." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"record contents are\", record)\n", "print(\"key components are\", key)\n", "print(\"metadata is\", metadata)\n", "# Close the connection to the Aerospike cluster\n", "client.close()" ] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python 3.7.9 64-bit", "language": "python", "name": "python37964bit728b6d8a91f74e8c9f0db525f58accf3" }, "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.9" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 2 }