{ "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 Java with Aerospike.\n", "This notebook requires Aerospike datbase running locally and that Java kernel has been installed. 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": [], "source": [ "import io.github.spencerpark.ijava.IJava;\n", "import io.github.spencerpark.jupyter.kernel.magic.common.Shell;\n", "IJava.getKernelInstance().getMagics().registerMagics(Shell.class);\n", "%sh asd" ] }, { "cell_type": "markdown", "metadata": { "hide_input": false }, "source": [ "## Download Aerospike client from POM" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%loadFromPOM\n", "\n", " \n", " com.aerospike\n", " aerospike-client\n", " 5.0.0\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the modules\n", "\n", "Import the client library and other modules." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Client modules imported.\n" ] } ], "source": [ "import com.aerospike.client.AerospikeClient;\n", "import com.aerospike.client.policy.WritePolicy;\n", "import com.aerospike.client.Bin;\n", "import com.aerospike.client.Key;\n", "import com.aerospike.client.Record;\n", "import com.aerospike.client.Value;\n", "System.out.println(\"Client modules imported.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize the client\n", "\n", "Initialize the client and connect to the cluster. The configuration is for Aerospike database running on port 3000 of localhost which is the default. Modify config if your environment is different (Aerospike database running on a different host or different port).\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialized the client and connected to the cluster.\n" ] } ], "source": [ "AerospikeClient client = new AerospikeClient(\"localhost\", 3000);\n", "System.out.println(\"Initialized the client and 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. By default userkey is not stored on server, only a hash (a byte array, the fourth component in the output below) which is the internal representation of the key is stored. 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:\n", "test:demo:foo:f57ec18335f7100c0458f8a644bcbc766d93471e\n" ] } ], "source": [ "Key key = new Key(\"test\", \"demo\", \"foo\");\n", "System.out.println(\"Working with record key:\");\n", "System.out.println(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": [ "Bin bin1 = new Bin(\"name\", \"John Doe\");\n", "Bin bin2 = new Bin(\"age\", 32);\n", "Bin bin3 = new Bin(\"greeting\", \"Hello World!\");\n", "\n", "// Write a record\n", "client.put(null, key, bin1, bin2, bin3);\n", "System.out.println(\"Successfully written the record.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read a record\n", "\n", "The record can 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": [ "// Read the record\n", "Record record = client.get(null, key);\n", "System.out.println(\"Read back the record.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display result\n", "\n", "Print the record that was just retrieved. We are printing: \n", "\n", "1. The metadata with the record's generation (or version) and expiration time. \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 values are:\n", "(gen:3),(exp:351567215),(bins:(name:John Doe),(age:32),(gpa:4.3),(greeting:Hello World!))\n" ] } ], "source": [ "System.out.println(\"Record values are:\");\n", "System.out.println(record);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clean up\n", "Finally close the client connection." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connection closed.\n" ] } ], "source": [ "client.close(); \n", "System.out.println(\"Connection closed.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## All code in Java boilerplate\n", "All the above code can also be written in the Java boilerplate format and run in a cell." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Record values are:\n", "(gen:1),(exp:351567216),(bins:(bin1:value1),(bin2:value2))\n" ] } ], "source": [ "import com.aerospike.client.AerospikeClient;\n", "import com.aerospike.client.policy.WritePolicy;\n", "import com.aerospike.client.Bin;\n", "import com.aerospike.client.Key;\n", "import com.aerospike.client.Record;\n", "import com.aerospike.client.Value;\n", "\n", "public class Test{\n", " public static void putRecordGetRecord () {\n", " AerospikeClient client = new AerospikeClient(\"localhost\", 3000);\n", "\n", " Key key = new Key(\"test\", \"demo\", \"putgetkey\");\n", " Bin bin1 = new Bin(\"bin1\", \"value1\");\n", " Bin bin2 = new Bin(\"bin2\", \"value2\");\n", "\n", " // Write a record\n", " client.put(null, key, bin1, bin2);\n", "\n", " // Read a record\n", " Record record = client.get(null, key);\n", " client.close(); \n", " System.out.println(\"Record values are:\");\n", " System.out.println(record);\n", " }\n", "}\n", "\n", "Test.putRecordGetRecord()" ] }, { "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." ] } ], "metadata": { "kernelspec": { "display_name": "Java", "language": "java", "name": "java" }, "language_info": { "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", "name": "Java", "pygments_lexer": "java", "version": "11.0.8+10-LTS" }, "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 } }, "nbformat": 4, "nbformat_minor": 2 }