{ "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 the Aerospike Database running locally with Java kernel and Aerospike Java Client. To create a Docker container that satisfies the requirements and holds a copy of Aerospike notebooks, visit the [Aerospike Notebooks Repo](https://github.com/aerospike-examples/interactive-notebooks)." ] }, { "cell_type": "markdown", "metadata": { "hide_input": false }, "source": [ "# Use magics to load Aerospike Client from POM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%loadFromPOM\n", "\n", " \n", " com.aerospike\n", " aerospike-client\n", " 5.0.0\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use client to write a record to Aerospike DB and read it back" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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()\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## You can also skip the java boilerplate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "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" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 2 }