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

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CDT Indexing in Aerospike\n", "This tutorial describes the secondary index and query functionality pertaining to CDTs available in Aerospike Database 6.1+.\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": {}, "source": [ "## Introduction\n", "In this notebook, we will illustrate the CDT indexing and query capabilities in Aerospike Database 6.1+. The detailed description is available in the accompanying blog post [Query JSON Documents Faster (and More) with CDT Indexing](https://developer.aerospike.com/blog/query-json-documents-faster).\n", "\n", "The specific topics covered in this notebook include:\n", "- New CDT indexing functionality\n", "- Code examples\n", " - Non-collection elements\n", " - Collection elements\n", " - JSON documents" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites\n", "This tutorial assumes familiarity with the following topics:\n", "- [Hello World](hello_world.ipynb)\n", "\n", "- [Query JSON Documents Faster (and More) with CDT Indexing](https://developer.aerospike.com/blog/query-json-documents-faster)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ensure database is running\n", "This notebook requires that Aerospike database is running. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-12-29T20:48:49.065421Z", "start_time": "2020-12-29T20:48:49.060897Z" } }, "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": {}, "source": [ "### Download and install additional components.\n", "Install the Java client version 6.1.0 or above that supports the new CDT indexing capabilities." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-12-29T20:48:50.084636Z", "start_time": "2020-12-29T20:48:50.080629Z" } }, "outputs": [], "source": [ "%%loadFromPOM\n", "\n", " \n", " com.aerospike\n", " aerospike-client\n", " 6.1.0\n", " \n", " \n", " com.aerospike\n", " aerospike-document-api\n", " 1.1.3\n", " \n", " \n", " org.apache.commons\n", " commons-io\n", " 1.3.2\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialize Client\n", "Initialize the client. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialized the client and connected to the cluster.\n" ] } ], "source": [ "import com.aerospike.client.AerospikeClient;\n", "\n", "AerospikeClient client = new AerospikeClient(\"localhost\", 3000);\n", "System.out.println(\"Initialized the client and connected to the cluster.\");;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Modules" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "// import all needed modules\n", "import java.util.ArrayList;\n", "import java.util.Arrays;\n", "import java.util.HashMap;\n", "import java.util.List;\n", "import java.util.Map;\n", "\n", "import com.aerospike.client.AerospikeException;\n", "import com.aerospike.client.Key;\n", "import com.aerospike.client.Bin;\n", "import com.aerospike.client.policy.WritePolicy;\n", "import com.aerospike.client.policy.ClientPolicy;\n", "import com.aerospike.client.Record;\n", "import com.aerospike.client.Operation;\n", "import com.aerospike.client.Value;\n", "import com.aerospike.client.Value.ListValue;\n", "import com.aerospike.client.Value.MapValue;\n", "import com.aerospike.client.cdt.ListOperation;\n", "import com.aerospike.client.cdt.ListPolicy;\n", "import com.aerospike.client.cdt.ListOrder;\n", "import com.aerospike.client.cdt.ListWriteFlags;\n", "import com.aerospike.client.cdt.MapOperation;\n", "import com.aerospike.client.cdt.MapPolicy;\n", "import com.aerospike.client.cdt.MapOrder;\n", "import com.aerospike.client.cdt.MapWriteFlags;\n", "import com.aerospike.client.query.Statement;\n", "import com.aerospike.client.query.Filter;\n", "import com.aerospike.client.query.RecordSet;\n", "import com.aerospike.client.Record;\n", "import com.aerospike.client.policy.QueryPolicy;\n", "import com.aerospike.client.exp.Exp;\n", "import com.aerospike.client.query.IndexType;\n", "import com.aerospike.client.query.IndexCollectionType;\n", "import com.aerospike.client.task.IndexTask;\n", "import com.aerospike.client.AerospikeException;\n", "import com.aerospike.client.ResultCode;\n", "import com.aerospike.client.cdt.CTX;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define Constants and Helper Functions\n", "Define constants for the namespaces `test`, set `cdt-indexing`, and helper functions `createIndex`, `dropIndex`, `executeQueryAndPrintResults`, and `truncateTestData`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "String Namespace = \"test\";\n", "String Set = \"cdt-indexing\";\n", "\n", "// convenience function to create an index - essentially a pass-through to the client API\n", "void createIndex(String idxName, String binName, IndexType idxType, \n", " IndexCollectionType colType, CTX... ctx) {\n", " try {\n", " IndexTask task = client.createIndex(null,\n", " Namespace,\n", " Set,\n", " idxName,\n", " binName,\n", " idxType,\n", " colType,\n", " ctx);\n", " task.waitTillComplete(1000, 0);\n", " }\n", " catch (AerospikeException ae) {\n", " if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {\n", " throw ae;\n", " }\n", " } \n", " System.out.format(\"Created index %s on ns=%s set=%s bin=%s.\\n\", \n", " idxName, Namespace, Set, binName);\n", "}\n", "\n", "// convenience function to drop an index - essentially a pass-through to the client API\n", "void dropIndex(String idxName) {\n", " try {\n", " IndexTask task = client.dropIndex(null, Namespace, Set, idxName);\n", " }\n", " catch (AerospikeException ae) {\n", " if (ae.getResultCode() != ResultCode.INDEX_NOTFOUND) {\n", " throw ae;\n", " }\n", " } \n", " System.out.format(\"Dropped index %s.\\n\", idxName);\n", "}\n", "\n", "// convenience function to execute a query using the input filter and print the results\n", "void executeQueryAndPrintResults(Filter filter, String binName) {\n", " Statement stmt = new Statement();\n", " stmt.setNamespace(Namespace);\n", " stmt.setSetName(Set);\n", " stmt.setFilter(filter);\n", " stmt.setBinNames(binName);\n", " RecordSet rs = client.query(null, stmt);\n", " while (rs.next()) {\n", " Key key = rs.getKey();\n", " Record record = rs.getRecord();\n", " System.out.format(\"key=%s bins=%s\\n\", key.userKey, record.bins);\n", " }\n", " //System.out.println();\n", " rs.close();\n", "}\n", "\n", "// convenience function to truncate test data\n", "void truncateTestData() {\n", " try {\n", " client.truncate(null, Namespace, null, null);\n", " }\n", " catch (AerospikeException e) {\n", " // ignore\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Open a Terminal Tab\n", "You can execute shell commands including Aerospike tools like aql to examine data in the terminal tab. Open a terminal tab by selecting File->Open from the notebook menu, and then New->Terminal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CDT Test Data\n", "We will illustrate how to create indexes on CDTs and issues queries using two CDTs - a nested List and a nested Map that are populated in 10 records each. \n", "\n", "1. A nested List in records with user_key (id) 1-10. The list is held in bin list_bin and has the following numeric, string, and List elements.\n", " - Numeric elements: user_key to user_key+4. \n", " - For example, for user_key=1: 1, 2, 3, 4, 5.\n", " - String elements: \"s\"+user_key to \"s\"+(user_key+4). \n", " - For example, for user_key=1: \"s1\", \"s2\", \"s3\", \"s4\", \"s5\"\n", " - Nested List element: holds numeric elements 10\\*user_key+1 to 10\\*user_key+5. \n", " - For example, for user_key=1: [11, 12, 13, 44, 15].\n", " \n", " So, the value in list_bin for the record with user_key=1 looks like: \n", " \n", " `[ 1, \"s1\", 2, \"s2\", 3, \"s3\", 4, \"s4\", 5, \"s5\", [ 11, 12, 13, 14, 15 ] ]`.\n", " \n", " \n", "2. A nested Map in records with user_key (id) 101-110. The map is held in bin map_bin and has the following numeric, string, and List elements. Below, we have used i with a shorthand for i=user_key-100.\n", " - \"oid\": i. \n", " - For example, for i=1: \"oid\": 1.\n", " - \"obj\": a nested map with the following key-value pairs: {\"attr1\": 10\\*i+1, \"attr2\": \"s\"+10\\*i+2, \"subobj\": {\"attr3\": 100\\*i+1, \"attr4\": 100\\*i+2}}. \n", " - For example, for i=1: \"obj\": {\"attr1\": 11, \"attr2\": \"s12\", \"subobj\": {\"attr3\": 101, \"attr4\": 102}}.\n", " - i: [\"s\"+i, \"s\"+i+1]. \n", " - For example, for i=1: 1: [\"s1\", \"s2\"].\n", " \n", " So, the value in map_bin for the record with user_key=101 (or, equivalently, i=1) looks like:\n", " \n", " `\n", " {\n", " \"oid\": 1, \n", " \"obj\": {\"attr1\": 11, \"attr2\": \"s12\", \"subobj\": {\"attr3\": 101, \"attr4\": 102}}, \n", " 1: [\"s1\", \"s2\"]\n", " } `" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Populate Test Data\n", "Execute the cell below to populate the test data described above." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "final String ListBin = \"list_bin\";\n", "final String MapBin = \"map_bin\";\n", "WritePolicy wpolicy = new WritePolicy();\n", "wpolicy.sendKey = true;\n", "\n", "// create the list_bin with \n", "// [i..i+4, \"s\"+i..\"s\"+4, [10*i+1..10*i+5]]\n", "\n", "for (int user_key = 1; user_key <= 10; user_key++) {\n", " Key key = new Key(Namespace, Set, user_key);\n", "\n", " // append integer and string elements\n", " ArrayList list_val = new ArrayList();\n", " for (int n = user_key; n < user_key+5; n++) {\n", " list_val.add(Value.get(n));\n", " list_val.add(Value.get(\"s\"+Integer.toString(n)));\n", " }\n", "\n", " // append the nested list element\n", " ArrayList list_int = new ArrayList();\n", " for (int n=user_key*10+1; n < user_key*10+6; n++) {\n", " list_int.add(n);\n", " }\n", " ListValue list_int_val = new ListValue(list_int);\n", "\n", " // create the list bin\n", " Record record = client.operate(wpolicy, key,\n", " ListOperation.clear(ListBin), \n", " ListOperation.appendItems(ListBin, list_val), \n", " ListOperation.append(ListBin, list_int_val));\n", "}\n", "\n", "// create map bin with \n", "// \"oid\": i,\n", "// \"obj\": {\"attr1\": 10*i+1, \"attr2\": \"s\"+10*+i+2, \"subobj\": {\"attr3\": 100*1+1, \"attr4\": 100*1+2}}, \n", "// i: [\"s\"+i, \"s\"+i+1]\n", "\n", "for (int user_key = 101; user_key <= 110; user_key++) {\n", " Integer i = user_key - 100;\n", " \n", " Key key = new Key(Namespace, Set, user_key);\n", " \n", " // construct obj map value\n", " HashMap obj = new HashMap ();\n", " obj.put(\"attr1\", Value.get(10*i+1)); \n", " obj.put(\"attr2\", Value.get(\"s\"+Integer.toString(10*i+2))); \n", " HashMap subobj = new HashMap ();\n", " subobj.put(\"attr3\", 100*i+1); \n", " subobj.put(\"attr4\", 100*i+2); \n", " obj.put(\"subobj\", new MapValue(subobj)); \n", "\n", " // construct arr list value\n", " ArrayList arr = new ArrayList();\n", " arr.add(\"s\"+Integer.toString(i*10+1));\n", " arr.add(\"s\"+Integer.toString(i*10+2));\n", " \n", " // create the map in map_bin\n", " MapPolicy mPolicy = new MapPolicy(MapOrder.UNORDERED, MapWriteFlags.DEFAULT);\n", " Record record = client.operate(wpolicy, key,\n", " MapOperation.clear(MapBin), \n", " MapOperation.put(mPolicy, MapBin, \n", " Value.get(\"oid\"), Value.get(i)),\n", " MapOperation.put(mPolicy, MapBin, \n", " Value.get(\"obj\"), new MapValue(obj)),\n", " MapOperation.put(mPolicy, MapBin, \n", " Value.get(i), new ListValue(arr))\n", " ); \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examine Test Data\n", "View the test data by executing the following commands in a Jupyter terminal tab.\n", "\n", "`\n", "aql -c \"select list_bin from test.cdt-indexing\"\n", "`\n", "\n", "`\n", "aql -c \"select map_bin from test.cdt-indexing\"\n", "`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Non-Collection Indexes\n", "We will illustrate how indexes on non-collection elements are created and used. We will start with results in mind, and then create the appropriate index, issue a query using the index, and show the results." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Equality queries\n", "Get records with a specific integer or string value at a specific index, rank, or key position of a List or a Map." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with a specific value at index X of a List or Map\n", "#### The top level List in list_bin has 3 at position 2 (0-indexed)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_list_bin_top_pos_2_num on ns=test set=cdt-indexing bin=list_bin.\n", "Records with the value 3 at position 2 of the top List in list_bin:\n", "key=2 bins={list_bin=[2, s2, 3, s3, 4, s4, 5, s5, 6, s6, [21, 22, 23, 24, 25]]}\n" ] } ], "source": [ "// Create an index for element at position 2 of the top List in list_bin. \n", "// idx_list_bin_top_pos_2_num\n", "createIndex(\"idx_list_bin_top_pos_2_num\", ListBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.listIndex(2));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value 3 at position 2 of the top List in list_bin:\");\n", "Filter filter = Filter.equal(ListBin, 3, CTX.listIndex(2));\n", "executeQueryAndPrintResults(filter, ListBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The top level Map in map_bin has 7 at position 2 " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_top_pos_2_num on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value 7 at position 2 of the top Map in map_bin:\n", "key=107 bins={map_bin={obj={attr2=s72, attr1=71, subobj={attr4=702, attr3=701}}, oid=7, 7=[s71, s72]}}\n" ] } ], "source": [ "// Create an index for element at position 2 of the top Map in map_bin. Note, in the key ordered sequence, \n", "// \"oid\" element is in position 2 after the numeric key and \"obj\".\n", "// idx_map_bin_top_pos_2_num\n", "createIndex(\"idx_map_bin_top_pos_2_num\", MapBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.mapIndex(2));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value 7 at position 2 of the top Map in map_bin:\");\n", "Filter filter = Filter.equal(MapBin, 7, CTX.mapIndex(2));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with a specific value at rank X of a List or Map \n", "#### The nested List in list_bin has 35 in rank -1 (highest value)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_list_bin_nested_rnk_-1_num on ns=test set=cdt-indexing bin=list_bin.\n", "Records with the value 35 in rank -1 (highest value) in the nested List in list_bin:\n", "key=3 bins={list_bin=[3, s3, 4, s4, 5, s5, 6, s6, 7, s7, [31, 32, 33, 34, 35]]}\n" ] } ], "source": [ "// Create an index for element at rank -1 of the nested List in list_bin. Note the nested List is at the \n", "// last poistion (index -1) in the top List.\n", "// idx_list_bin_nested_rnk_-1_num\n", "createIndex(\"idx_list_bin_nested_rnk_-1_num\", ListBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.listIndex(-1), CTX.listRank(-1));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value 35 in rank -1 (highest value) in the nested List in list_bin:\");\n", "Filter filter = Filter.equal(ListBin, 35, CTX.listIndex(-1), CTX.listRank(-1));\n", "executeQueryAndPrintResults(filter, ListBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The nested \"subobj\" Map in map_bin has 901 in rank 0 (lowest value)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_subobj_rnk_0_num on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value 901 in rank 0 (lowest value) in the nested \"subobj\" Map in map_bin:\n", "key=109 bins={map_bin={9=[s91, s92], obj={attr2=s92, attr1=91, subobj={attr4=902, attr3=901}}, oid=9}}\n" ] } ], "source": [ "// Create an index for element at rank 0 (lowest value) in the nested \"subobj\" Map in map_bin. \n", "// idx_map_bin_subobj_rnk_0_num\n", "createIndex(\"idx_map_bin_subobj_rnk_0_num\", MapBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"subobj\")), CTX.mapRank(0));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value 901 in rank 0 (lowest value) in the nested \\\"subobj\\\" Map in map_bin:\");\n", "Filter filter = Filter.equal(MapBin, 901, \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"subobj\")), CTX.mapRank(0));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with a specific value at key X of a Map\n", "#### The top level Map in map_bin has 8 at key \"oid\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_top_key_oid_num on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value 8 at key \"oid\" in the top level Map in map_bin:\n", "key=108 bins={map_bin={8=[s81, s82], obj={attr2=s82, attr1=81, subobj={attr4=802, attr3=801}}, oid=8}}\n" ] } ], "source": [ "// Create an index for element with key \"oid\" in the top level Map in map_bin. \n", "// idx_map_bin_top_key_oid_num\n", "createIndex(\"idx_map_bin_top_key_oid_num\", MapBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"oid\")));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value 8 at key \\\"oid\\\" in the top level Map in map_bin:\");\n", "Filter filter = Filter.equal(MapBin, 8, \n", " CTX.mapKey(Value.get(\"oid\")));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The nested \"obj\" Map in map_bin has \"s42\" at key \"attr2\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_obj_key_attr2_str on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value \"s42\" at key \"attr2\" in the nested \"obj\" Map in map_bin:\n", "key=104 bins={map_bin={4=[s41, s42], obj={attr2=s42, attr1=41, subobj={attr4=402, attr3=401}}, oid=4}}\n" ] } ], "source": [ "// Create an index for element with key \"attr2\" in the nested \"obj\" Map in map_bin. \n", "// idx_map_bin_obj_key_attr2_str\n", "createIndex(\"idx_map_bin_obj_key_attr2_str\", MapBin, IndexType.STRING, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"attr2\")));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value \\\"s42\\\" at key \\\"attr2\\\" in the nested \\\"obj\\\" Map in map_bin:\");\n", "Filter filter = Filter.equal(MapBin, \"s42\", \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"attr2\")));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Range Queries\n", "Get records having an integer value within a range at a specific index, rank, or key position of a List or a Map. Range queries are supported on integer values only.\n", "\n", "We will use the indexes defined above for the range queries too." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with value in a specific range at index X of a List or Map\n", "#### The top level List in list_bin is in range 3-5 at position 2 (0-indexed)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with value in range 3-5 at position 2 in the top level List in list_bin:\n", "key=3 bins={list_bin=[3, s3, 4, s4, 5, s5, 6, s6, 7, s7, [31, 32, 33, 34, 35]]}\n", "key=2 bins={list_bin=[2, s2, 3, s3, 4, s4, 5, s5, 6, s6, [21, 22, 23, 24, 25]]}\n", "key=4 bins={list_bin=[4, s4, 5, s5, 6, s6, 7, s7, 8, s8, [41, 42, 43, 44, 45]]}\n" ] } ], "source": [ "// issue the query and print results\n", "System.out.println(\"Records with value in range 3-5 at position 2 in the top level List in list_bin:\");\n", "Filter filter = Filter.range(ListBin, 3, 5, CTX.listIndex(2));\n", "executeQueryAndPrintResults(filter, ListBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The top level Map in map_bin is in range 5-7 at position 2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with value in range 5-7 at position 2 of the top Map in map_bin:\n", "key=107 bins={map_bin={obj={attr2=s72, attr1=71, subobj={attr4=702, attr3=701}}, oid=7, 7=[s71, s72]}}\n", "key=105 bins={map_bin={5=[s51, s52], obj={attr2=s52, attr1=51, subobj={attr4=502, attr3=501}}, oid=5}}\n", "key=106 bins={map_bin={obj={attr2=s62, attr1=61, subobj={attr4=602, attr3=601}}, 6=[s61, s62], oid=6}}\n" ] } ], "source": [ "// issue the query and print results\n", "System.out.println(\"Records with value in range 5-7 at position 2 of the top Map in map_bin:\");\n", "Filter filter = Filter.range(MapBin, 5, 7, CTX.mapIndex(2));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with value in a specific range at rank X of a List or Map\n", "- The nested List in list_bin is in range 20-50 at rank -1 (highest value)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with value in range 20-50 in rank -1 (highest value) in the nested List in list_bin:\n", "key=3 bins={list_bin=[3, s3, 4, s4, 5, s5, 6, s6, 7, s7, [31, 32, 33, 34, 35]]}\n", "key=2 bins={list_bin=[2, s2, 3, s3, 4, s4, 5, s5, 6, s6, [21, 22, 23, 24, 25]]}\n", "key=4 bins={list_bin=[4, s4, 5, s5, 6, s6, 7, s7, 8, s8, [41, 42, 43, 44, 45]]}\n" ] } ], "source": [ "// issue the query and print results\n", "System.out.println(\"Records with value in range 20-50 in rank -1 (highest value) in the nested List in list_bin:\");\n", "Filter filter = Filter.range(ListBin, 20, 50, CTX.listIndex(-1), CTX.listRank(-1));\n", "executeQueryAndPrintResults(filter, ListBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The nested \"subobj\" Map in map_bin is in range 500-800 at rank 0 (lowest value)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with value in range 500-800 in rank 0 (lowest value) in the nested \"subobj\" Map in map_bin:\n", "key=107 bins={map_bin={obj={attr2=s72, attr1=71, subobj={attr4=702, attr3=701}}, oid=7, 7=[s71, s72]}}\n", "key=105 bins={map_bin={5=[s51, s52], obj={attr2=s52, attr1=51, subobj={attr4=502, attr3=501}}, oid=5}}\n", "key=106 bins={map_bin={obj={attr2=s62, attr1=61, subobj={attr4=602, attr3=601}}, 6=[s61, s62], oid=6}}\n" ] } ], "source": [ "// issue the query and print results\n", "System.out.println(\"Records with value in range 500-800 in rank 0 (lowest value) in the nested \\\"subobj\\\" Map in map_bin:\");\n", "Filter filter = Filter.range(MapBin, 500, 800,\n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"subobj\")), CTX.mapRank(0));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with value in a specific range at key X of a Map\n", "#### The top level Map in map_bin has a value in range 4-6 at key \"oid\"" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with value in range 4-6 at key \"oid\" in the top level Map in map_bin:\n", "key=105 bins={map_bin={5=[s51, s52], obj={attr2=s52, attr1=51, subobj={attr4=502, attr3=501}}, oid=5}}\n", "key=104 bins={map_bin={4=[s41, s42], obj={attr2=s42, attr1=41, subobj={attr4=402, attr3=401}}, oid=4}}\n", "key=106 bins={map_bin={obj={attr2=s62, attr1=61, subobj={attr4=602, attr3=601}}, 6=[s61, s62], oid=6}}\n" ] } ], "source": [ "// issue the query and print results\n", "System.out.println(\"Records with value in range 4-6 at key \\\"oid\\\" in the top level Map in map_bin:\");\n", "Filter filter = Filter.range(MapBin, 4, 6, CTX.mapKey(Value.get(\"oid\")));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Collection Indexes\n", "Collection indexes are defined on List and Map elements with the collection type LIST (list values in the List), MAPKEYS (key values in the Map), or MAPVALUES (values in the Map). The indexes can be used for equality queries on integr and string elements, as well as range queries on integer elements. We will illustrate these variations below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Equality Queries\n", "Get records with a specific integer or string value in a List or Map." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with a List or Map containing a specific value.\n", "#### The nested List in map_bin contains \"s91\". " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_i_list_str on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value \"s91\" in the nested list \"arr\" in map_bin:\n", "key=109 bins={map_bin={9=[s91, s92], obj={attr2=s92, attr1=91, subobj={attr4=902, attr3=901}}, oid=9}}\n" ] } ], "source": [ "// Create an index for the nested List in map_bin. Note, in key-ordered sequence, the numeric key is the first one.\n", "// idx_map_bin_i_list_str\n", "createIndex(\"idx_map_bin_i_list_str\", MapBin, IndexType.STRING, IndexCollectionType.LIST, \n", " CTX.mapIndex(0));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value \\\"s91\\\" in the nested list \\\"arr\\\" in map_bin:\");\n", "Filter filter = Filter.contains(MapBin, IndexCollectionType.LIST, \"s91\", CTX.mapIndex(0));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The top level Map in map_bin contains a key 5." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_top_mapkeys_num on ns=test set=cdt-indexing bin=map_bin.\n", "Records with key 5 in the top level Map in map_bin:\n", "key=105 bins={map_bin={5=[s51, s52], obj={attr2=s52, attr1=51, subobj={attr4=502, attr3=501}}, oid=5}}\n" ] } ], "source": [ "// Create an index for keys in top level Map in map_bin. \n", "// idx_map_bin_top_mapkeys_num\n", "createIndex(\"idx_map_bin_top_mapkeys_num\", MapBin, IndexType.NUMERIC, IndexCollectionType.MAPKEYS);\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with key 5 in the top level Map in map_bin:\");\n", "Filter filter = Filter.contains(MapBin, IndexCollectionType.MAPKEYS, 5);\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The nested \"obj\" Map in map_bin contains a value \"s12\". " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_obj_mapvals_str on ns=test set=cdt-indexing bin=map_bin.\n", "Records with the value \"s12\" in the nested Map \"obj\" in map_bin:\n", "key=101 bins={map_bin={1=[s11, s12], obj={attr2=s12, attr1=11, subobj={attr4=102, attr3=101}}, oid=1}}\n" ] } ], "source": [ "// Create an index for values in nested Map \"obj\" in map_bin. \n", "// idx_map_bin_obj_mapvals_str\n", "createIndex(\"idx_map_bin_obj_mapvals_str\", MapBin, IndexType.STRING, IndexCollectionType.MAPVALUES, \n", " CTX.mapKey(Value.get(\"obj\")));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with the value \\\"s12\\\" in the nested Map \\\"obj\\\" in map_bin:\");\n", "Filter filter = Filter.contains(MapBin, IndexCollectionType.MAPVALUES, \"s12\", CTX.mapKey(Value.get(\"obj\")));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Range Queries\n", "Get records with a range of integer values in a List or Map." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Records with a List or Map containing an integer in a given range.\n", "#### The nested list in list_bin has a value in range 85-93." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_list_bin_nested_list_num on ns=test set=cdt-indexing bin=list_bin.\n", "Records with value in range 85-93 in the nested list in list_bin:\n", "key=8 bins={list_bin=[8, s8, 9, s9, 10, s10, 11, s11, 12, s12, [81, 82, 83, 84, 85]]}\n", "key=9 bins={list_bin=[9, s9, 10, s10, 11, s11, 12, s12, 13, s13, [91, 92, 93, 94, 95]]}\n" ] } ], "source": [ "// Create an index for the nested List in map_bin. Note, the nested list is the last element.\n", "// idx_list_bin_nested_list_num\n", "createIndex(\"idx_list_bin_nested_list_num\", ListBin, IndexType.NUMERIC, IndexCollectionType.LIST, \n", " CTX.listIndex(-1));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with value in range 85-93 in the nested list in list_bin:\");\n", "Filter filter = Filter.range(ListBin, IndexCollectionType.LIST, 85, 93, CTX.listIndex(-1));\n", "executeQueryAndPrintResults(filter, ListBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The top level Map in map_bin has a key in range 5-7." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Records with key in range 5-7 in the top level Map in map_bin:\n", "key=107 bins={map_bin={obj={attr2=s72, attr1=71, subobj={attr4=702, attr3=701}}, oid=7, 7=[s71, s72]}}\n", "key=105 bins={map_bin={5=[s51, s52], obj={attr2=s52, attr1=51, subobj={attr4=502, attr3=501}}, oid=5}}\n", "key=106 bins={map_bin={obj={attr2=s62, attr1=61, subobj={attr4=602, attr3=601}}, 6=[s61, s62], oid=6}}\n" ] } ], "source": [ "// use the index created earlier\n", "// issue the query and print results\n", "System.out.println(\"Records with key in range 5-7 in the top level Map in map_bin:\");\n", "Filter filter = Filter.range(MapBin, IndexCollectionType.MAPKEYS, 5, 7);\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The nested Map \"subobj\" in map_bin has a value in range 300-500." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_map_bin_subobj_mapvals_num on ns=test set=cdt-indexing bin=map_bin.\n", "Records with value in range 300-500 in the nested Map \"subobj\" in map_bin:\n", "key=103 bins={map_bin={obj={attr2=s32, attr1=31, subobj={attr4=302, attr3=301}}, oid=3, 3=[s31, s32]}}\n", "key=104 bins={map_bin={4=[s41, s42], obj={attr2=s42, attr1=41, subobj={attr4=402, attr3=401}}, oid=4}}\n" ] } ], "source": [ "// Create an index for values in nested Map \"subobj\" in map_bin. \n", "// idx_map_bin_subobj_mapvals_num\n", "createIndex(\"idx_map_bin_subobj_mapvals_num\", MapBin, IndexType.NUMERIC, IndexCollectionType.MAPVALUES, \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"subobj\")));\n", "\n", "// issue the query and print results\n", "System.out.println(\"Records with value in range 300-500 in the nested Map \\\"subobj\\\" in map_bin:\");\n", "Filter filter = Filter.range(MapBin, IndexCollectionType.MAPVALUES, 300, 500, \n", " CTX.mapKey(Value.get(\"obj\")), CTX.mapKey(Value.get(\"subobj\")));\n", "executeQueryAndPrintResults(filter, MapBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Indexing JSON Documents\n", "We will illustrate how JSON documents can be indexed and accessed using CDT indexing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize Document Client" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initialized document client from the Aerospike client.\n" ] } ], "source": [ "// Import modules\n", "import org.apache.commons.io.FileUtils;\n", "import com.fasterxml.jackson.databind.JsonNode;\n", "import com.aerospike.documentapi.*;\n", "\n", "// Initialize Document client from Aerospike client\n", "try {\n", " client.close();\n", "}\n", "catch (Exception ae) {\n", " //ignore\n", "}\n", "ClientPolicy cPolicy = new ClientPolicy();\n", "cPolicy.writePolicyDefault.sendKey = true;\n", "AerospikeClient client = new AerospikeClient(cPolicy, \"localhost\", 3000);\n", "AerospikeDocumentClient documentClient = new AerospikeDocumentClient(client);\n", "System.out.println(\"Initialized document client from the Aerospike client.\");;" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "String Namespace = \"test\";\n", "String Set = \"cdt-indexing\";\n", "\n", "// convenience function to create an index - essentially a pass-through to the client API\n", "void createIndex(String idxName, String binName, IndexType idxType, \n", " IndexCollectionType colType, CTX... ctx) {\n", " try {\n", " IndexTask task = client.createIndex(null,\n", " Namespace,\n", " Set,\n", " idxName,\n", " binName,\n", " idxType,\n", " colType,\n", " ctx);\n", " task.waitTillComplete(1000, 0);\n", " }\n", " catch (AerospikeException ae) {\n", " if (ae.getResultCode() != ResultCode.INDEX_ALREADY_EXISTS) {\n", " throw ae;\n", " }\n", " } \n", " System.out.format(\"Created index %s on ns=%s set=%s bin=%s.\\n\", \n", " idxName, Namespace, Set, binName);\n", "}\n", "\n", "// convenience function to drop an index - essentially a pass-through to the client API\n", "void dropIndex(String idxName) {\n", " try {\n", " IndexTask task = client.dropIndex(null, Namespace, Set, idxName);\n", " }\n", " catch (AerospikeException ae) {\n", " if (ae.getResultCode() != ResultCode.INDEX_NOTFOUND) {\n", " throw ae;\n", " }\n", " } \n", " System.out.format(\"Dropped index %s.\\n\", idxName);\n", "}\n", "\n", "// convenience function to execute a query using the input filter and print the results\n", "void executeQueryAndPrintResults(Filter filter, String binName) {\n", " Statement stmt = new Statement();\n", " stmt.setNamespace(Namespace);\n", " stmt.setSetName(Set);\n", " stmt.setFilter(filter);\n", " stmt.setBinNames(binName);\n", " RecordSet rs = client.query(null, stmt);\n", " while (rs.next()) {\n", " Key key = rs.getKey();\n", " System.out.format(\"key=%s\\n\", key.userKey);\n", " Record record = rs.getRecord();\n", " //System.out.format(\"key=%s bins=%s\\n\", key.userKey, record.bins);\n", "\t\tMap jsonMap = (Map) record.getValue(JsonBin);\n", " for (String field: jsonMap.keySet()) {\n", " System.out.printf(\"\\t%s=%.200s\\n\", field, jsonMap.get(field));\n", " }\n", " }\n", " //System.out.println();\n", " rs.close();\n", "}\n", "\n", "// convenience function to truncate test data\n", "void truncateTestData() {\n", " try {\n", " client.truncate(null, Namespace, null, null);\n", " }\n", " catch (AerospikeException e) {\n", " // ignore\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1: Nobel Prizes JSON Dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read JSON File and Populate Database\n", "We will load JSON documents from the file \"nobel_prizes.json\", and store each prize entry in the database in a separate record with user_key starting at 1000 in bin \"json_bin\". You can view the file's contents by opening it from Jupyter's file browser." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Read JSON doc from: /home/jovyan/notebooks/java/nobel_prizes.json\n", "Example prize schema: \n", "{\n", " \"year\" : \"2021\",\n", " \"category\" : \"chemistry\",\n", " \"laureates\" : [ {\n", " \"id\" : \"1002\",\n", " \"firstname\" : \"Benjamin\",\n", " \"surname\" : \"List\",\n", " \"motivation\" : \"\\\"for the development of asymmetric organocatalysis\\\"\",\n", " \"share\" : \"2\"\n", " }, {\n", " \"id\" : \"1003\",\n", " \"firstname\" : \"David\",\n", " \"surname\" : \"MacMillan\",\n", " \"motivation\" : \"\\\"for the development of asymmetric organocatalysis\\\"\",\n", " \"share\" : \"2\"\n", " } ]\n", "} \n", "\n", "Stored 658 JSON documents in database \n" ] } ], "source": [ "String JsonFilePath = \"/home/jovyan/notebooks/java/nobel_prizes.json\";\n", "String JsonBin = \"json_bin\";\n", "Integer UserKeyOffset = 1000;\n", "\n", "// Read the json document into a string.\n", "String jsonString = FileUtils.readFileToString(new File(JsonFilePath));\n", "System.out.format(\"Read JSON doc from: %s\\n\", JsonFilePath);;\n", "\n", "// Convert JSON string to a JsonNode\n", "JsonNode rootNode = JsonConverters.convertStringToJsonNode(jsonString);\n", "\n", "// Add the prizes in JSON in separate records\n", "JsonNode prizesNode = rootNode.path(\"prizes\");\n", "\n", "Integer prizeIndex = 0;\n", "for (JsonNode prizeNode : prizesNode) {\n", " if (prizeIndex == 0) {\n", " System.out.format(\"Example prize schema: \\n%s \\n\\n\", prizeNode.toPrettyString());\n", " }\n", " Key jsonDocKey = new Key(Namespace, Set, UserKeyOffset+prizeIndex+1); \n", " documentClient.put(jsonDocKey, JsonBin, prizeNode); \n", " prizeIndex++;\n", "}\n", "System.out.format(\"Stored %d JSON documents in database \\n\", prizeIndex);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute Query with Index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find nobel prizes in a given year." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_json_bin_year_str on ns=test set=cdt-indexing bin=json_bin.\n", "Nobel prizes in year 1969:\n", "key=1314\n", "\tcategory=economics\n", "\tyear=1969\n", "\tlaureates=[{firstname=Ragnar, share=2, id=677, surname=Frisch, motivation=\"for having developed and applied dynamic models for the analysis of economic processes\"}, {firstname=Jan, share=2, id=678, surname=Tinb\n", "key=1313\n", "\tcategory=chemistry\n", "\tyear=1969\n", "\tlaureates=[{firstname=Derek, share=2, id=237, surname=Barton, motivation=\"for their contributions to the development of the concept of conformation and its application in chemistry\"}, {firstname=Odd, share=2, i\n", "key=1315\n", "\tcategory=literature\n", "\tyear=1969\n", "\tlaureates=[{firstname=Samuel, share=1, id=643, surname=Beckett, motivation=\"for his writing, which - in new forms for the novel and drama - in the destitution of modern man acquires its elevation\"}]\n", "key=1318\n", "\tcategory=medicine\n", "\tyear=1969\n", "\tlaureates=[{firstname=Max, share=3, id=391, surname=Delbrück, motivation=\"for their discoveries concerning the replication mechanism and the genetic structure of viruses\"}, {firstname=Alfred D., share=3, id=392\n", "key=1317\n", "\tcategory=physics\n", "\tyear=1969\n", "\tlaureates=[{firstname=Murray, share=1, id=90, surname=Gell-Mann, motivation=\"for his contributions and discoveries concerning the classification of elementary particles and their interactions\"}]\n", "key=1316\n", "\tcategory=peace\n", "\tyear=1969\n", "\tlaureates=[{share=1, firstname=International Labour Organization, id=527, motivation=\"for creating international legislation insuring certain norms for working conditions in every country\"}]\n" ] } ], "source": [ "// Create an index on the year field.\n", "// idx_json_bin_year_str\n", "createIndex(\"idx_json_bin_year_str\", JsonBin, IndexType.STRING, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"year\")));\n", "\n", "// Find nobel prizes in year 1969.\n", "System.out.println(\"Nobel prizes in year 1969:\");\n", "Filter filter = Filter.equal(JsonBin, \"1969\", CTX.mapKey(Value.get(\"year\")));\n", "executeQueryAndPrintResults(filter, JsonBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find nobel prizes in a given category." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_json_bin_category_str on ns=test set=cdt-indexing bin=json_bin.\n", "Nobel peace prizes:\n", "key=1190\n", "\tcategory=peace\n", "\tyear=1990\n", "\tlaureates=[{firstname=Mikhail, share=1, id=552, surname=Gorbachev, motivation=\"for the leading role he played in the radical changes in East-West relations\"}]\n", "key=1561\n", "\tcategory=peace\n", "\tyear=1920\n", "\tlaureates=[{firstname=Léon, share=1, id=484, surname=Bourgeois, motivation=\"for his longstanding contribution to the cause of peace and justice and his prominent role in the establishment of the League of Natio\n", "key=1401\n", "\tcategory=peace\n", "\tyear=1952\n", "\tlaureates=[{firstname=Albert, share=1, id=513, surname=Schweitzer, motivation=\"for his altruism, reverence for life, and tireless humanitarian work which has helped making the idea of brotherhood between men an\n", "key=1386\n", "\tcategory=peace\n", "\tyear=1955\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1591\n", "\tcategory=peace\n", "\tyear=1914\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1646\n", "\tcategory=peace\n", "\tyear=1903\n", "\tlaureates=[{firstname=Randal, share=1, id=466, surname=Cremer, motivation=\"for his longstanding and devoted effort in favour of the ideas of peace and arbitration\"}]\n", "key=1034\n", "\tcategory=peace\n", "\tyear=2016\n", "\tlaureates=[{firstname=Juan Manuel, share=1, id=934, surname=Santos, motivation=\"for his resolute efforts to bring the country's more than 50-year-long civil war to an end\"}]\n", "key=1426\n", "\tcategory=peace\n", "\tyear=1947\n", "\tlaureates=[{share=2, firstname=Friends Service Council, id=508, motivation=\"for their pioneering work in the international peace movement and compassionate effort to relieve human suffering, thereby promoting t\n", "key=1471\n", "\tcategory=peace\n", "\tyear=1938\n", "\tlaureates=[{share=1, firstname=Nansen International Office for Refugees, id=503, motivation=\"for having carried on the work of Fridtjof Nansen to the benefit of refugees across Europe\"}]\n", "key=1606\n", "\tcategory=peace\n", "\tyear=1911\n", "\tlaureates=[{firstname=Tobias, share=2, id=478, surname=Asser, motivation=\"for his role as co-founder of the Institut de droit international, initiator of the Conferences on International Private Law (Conférence\n", "key=1130\n", "\tcategory=peace\n", "\tyear=2000\n", "\tlaureates=[{firstname=Kim, share=1, id=725, surname=Dae-jung, motivation=\"for his work for democracy and human rights in South Korea and in East Asia in general, and for peace and reconciliation with North Kore\n", "key=1506\n", "\tcategory=peace\n", "\tyear=1931\n", "\tlaureates=[{firstname=Jane, share=2, id=496, surname=Addams, motivation=\"for their assiduous effort to revive the ideal of peace and to rekindle the spirit of peace in their own nation and in the whole of manki\n", "key=1601\n", "\tcategory=peace\n", "\tyear=1912\n", "\tlaureates=[{firstname=Elihu, share=1, id=480, surname=Root, motivation=\"for bringing about better understanding between the countries of North and South America and initiating important arbitration agreements b\n", "key=1154\n", "\tcategory=peace\n", "\tyear=1996\n", "\tlaureates=[{firstname=Carlos Filipe Ximenes, share=2, id=562, surname=Belo, motivation=\"for their work towards a just and peaceful solution to the conflict in East Timor\"}, {firstname=José, share=2, id=563, sur\n", "key=1244\n", "\tcategory=peace\n", "\tyear=1981\n", "\tlaureates=[{share=1, firstname=Office of the United Nations High Commissioner for Refugees, id=515, motivation=\"for promoting the fundamental rights of refugees\"}]\n", "key=1466\n", "\tcategory=peace\n", "\tyear=1939\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1621\n", "\tcategory=peace\n", "\tyear=1908\n", "\tlaureates=[{firstname=Klas Pontus, share=2, id=473, surname=Arnoldson, motivation=\"for their long time work for the cause of peace as politicians, peace society leaders, orators and authors\"}, {firstname=Fredri\n", "key=1636\n", "\tcategory=peace\n", "\tyear=1905\n", "\tlaureates=[{firstname=Bertha, share=1, id=468, surname=von Suttner, motivation=\"for her audacity to oppose the horrors of war\"}]\n", "key=1280\n", "\tcategory=peace\n", "\tyear=1975\n", "\tlaureates=[{firstname=Andrei, share=1, id=534, surname=Sakharov, motivation=\"for his struggle for human rights in the Soviet Union, for disarmament and cooperation between all nations\"}]\n", "key=1321\n", "\tcategory=peace\n", "\tyear=1968\n", "\tlaureates=[{firstname=René, share=1, id=526, surname=Cassin, motivation=\"for his struggle to ensure the rights of man as stipulated in the UN Declaration\"}]\n", "key=1411\n", "\tcategory=peace\n", "\tyear=1950\n", "\tlaureates=[{firstname=Ralph, share=1, id=511, surname=Bunche, motivation=\"for his work as mediator in Palestine in 1948-1949\"}]\n", "key=1220\n", "\tcategory=peace\n", "\tyear=1985\n", "\tlaureates=[{share=1, firstname=International Physicians for the Prevention of Nuclear War, id=547, motivation=\"for spreading authoritative information and by creating awareness of the catastrophic consequences \n", "key=1304\n", "\tcategory=peace\n", "\tyear=1971\n", "\tlaureates=[{firstname=Willy, share=1, id=529, surname=Brandt, motivation=\"for paving the way for a meaningful dialogue between East and West\"}]\n", "key=1461\n", "\tcategory=peace\n", "\tyear=1940\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1491\n", "\tcategory=peace\n", "\tyear=1934\n", "\tlaureates=[{firstname=Arthur, share=1, id=499, surname=Henderson, motivation=\"for his untiring struggle and his courageous efforts as Chairman of the League of Nations Disarmament Conference 1931-34\"}]\n", "key=1256\n", "\tcategory=peace\n", "\tyear=1979\n", "\tlaureates=[{firstname=Anjezë Gonxhe, share=1, id=540, surname=Bojaxhiu, motivation=\"for her work for bringing help to suffering humanity\"}]\n", "key=1391\n", "\tcategory=peace\n", "\tyear=1954\n", "\tlaureates=[{share=1, firstname=Office of the United Nations High Commissioner for Refugees, id=515, motivation=\"for its efforts to heal the wounds of war by providing help and protection to refugees all over th\n", "key=1250\n", "\tcategory=peace\n", "\tyear=1980\n", "\tlaureates=[{firstname=Adolfo, share=1, id=541, surname=Pérez Esquivel, motivation=\"for being a source of inspiration to repressed people, especially in Latin America\"}]\n", "key=1486\n", "\tcategory=peace\n", "\tyear=1935\n", "\tlaureates=[{firstname=Carl, share=1, id=500, surname=von Ossietzky, motivation=\"for his burning love for freedom of thought and expression and his valuable contribution to the cause of peace\"}]\n", "key=1536\n", "\tcategory=peace\n", "\tyear=1925\n", "\tlaureates=[{firstname=Sir Austen, share=2, id=488, surname=Chamberlain, motivation=\"for his crucial role in bringing about the Locarno Treaty\"}, {firstname=Charles G., share=2, id=489, surname=Dawes, motivation\n", "key=1521\n", "\tcategory=peace\n", "\tyear=1928\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1268\n", "\tcategory=peace\n", "\tyear=1977\n", "\tlaureates=[{share=1, firstname=Amnesty International, id=537, motivation=\"for worldwide respect for human rights\"}]\n", "key=1010\n", "\tcategory=peace\n", "\tyear=2020\n", "\tlaureates=[{share=1, firstname=World Food Programme, id=994, motivation=\"for its efforts to combat hunger, for its contribution to bettering conditions for peace in conflict-affected areas and for acting as a d\n", "key=1226\n", "\tcategory=peace\n", "\tyear=1984\n", "\tlaureates=[{firstname=Desmond, share=1, id=546, surname=Tutu, motivation=\"for his role as a unifying leader figure in the non-violent campaign to resolve the problem of apartheid in South Africa\"}]\n", "key=1172\n", "\tcategory=peace\n", "\tyear=1993\n", "\tlaureates=[{firstname=Nelson, share=2, id=555, surname=Mandela, motivation=\"for their work for the peaceful termination of the apartheid regime, and for laying the foundations for a new democratic South Africa\"\n", "key=1232\n", "\tcategory=peace\n", "\tyear=1983\n", "\tlaureates=[{firstname=Lech, share=1, id=545, surname=Walesa, motivation=\"for non-violent struggle for free trade unions and human rights in Poland\"}]\n", "key=1451\n", "\tcategory=peace\n", "\tyear=1942\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1292\n", "\tcategory=peace\n", "\tyear=1973\n", "\tlaureates=[{firstname=Henry, share=2, id=530, surname=Kissinger, motivation=\"for jointly having negotiated a cease fire in Vietnam in 1973\"}, {firstname=Le Duc Tho, share=2, id=531, motivation=\"for jointly havi\n", "key=1546\n", "\tcategory=peace\n", "\tyear=1923\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1431\n", "\tcategory=peace\n", "\tyear=1946\n", "\tlaureates=[{firstname=Emily Greene, share=2, id=506, surname=Balch, motivation=\"for her lifelong work for the cause of peace\"}, {firstname=John R., share=2, id=507, surname=Mott, motivation=\"for his contributio\n", "key=1196\n", "\tcategory=peace\n", "\tyear=1989\n", "\tlaureates=[{firstname=Lhamo, share=1, id=551, surname=Thondup, motivation=\"for advocating peaceful solutions based upon tolerance and mutual respect in order to preserve the historical and cultural heritage of \n", "key=1298\n", "\tcategory=peace\n", "\tyear=1972\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money for 1972 was allocated to the Main Fund.\"\n", "key=1436\n", "\tcategory=peace\n", "\tyear=1945\n", "\tlaureates=[{firstname=Cordell, share=1, id=505, surname=Hull, motivation=\"for his indefatigable work for international understanding and his pivotal role in establishing the United Nations\"}]\n", "key=1178\n", "\tcategory=peace\n", "\tyear=1992\n", "\tlaureates=[{firstname=Rigoberta, share=1, id=554, surname=Menchú Tum, motivation=\"for her struggle for social justice and ethno-cultural reconciliation based on respect for the rights of indigenous peoples\"}]\n", "key=1371\n", "\tcategory=peace\n", "\tyear=1958\n", "\tlaureates=[{firstname=Georges, share=1, id=517, surname=Pire, motivation=\"for his efforts to help refugees to leave their camps and return to a life of freedom and dignity\"}]\n", "key=1356\n", "\tcategory=peace\n", "\tyear=1961\n", "\tlaureates=[{firstname=Dag, share=1, id=520, surname=Hammarskjöld, motivation=\"for developing the UN into an effective and constructive international organization, capable of giving life to the principles and ai\n", "key=1040\n", "\tcategory=peace\n", "\tyear=2015\n", "\tlaureates=[{share=1, firstname=National Dialogue Quartet, id=925, motivation=\"for its decisive contribution to the building of a pluralistic democracy in Tunisia in the wake of the Jasmine Revolution of 2011\"}]\n", "key=1406\n", "\tcategory=peace\n", "\tyear=1951\n", "\tlaureates=[{firstname=Léon, share=1, id=512, surname=Jouhaux, motivation=\"for having devoted his life to the fight against war through the promotion of social justice and brotherhood among men and nations\"}]\n", "key=1184\n", "\tcategory=peace\n", "\tyear=1991\n", "\tlaureates=[{firstname=Aung San Suu Kyi, share=1, id=553, motivation=\"for her non-violent struggle for democracy and human rights\"}]\n", "key=1571\n", "\tcategory=peace\n", "\tyear=1918\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1202\n", "\tcategory=peace\n", "\tyear=1988\n", "\tlaureates=[{share=1, firstname=United Nations Peacekeeping Forces, id=550, motivation=\"for preventing armed clashes and creating conditions for negotiations\"}]\n", "key=1416\n", "\tcategory=peace\n", "\tyear=1949\n", "\tlaureates=[{firstname=John, share=1, id=510, surname=Boyd Orr, motivation=\"for his lifelong effort to conquer hunger and want, thereby helping to remove a major cause of military conflict and war\"}]\n", "key=1160\n", "\tcategory=peace\n", "\tyear=1995\n", "\tlaureates=[{firstname=Joseph, share=2, id=560, surname=Rotblat, motivation=\"for their efforts to diminish the part played by nuclear arms in international politics and, in the longer run, to eliminate such arms\n", "key=1531\n", "\tcategory=peace\n", "\tyear=1926\n", "\tlaureates=[{firstname=Aristide, share=2, id=490, surname=Briand, motivation=\"for their crucial role in bringing about the Locarno Treaty\"}, {firstname=Gustav, share=2, id=491, surname=Stresemann, motivation=\"fo\n", "key=1262\n", "\tcategory=peace\n", "\tyear=1978\n", "\tlaureates=[{firstname=Anwar, share=2, id=538, surname=al-Sadat, motivation=\"for jointly having negotiated peace between Egypt and Israel in 1978\"}, {firstname=Menachem, share=2, id=539, surname=Begin, motivatio\n", "key=1028\n", "\tcategory=peace\n", "\tyear=2017\n", "\tlaureates=[{share=1, firstname=International Campaign to Abolish Nuclear Weapons, id=948, motivation=\"for its work to draw attention to the catastrophic humanitarian consequences of any use of nuclear weapons a\n", "key=1316\n", "\tcategory=peace\n", "\tyear=1969\n", "\tlaureates=[{share=1, firstname=International Labour Organization, id=527, motivation=\"for creating international legislation insuring certain norms for working conditions in every country\"}]\n", "key=1046\n", "\tcategory=peace\n", "\tyear=2014\n", "\tlaureates=[{firstname=Kailash, share=2, id=913, surname=Satyarthi, motivation=\"for their struggle against the suppression of children and young people and for the right of all children to education\"}, {firstnam\n", "key=1541\n", "\tcategory=peace\n", "\tyear=1924\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1070\n", "\tcategory=peace\n", "\tyear=2010\n", "\tlaureates=[{firstname=Xiaobo, share=1, id=855, surname=Liu, motivation=\"for his long and non-violent struggle for fundamental human rights in China\"}]\n", "key=1106\n", "\tcategory=peace\n", "\tyear=2004\n", "\tlaureates=[{firstname=Wangari, share=1, id=783, surname=Maathai, motivation=\"for her contribution to sustainable development, democracy and peace\"}]\n", "key=1016\n", "\tcategory=peace\n", "\tyear=2019\n", "\tlaureates=[{firstname=Abiy, share=1, id=981, surname=Ahmed Ali, motivation=\"for his efforts to achieve peace and international cooperation, and in particular for his decisive initiative to resolve the border co\n", "key=1238\n", "\tcategory=peace\n", "\tyear=1982\n", "\tlaureates=[{firstname=Alva, share=2, id=543, surname=Myrdal, motivation=\"for their work for disarmament and nuclear and weapon-free zones\"}, {firstname=Alfonso, share=2, id=544, surname=García Robles, motivatio\n", "key=1526\n", "\tcategory=peace\n", "\tyear=1927\n", "\tlaureates=[{firstname=Ferdinand, share=2, id=492, surname=Buisson, motivation=\"for their contribution to the emergence in France and Germany of a public opinion which favours peaceful international cooperation\"\n", "key=1208\n", "\tcategory=peace\n", "\tyear=1987\n", "\tlaureates=[{firstname=Oscar, share=1, id=549, surname=Arias Sánchez, motivation=\"for his work for lasting peace in Central America\"}]\n", "key=1511\n", "\tcategory=peace\n", "\tyear=1930\n", "\tlaureates=[{firstname=Nathan, share=1, id=495, surname=Söderblom, motivation=\"for promoting Christian unity and helping create 'that new attitude of mind which is necessary if peace between nations is to become\n", "key=1148\n", "\tcategory=peace\n", "\tyear=1997\n", "\tlaureates=[{share=2, firstname=International Campaign to Ban Landmines, id=564, motivation=\"for their work for the banning and clearing of anti-personnel mines\"}, {firstname=Jody, share=2, id=565, surname=Willi\n", "key=1626\n", "\tcategory=peace\n", "\tyear=1907\n", "\tlaureates=[{firstname=Ernesto Teodoro, share=2, id=471, surname=Moneta, motivation=\"for his work in the press and in peace meetings, both public and private, for an understanding between France and Italy\"}, {fi\n", "key=1346\n", "\tcategory=peace\n", "\tyear=1963\n", "\tlaureates=[{share=2, firstname=International Committee of the Red Cross, id=482, motivation=\"for promoting the principles of the Geneva Convention and cooperation with the UN\"}, {share=2, firstname=League of Re\n", "key=1446\n", "\tcategory=peace\n", "\tyear=1943\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1136\n", "\tcategory=peace\n", "\tyear=1999\n", "\tlaureates=[{share=1, firstname=Doctors Without Borders, id=568, motivation=\"in recognition of the organisation's pioneering humanitarian work on several continents\"}]\n", "key=1576\n", "\tcategory=peace\n", "\tyear=1917\n", "\tlaureates=[{share=1, firstname=International Committee of the Red Cross, id=482, motivation=\"for the efforts to take care of wounded soldiers and prisoners of war and their families\"}]\n", "key=1501\n", "\tcategory=peace\n", "\tyear=1932\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1481\n", "\tcategory=peace\n", "\tyear=1936\n", "\tlaureates=[{firstname=Carlos, share=1, id=501, surname=Saavedra Lamas, motivation=\"for his role as father of the Argentine Antiwar Pact of 1933, which he also used as a means to mediate peace between Paraguay a\n", "key=1118\n", "\tcategory=peace\n", "\tyear=2002\n", "\tlaureates=[{firstname=Jimmy, share=1, id=762, surname=Carter, motivation=\"for his decades of untiring effort to find peaceful solutions to international conflicts, to advance democracy and human rights, and to \n", "key=1064\n", "\tcategory=peace\n", "\tyear=2011\n", "\tlaureates=[{firstname=Ellen, share=3, id=869, surname=Johnson Sirleaf, motivation=\"for their non-violent struggle for the safety of women and for women's rights to full participation in peace-building work\"}, {\n", "key=1082\n", "\tcategory=peace\n", "\tyear=2008\n", "\tlaureates=[{firstname=Martti, share=1, id=833, surname=Ahtisaari, motivation=\"for his important efforts, on several continents and over more than three decades, to resolve international conflicts\"}]\n", "key=1214\n", "\tcategory=peace\n", "\tyear=1986\n", "\tlaureates=[{firstname=Elie, share=1, id=548, surname=Wiesel, motivation=\"for being a messenger to mankind: his message is one of peace, atonement and dignity\"}]\n", "key=1058\n", "\tcategory=peace\n", "\tyear=2012\n", "\tlaureates=[{share=1, firstname=European Union, id=881, motivation=\"for over six decades contributed to the advancement of peace and reconciliation, democracy and human rights in Europe\"}]\n", "key=1366\n", "\tcategory=peace\n", "\tyear=1959\n", "\tlaureates=[{firstname=Philip, share=1, id=518, surname=Noel-Baker, motivation=\"for his longstanding contribution to the cause of disarmament and peace\"}]\n", "key=1076\n", "\tcategory=peace\n", "\tyear=2009\n", "\tlaureates=[{firstname=Barack, share=1, id=845, surname=Obama, motivation=\"for his extraordinary efforts to strengthen international diplomacy and cooperation between peoples\"}]\n", "key=1112\n", "\tcategory=peace\n", "\tyear=2003\n", "\tlaureates=[{firstname=Shirin, share=1, id=773, surname=Ebadi, motivation=\"for her efforts for democracy and human rights. She has focused especially on the struggle for the rights of women and children\"}]\n", "key=1641\n", "\tcategory=peace\n", "\tyear=1904\n", "\tlaureates=[{share=1, firstname=Institute of International Law, id=467, motivation=\"for its striving in public law to develop peaceful ties between nations and to make the laws of war more humane\"}]\n", "key=1310\n", "\tcategory=peace\n", "\tyear=1970\n", "\tlaureates=[{firstname=Norman, share=1, id=528, surname=Borlaug, motivation=\"for having given a well-founded hope - the green revolution\"}]\n", "key=1376\n", "\tcategory=peace\n", "\tyear=1957\n", "\tlaureates=[{firstname=Lester Bowles, share=1, id=516, surname=Pearson, motivation=\"for his crucial contribution to the deployment of a United Nations Emergency Force in the wake of the Suez Crisis\"}]\n", "key=1351\n", "\tcategory=peace\n", "\tyear=1962\n", "\tlaureates=[{firstname=Linus, share=1, id=217, surname=Pauling, motivation=\"for his fight against the nuclear arms race between East and West\"}]\n", "key=1456\n", "\tcategory=peace\n", "\tyear=1941\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1336\n", "\tcategory=peace\n", "\tyear=1965\n", "\tlaureates=[{share=1, firstname=United Nations Children's Fund, id=525, motivation=\"for its effort to enhance solidarity between nations and reduce the difference between rich and poor states\"}]\n", "key=1274\n", "\tcategory=peace\n", "\tyear=1976\n", "\tlaureates=[{firstname=Betty, share=2, id=535, surname=Williams, motivation=\"for the courageous efforts in founding a movement to put an end to the violent conflict in Northern Ireland\"}, {firstname=Mairead, sha\n", "key=1341\n", "\tcategory=peace\n", "\tyear=1964\n", "\tlaureates=[{firstname=Martin Luther, share=1, id=524, surname=King Jr., motivation=\"for his non-violent struggle for civil rights for the Afro-American population\"}]\n", "key=1496\n", "\tcategory=peace\n", "\tyear=1933\n", "\tlaureates=[{firstname=Sir Norman, share=1, id=498, surname=Angell, motivation=\"for having exposed by his pen the illusion of war and presented a convincing plea for international cooperation and peace\"}]\n", "key=1326\n", "\tcategory=peace\n", "\tyear=1967\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1124\n", "\tcategory=peace\n", "\tyear=2001\n", "\tlaureates=[{share=2, firstname=United Nations, id=748, motivation=\"for their work for a better organized and more peaceful world\"}, {firstname=Kofi, share=2, id=749, surname=Annan, motivation=\"for their work fo\n", "key=1656\n", "\tcategory=peace\n", "\tyear=1901\n", "\tlaureates=[{firstname=Henry, share=2, id=462, surname=Dunant, motivation=\"for his humanitarian efforts to help wounded soldiers and create international understanding\"}, {firstname=Frédéric, share=2, id=463, su\n", "key=1100\n", "\tcategory=peace\n", "\tyear=2005\n", "\tlaureates=[{share=2, firstname=International Atomic Energy Agency, id=797, motivation=\"for their efforts to prevent nuclear energy from being used for military purposes and to ensure that nuclear energy for pea\n", "key=1586\n", "\tcategory=peace\n", "\tyear=1915\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1556\n", "\tcategory=peace\n", "\tyear=1921\n", "\tlaureates=[{firstname=Hjalmar, share=2, id=485, surname=Branting, motivation=\"for their lifelong contributions to the cause of peace and organized internationalism\"}, {firstname=Christian, share=2, id=486, surn\n", "key=1022\n", "\tcategory=peace\n", "\tyear=2018\n", "\tlaureates=[{firstname=Denis, share=2, id=966, surname=Mukwege, motivation=\"for their efforts to end the use of sexual violence as a weapon of war and armed conflict\"}, {firstname=Nadia, share=2, id=967, surname\n", "key=1581\n", "\tcategory=peace\n", "\tyear=1916\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1052\n", "\tcategory=peace\n", "\tyear=2013\n", "\tlaureates=[{share=1, firstname=Organisation for the Prohibition of Chemical Weapons, id=893, motivation=\"for its extensive efforts to eliminate chemical weapons\"}]\n", "key=1088\n", "\tcategory=peace\n", "\tyear=2007\n", "\tlaureates=[{share=2, firstname=Intergovernmental Panel on Climate Change, id=818, motivation=\"for their efforts to build up and disseminate greater knowledge about man-made climate change, and to lay the founda\n", "key=1421\n", "\tcategory=peace\n", "\tyear=1948\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1396\n", "\tcategory=peace\n", "\tyear=1953\n", "\tlaureates=[{firstname=George C., share=1, id=514, surname=Marshall, motivation=\"for proposing and supervising the plan for the economic recovery of Europe\"}]\n", "key=1566\n", "\tcategory=peace\n", "\tyear=1919\n", "\tlaureates=[{firstname=Woodrow, share=1, id=483, surname=Wilson, motivation=\"for his role as founder of the League of Nations\"}]\n", "key=1361\n", "\tcategory=peace\n", "\tyear=1960\n", "\tlaureates=[{firstname=Albert, share=1, id=519, surname=Lutuli, motivation=\"for his non-violent struggle against apartheid\"}]\n", "key=1004\n", "\tcategory=peace\n", "\tyear=2021\n", "\tlaureates=[{firstname=Maria, share=2, id=1005, surname=Ressa, motivation=\"for their efforts to safeguard freedom of expression, which is a precondition for democracy and lasting peace\"}, {firstname=Dmitry, shar\n", "key=1611\n", "\tcategory=peace\n", "\tyear=1910\n", "\tlaureates=[{share=1, firstname=Permanent International Peace Bureau, id=477, motivation=\"for acting as a link between the peace societies of the various countries, and helping them to organize the world rallies\n", "key=1381\n", "\tcategory=peace\n", "\tyear=1956\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was with 1/3 allocated to the Main Fund and with 2/3 to the Special Fund of this prize section.\"\n", "key=1286\n", "\tcategory=peace\n", "\tyear=1974\n", "\tlaureates=[{firstname=Seán, share=2, id=532, surname=MacBride, motivation=\"for his efforts to secure and develop human rights throughout the world\"}, {firstname=Eisaku, share=2, id=533, surname=Sato, motivation\n", "key=1142\n", "\tcategory=peace\n", "\tyear=1998\n", "\tlaureates=[{firstname=John, share=2, id=566, surname=Hume, motivation=\"for their efforts to find a peaceful solution to the conflict in Northern Ireland\"}, {firstname=David, share=2, id=567, surname=Trimble, mo\n", "key=1441\n", "\tcategory=peace\n", "\tyear=1944\n", "\tlaureates=[{share=1, firstname=International Committee of the Red Cross, id=482, motivation=\"for the great work it has performed during the war on behalf of humanity\"}]\n", "key=1516\n", "\tcategory=peace\n", "\tyear=1929\n", "\tlaureates=[{firstname=Frank B., share=1, id=494, surname=Kellogg, motivation=\"for his crucial role in bringing about the Briand-Kellogg Pact\"}]\n", "key=1094\n", "\tcategory=peace\n", "\tyear=2006\n", "\tlaureates=[{firstname=Muhammad, share=2, id=809, surname=Yunus, motivation=\"for their efforts to create economic and social development from below\"}, {share=2, firstname=Grameen Bank, id=810, motivation=\"for th\n", "key=1596\n", "\tcategory=peace\n", "\tyear=1913\n", "\tlaureates=[{firstname=Henri, share=1, id=481, surname=La Fontaine, motivation=\"for his unparalleled contribution to the organization of peaceful internationalism\"}]\n", "key=1616\n", "\tcategory=peace\n", "\tyear=1909\n", "\tlaureates=[{firstname=Auguste, share=2, id=475, surname=Beernaert, motivation=\"for their prominent position in the international movement for peace and arbitration\"}, {firstname=Paul Henri, share=2, id=476, sur\n", "key=1631\n", "\tcategory=peace\n", "\tyear=1906\n", "\tlaureates=[{firstname=Theodore, share=1, id=470, surname=Roosevelt, motivation=\"for his role in bringing to an end the bloody war recently waged between two of the world's great powers, Japan and Russia\"}]\n", "key=1651\n", "\tcategory=peace\n", "\tyear=1902\n", "\tlaureates=[{firstname=Élie, share=2, id=464, surname=Ducommun, motivation=\"for his untiring and skilful directorship of the Bern Peace Bureau\"}, {firstname=Albert, share=2, id=465, surname=Gobat, motivation=\"fo\n", "key=1331\n", "\tcategory=peace\n", "\tyear=1966\n", "\toverallMotivation=\"No Nobel Prize was awarded this year. The prize money was allocated to the Special Fund of this prize section.\"\n", "key=1166\n", "\tcategory=peace\n", "\tyear=1994\n", "\tlaureates=[{firstname=Yasser, share=3, id=557, surname=Arafat, motivation=\"for their efforts to create peace in the Middle East\"}, {firstname=Shimon, share=3, id=558, surname=Peres, motivation=\"for their effort\n", "key=1551\n", "\tcategory=peace\n", "\tyear=1922\n", "\tlaureates=[{firstname=Fridtjof, share=1, id=487, surname=Nansen, motivation=\"for his leading role in the repatriation of prisoners of war, in international relief work and as the League of Nations' High Commiss\n", "key=1476\n", "\tcategory=peace\n", "\tyear=1937\n", "\tlaureates=[{firstname=Robert, share=1, id=502, surname=Cecil, motivation=\"for his tireless effort in support of the League of Nations, disarmament and peace\"}]\n" ] } ], "source": [ "// Create an index on the category field.\n", "// idx_json_bin_category_str\n", "createIndex(\"idx_json_bin_category_str\", JsonBin, IndexType.STRING, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"category\")));\n", "\n", "// Find nobel prizes for peace.\n", "System.out.println(\"Nobel peace prizes:\");\n", "Filter filter = Filter.equal(JsonBin, \"peace\", CTX.mapKey(Value.get(\"category\")));\n", "executeQueryAndPrintResults(filter, JsonBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2: Space Companies JSON Dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read JSON File and Populate Database\n", "We will load JSON documents from the file \"space_companies.json\", and store each prize entry in the database in a separate record with user_key starting at 2000 in bin \"json_bin\". You can view the file contents by opening it from Jupyter's file browser." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Read JSON doc: /home/jovyan/notebooks/java/space_companies.json\n", "Example company schema: \n", "{\n", " \"_id\" : \"58d163cd6ed7bb3c6ee62e63\",\n", " \"company\" : {\n", " \"_id\" : \"58d164516ed7bb3c6ee62e64\",\n", " \"about\" : \"Telespazio VEGA UK is a dynamic and experienced consulting, technology and engineering services business based in Luton, UK. Following our merger into the Telespazio Group in 2011 we now have access to a vast array of world leading facilities and services, which we provide both within the UK and to our export markets. With a strong history in the European space arena starting in 1978, we have rich relationships across the space industry, with staff working closely with the European Space Agency (ESA) on programs and projects for over 35 years. \",\n", " \"backgroundImage\" : \"https://www.spaceindividuals.com/var/www/images/8907e7e06a094fe31098d167567fa1dc.jpg\",\n", " \"companyLocation\" : \"Capability Green, Luton LU1 3LU, UK\",\n", " \"companyName\" : \"Telespazio VEGA UK\",\n", " \"createdAt\" : \"2017-06-05T00:00:00.000Z\",\n", " \"culture\" : \"As an organisation that aspires to excellence, Telespazio VEGA recognises the need to appreciate our people for their achievements. Without the skills, dedication and enthusiasm of our people, we would not be where we are today. Telespazio VEGA continually strives to create a rewarding environment, offer ample opportunity for personal and professional development and support our people in balancing work and life.\\nTo ensure that we remain relevant and of value to our clients, Telespazio VEGA has created an environment that encourages and rewards innovation and entrepreneurial spirit. This, offered with the financial stability afforded us as part of the Finmeccanica group of companies, means that valid business ideas are given the appropriate backing and support to enable them to succeed.\\nWe also recognise that each individual has their own aspirations for personal development. Therefore, we offer a range of career options that enable consultants to enhance individual niche capabilities, embrace the challenge of business development, and take on the responsibility of a management role. These opportunities are made available through a structured career development programme which enables our people to look closely at themselves, where they are today, and where they see themselves in the future. We recognise and support all these ambitions and look to retain a rich mix of skills and capabilities that will continue to support our clients’ evolving business requirements.\\n\\nContinuous Learning\\nAs a business, Telespazio VEGA places great emphasis on allowing our people to learn and develop, both personally and professionally. We share ideas with colleagues and clients, challenge each other and learn through our involvement with a variety of projects.\\nTelespazio VEGA provides ample opportunity for more formal training. We encourage our people to undertake professional qualifications, and support those who undertake study which relates to their work\\n\",\n", " \"email\" : \"careers@telespazio.com\",\n", " \"galleryImages\" : [ \"https://spaceindividuals.com/var/www/images/4feb7c40b8b3d5a3ea83b600d6731b94.jpg\", \"https://spaceindividuals.com/var/www/images/ab664d6614614b4b771c3fe99e55383b.jpg\", \"https://spaceindividuals.com/var/www/images/d3ab90c334abfe301c93b14b1ff0f2d4.jpg\" ],\n", " \"isPublished\" : true,\n", " \"kindOfEmployeeLookFor\" : null,\n", " \"logoURL\" : \"https://spaceindividuals.com/var/www/images/ca6d9b5d356a2186fb9144b6bb34ab03.png\",\n", " \"mission\" : null,\n", " \"paymentType\" : \"enterprise\",\n", " \"phone\" : \"+44 (0)1582 399000\",\n", " \"sizeCompany\" : \"100-250 employees\",\n", " \"specialities\" : \"We now serve a wide range of public and private commercial markets globally by providing efficient, skilled support, the most up to date space data and services, and fundamentally, understanding and integrating the needs of our customers.\\n\\nWe have developed and harnessed our knowledge and expertise to deliver unique complex solutions for:\\nGEO Information, Satellite Systems and Applications & Satellite Communications.\\nWe offer robust and reliable ground segment systems for satellite missions and utilise space assets to develop downstream applications across a wide variety of sectors. \\nWe provide world leading scientific services on earth observation and space science missions.\\nOur valued and experienced knowledgeable experts deliver consulting and engineering support to space missions worldwide.\",\n", " \"testimonials\" : null,\n", " \"updatedAt\" : \"2021-01-20T09:45:41.731Z\",\n", " \"weOfferChallenges\" : null,\n", " \"weOfferKindOfChallenges\" : \"Telespazio VEGA’s culture is defined by our people, and drives the value we deliver through the relationships we have with our stakeholders.\\n\\nOur culture is distinct and discernible, mirroring the multi-national, multi-disciplinary environment in which we work. It is based on a practical engineering ethos that thrives on challenge, and engenders a rigorous, analytical, results-oriented approach to problem solving. We are open and honest, say what we believe, and challenge that which we do not.\\n\\nAt Telespazio VEGA, our culture is also based on a pursuit of excellence and thought leadership – not only in our understanding of the technologies with which we work and the market domains in which they are applied, but also in the quality of the ideas, methodologies, services and solutions we deliver.\\n\\nWe share with our clients an unparalleled enthusiasm for Space and its application in society. Therefore, we are able to empathise strongly, and invest this understanding and commitment into long-term relationships with our clients and stakeholders.\\n\",\n", " \"weOfferKindOfTrainings\" : null,\n", " \"weOfferTrainings\" : null,\n", " \"webSite\" : \"http://telespazio-vega.com\"\n", " },\n", " \"createdAt\" : \"2017-06-05T00:00:00.000Z\",\n", " \"email\" : \"careers@telespazio.com\",\n", " \"jobs\" : null,\n", " \"numberOfJobs\" : 0,\n", " \"updatedAt\" : \"2021-01-20T09:47:36.694Z\"\n", "} \n", "\n", "Stored 154 JSON documents in database \n" ] } ], "source": [ "String JsonFilePath = \"/home/jovyan/notebooks/java/space_companies.json\";\n", "String JsonBin = \"json_bin\";\n", "Integer UserKeyOffset = 2000;\n", "\n", "// Read the json document into a string.\n", "String jsonString = FileUtils.readFileToString(new File(JsonFilePath));\n", "System.out.format(\"Read JSON doc: %s\\n\", JsonFilePath);;\n", "\n", "// Convert JSON string to a JsonNode\n", "JsonNode rootNode = JsonConverters.convertStringToJsonNode(jsonString);\n", "\n", "// Add the companies in JSON in separate records\n", "JsonNode companiesNode = rootNode.path(\"companies\");\n", "\n", "Integer companyIndex = 0;\n", "for (JsonNode companyNode : companiesNode) {\n", " if (companyIndex == 1) {\n", " System.out.format(\"Example company schema: \\n%s \\n\\n\", companyNode.toPrettyString());\n", " }\n", " Key jsonDocKey = new Key(Namespace, Set, UserKeyOffset+companyIndex+1); \n", " documentClient.put(jsonDocKey, JsonBin, companyNode); \n", " companyIndex++;\n", "}\n", "System.out.format(\"Stored %d JSON documents in database \\n\", companyIndex);;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute Query with Index " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find companies by number of job postings." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_json_bin_number_of_jobs_num on ns=test set=cdt-indexing bin=json_bin.\n", "Companies with numberOfJobs greater than 10:\n", "key=2013\n", "\tcreatedAt=2018-03-04T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=11\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/971f13def35f0b21e379fd1555b3e10d.jpg, isPublished=true, companyName=AKKA Technologies, about=As a consulting and engineering Group and Euro\n", "\t_id=593fdfae9839ef0a13b6b92d\n", "\temail=kontakt@akka.eu\n", "\tupdatedAt=2021-01-27T14:04:41.487Z\n", "key=2070\n", "\tcreatedAt=2018-03-29T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=62\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/02873814215a7b781e0f5f10e726926b.jpg, companyName=CERN, about=Imagine taking part in the largest scientific experiment in the world. CERN n\n", "\t_id=59df28ff947f49c77101e477\n", "\temail=recruitment.service@cern.ch\n", "\tupdatedAt=2021-01-27T09:28:02.836Z\n", "key=2084\n", "\tcreatedAt=2018-04-01T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=30\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/81e0f8e35dbab0242b8729f795986047.jpg, isPublished=true, companyName=RHEA Group, about=25 years of experience in the professional engineerin\n", "\t_id=58d965bfde9c102b036040f7\n", "\temail=temp@rheagroup.com\n", "\tupdatedAt=2021-01-27T10:16:40.854Z\n", "key=2107\n", "\tcreatedAt=2018-11-21T15:48:41.941Z\n", "\tjobs=null\n", "\tnumberOfJobs=95\n", "\tcompany={backgroundImage=/profile-pic/1574021440639.jpg, isPublished=true, companyName=European Space Agency - ESA, about=The European Space Agency (ESA) is Europe’s gateway to space. Its mission is to shape \n", "\t_id=5bf57e594739c05820a92eee\n", "\temail=ZINEB.ELOMRI@ESA.INT\n", "\tupdatedAt=2021-01-25T09:34:07.372Z\n", "key=2071\n", "\tcreatedAt=2018-03-07T14:02:08.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=16\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/093fb01f43d429801105c6fc0b224d40.jpg, companyName=Terma Group, about=At Terma our people make the difference. We employ talented, dedicated\n", "\t_id=590a258c7cd117274a0ae681\n", "\temail=recruitment.de@terma.com\n", "\tupdatedAt=2021-01-27T13:24:59.183Z\n", "key=2083\n", "\tcreatedAt=2018-03-04T15:01:08.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=52\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/98181cdb3d430d1e90c695e44caeea90.jpg, isPublished=true, companyName=Serco Europe, about=The Serco Group is a FTSE 250 corporation with over\n", "\t_id=58f78ba25a0782e629c7b621\n", "\temail=tatiana.thiessen@serco.com\n", "\tupdatedAt=2020-11-30T09:49:19.914Z\n", "key=2051\n", "\tcreatedAt=2018-04-17T13:04:35.890Z\n", "\tjobs=null\n", "\tnumberOfJobs=26\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/604d16af11bf4f83e8db7aa57a10c3d0.jpg, isPublished=true, companyName=OHB-System AG, about=3,2,1, lift off...\n", "\n", "At OHB System AG, you will be \n", "\t_id=5ad5f0e39a451a20f65c5a18\n", "\temail=career@ohb.de\n", "\tupdatedAt=2020-12-22T17:10:11.754Z\n" ] } ], "source": [ "// Create an index on the numberOfJobs field.\n", "// idx_json_bin_number_of_jobs_num\n", "createIndex(\"idx_json_bin_number_of_jobs_num\", JsonBin, IndexType.NUMERIC, IndexCollectionType.DEFAULT, \n", " CTX.mapKey(Value.get(\"numberOfJobs\")));\n", "\n", "// Find companies with more than 10 job postings.\n", "System.out.println(\"Companies with numberOfJobs greater than 10:\");\n", "Filter filter = Filter.range(JsonBin, 11, 100, CTX.mapKey(Value.get(\"numberOfJobs\")));\n", "executeQueryAndPrintResults(filter, JsonBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find companies by a nested field such as the payment type.\n", "The paymentType field may be truncated in the result output, but can be verified with the AQL query in the terminal tab: aql -c \"select json_bin from test.cdt-indexing where PK=key\". " ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_json_bin_payment_type_str on ns=test set=cdt-indexing bin=json_bin.\n", "Companies with enterprise payment type:\n", "key=2013\n", "\tcreatedAt=2018-03-04T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=11\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/971f13def35f0b21e379fd1555b3e10d.jpg, isPublished=true, companyName=AKKA Technologies, about=As a consulting and engineering Group and Euro\n", "\t_id=593fdfae9839ef0a13b6b92d\n", "\temail=kontakt@akka.eu\n", "\tupdatedAt=2021-01-27T14:04:41.487Z\n", "key=2002\n", "\tcreatedAt=2017-06-05T00:00:00.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=0\n", "\tcompany={backgroundImage=https://www.spaceindividuals.com/var/www/images/8907e7e06a094fe31098d167567fa1dc.jpg, isPublished=true, companyName=Telespazio VEGA UK, about=Telespazio VEGA UK is a dynamic and exper\n", "\t_id=58d163cd6ed7bb3c6ee62e63\n", "\temail=careers@telespazio.com\n", "\tupdatedAt=2021-01-20T09:47:36.694Z\n", "key=2045\n", "\tcreatedAt=2018-03-24T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=8\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/0786a572d77f4a0b8a48c5b054766c54.jpg, isPublished=true, companyName=Telespazio Germany GmbH, about=Telespazio Germany is the first choice a\n", "\t_id=58e13089de9c102b03604295\n", "\temail=recruitment@telespazio.de\n", "\tupdatedAt=2021-01-27T11:04:38.881Z\n", "key=2031\n", "\tcreatedAt=2018-04-25T12:27:23.286Z\n", "\tjobs=null\n", "\tnumberOfJobs=0\n", "\tcompany={paymentTypeOrdered=enterprise, backgroundImage=https://www.spaceindividuals.com/var/www/images/5207d3c4216068b9423ade06215f1529.PNG, isPublished=true, companyName=European GNSS Agency, about=Space is\n", "\t_id=5ae0742b4392412b350ca064\n", "\temail=jobs@gsa.europa.eu\n", "\tupdatedAt=2020-11-13T17:25:53.261Z\n", "key=2107\n", "\tcreatedAt=2018-11-21T15:48:41.941Z\n", "\tjobs=null\n", "\tnumberOfJobs=95\n", "\tcompany={backgroundImage=/profile-pic/1574021440639.jpg, isPublished=true, companyName=European Space Agency - ESA, about=The European Space Agency (ESA) is Europe’s gateway to space. Its mission is to shape \n", "\t_id=5bf57e594739c05820a92eee\n", "\temail=ZINEB.ELOMRI@ESA.INT\n", "\tupdatedAt=2021-01-25T09:34:07.372Z\n", "key=2084\n", "\tcreatedAt=2018-04-01T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=30\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/81e0f8e35dbab0242b8729f795986047.jpg, isPublished=true, companyName=RHEA Group, about=25 years of experience in the professional engineerin\n", "\t_id=58d965bfde9c102b036040f7\n", "\temail=temp@rheagroup.com\n", "\tupdatedAt=2021-01-27T10:16:40.854Z\n", "key=2078\n", "\tcreatedAt=2017-06-06T00:00:00.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=3\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/29155fc6f21de51c6888a8c7108a211e.jpg, companyName=HE Space, about=HE Space Operations is a privately-owned company, operating international\n", "\t_id=58ec81c2452918cb21646e51\n", "\temail=jobs@hespace.com\n", "\tupdatedAt=2021-01-20T13:44:16.400Z\n", "key=2081\n", "\tcreatedAt=2018-03-03T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=7\n", "\tcompany={backgroundImage=https://www.spaceindividuals.com/var/www/images/3f43b6ad7566a74d13bd838b0e9d7a06.jpg, companyName=Space Applications Services NV/SA, about=Space Applications Services NV/SA Head Offic\n", "\t_id=59fb14a7da2f26fd7c0d309d\n", "\temail=hr@spaceapplications.com\n", "\tupdatedAt=2021-01-27T07:59:12.608Z\n", "key=2149\n", "\tcreatedAt=2019-03-26T21:06:11.751Z\n", "\tjobs=null\n", "\tnumberOfJobs=4\n", "\tcompany={paymentTypeOrdered=pro, isPublished=true, companyName=Made In Space Europe, about=We are an aerospace firm dedicated to providing industry-wide access to sophisticated, in-space, robotic arm solution\n", "\t_id=5c9a9443a6da7408f3ced540\n", "\temail=admin@madeinspaceeurope.com\n", "\tupdatedAt=2021-01-27T10:47:18.850Z\n", "key=2070\n", "\tcreatedAt=2018-03-29T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=62\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/02873814215a7b781e0f5f10e726926b.jpg, companyName=CERN, about=Imagine taking part in the largest scientific experiment in the world. CERN n\n", "\t_id=59df28ff947f49c77101e477\n", "\temail=recruitment.service@cern.ch\n", "\tupdatedAt=2021-01-27T09:28:02.836Z\n", "key=2018\n", "\tcreatedAt=2018-03-03T14:00:04.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=8\n", "\tcompany={backgroundImage=/profile-pic/1610979249861.jpg, companyName=DLR GfR mbH, about=We’re the DLR Space Applications Institute mbH – the DLR GfR for short.
, paymentType=enterprise, webSite=www.dlr-\n", "\t_id=59a03d34a927a2237eb23ae1\n", "\temail=gundula.lenz@dlr-gfr.com\n", "\tupdatedAt=2021-01-25T13:37:20.548Z\n", "key=2052\n", "\tcreatedAt=2017-05-09T00:00:00.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=0\n", "\tcompany={paymentTypeOrdered=enterprise, backgroundImage=/profile-pic/1568838624562.jpg, companyName=Space Individuals, about=We want to offer our support to the space industry by connecting those who long to \n", "\t_id=58caf8b26ed7bb3c6ee62ce4\n", "\temail=welcome@spaceindividuals.com\n", "\tupdatedAt=2021-01-20T09:44:13.172Z\n", "key=2071\n", "\tcreatedAt=2018-03-07T14:02:08.000Z\n", "\tjobs=null\n", "\tnumberOfJobs=16\n", "\tcompany={backgroundImage=https://spaceindividuals.com/var/www/images/093fb01f43d429801105c6fc0b224d40.jpg, companyName=Terma Group, about=At Terma our people make the difference. We employ talented, dedicated\n", "\t_id=590a258c7cd117274a0ae681\n", "\temail=recruitment.de@terma.com\n", "\tupdatedAt=2021-01-27T13:24:59.183Z\n", "key=2145\n", "\tcreatedAt=2020-10-27T16:27:01.753Z\n", "\tjobs=null\n", "\tnumberOfJobs=0\n", "\tcompany={createdAt=2020-10-27T16:28:06.390Z, testimonials=[], paymentTypeOrdered=free, isPublished=true, companyName=Bitpanda, _id=5f984a9692723201a3525dbf, companyLocation=Vienna, Austria, galleryImages=[], \n", "\t_id=5f984a5592723201a3525dbd\n", "\temail=astrid.herrera@bitpanda.com\n", "\tupdatedAt=2020-10-27T16:32:57.331Z\n", "key=2151\n", "\tcreatedAt=2021-01-13T14:29:59.814Z\n", "\tjobs=null\n", "\tnumberOfJobs=0\n", "\tcompany={createdAt=2021-01-13T14:30:10.995Z, testimonials=[], paymentTypeOrdered=free, isPublished=true, companyName=LIST, _id=5fff03f2975ca80e320d4442, companyLocation=Luxembourg, galleryImages=[], paymentTy\n", "\t_id=5fff03e7975ca80e320d443f\n", "\temail=aldjia.afiri@list.lu\n", "\tupdatedAt=2021-01-20T09:34:16.358Z\n", "key=2152\n", "\tcreatedAt=2021-01-19T19:46:27.740Z\n", "\tjobs=null\n", "\tnumberOfJobs=1\n", "\tcompany={paymentTypeOrdered=free, isPublished=true, companyName=ONERA, about=

Office national d’études et de recherches aérospatiales (ONERA) laureateNamesList = new ArrayList();\n", " List> laureates = (List>)\n", " ((HashMap)binval).get(\"laureates\");\n", " if (laureates == null) {\n", " return;\n", " }\n", " for (HashMap laureate: laureates) {\n", " //System.out.format(\"firstname: %s, surname: %s\\n\", laureate.get(\"firstname\"), \n", " // laureate.get(\"surname\"));\n", " laureateNamesList.add(laureate.getOrDefault(\"firstname\",\"\").toLowerCase() + \" \" + \n", " laureate.getOrDefault(\"surname\",\"\").toLowerCase());\n", " }\n", " //System.out.println(laureateNamesList);\n", " Bin laureateNamesBin = new Bin(\"laureate_names\", laureateNamesList);\n", " client.put(null, key, laureateNamesBin);\n", " }\n", "}\n", "// expression filter 1001 <= user-key < 2000 is specifed in the policy\n", "ScanPolicy policy = new ScanPolicy();\n", "policy.filterExp = Exp.build(\n", " Exp.and(\n", " Exp.ge(Exp.key(Type.INT), Exp.val(1001)),\n", " Exp.lt(Exp.key(Type.INT), Exp.val(2000))));\n", "System.out.format(\"Scan with expression filter results:\\n\");\n", "client.scanAll(policy, Namespace, Set, new ScanParallel(), JsonBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Find nobel prizes by winner name." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created index idx_laureate_names_list_str on ns=test set=cdt-indexing bin=laureate_names.\n", "Prizes with laureate name \"marie curie\":\n", "key=1647\n", "\tcategory=physics\n", "\tyear=1903\n", "\tlaureates=[{firstname=Henri, share=2, id=4, surname=Becquerel, motivation=\"in recognition of the extraordinary services he has rendered by his discovery of spontaneous radioactivity\"}, {firstname=Pierre, share=\n", "key=1604\n", "\tcategory=chemistry\n", "\tyear=1911\n", "\tlaureates=[{firstname=Marie, share=1, id=6, surname=Curie, motivation=\"in recognition of her services to the advancement of chemistry by the discovery of the elements radium and polonium, by the isolation of ra\n" ] } ], "source": [ "// Create an index on the numberOfJobs field.\n", "// idx_laureate_names_list_str\n", "createIndex(\"idx_laureate_names_list_str\", \"laureate_names\", IndexType.STRING, IndexCollectionType.LIST);\n", "\n", "// Find nobel prizes by winner name \"albert einstein\".\n", "System.out.println(\"Prizes with laureate name \\\"marie curie\\\":\");\n", "Filter filter = Filter.contains(\"laureate_names\", IndexCollectionType.LIST, \"marie curie\");\n", "executeQueryAndPrintResults(filter, JsonBin);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Clean up\n", "Remove tutorial data and close connection." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2020-12-29T20:49:19.972650Z", "start_time": "2020-12-29T20:49:19.967344Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Removed tutorial data and closed server connection.\n" ] } ], "source": [ "client.truncate(null, Namespace, null, null);\n", "client.close();\n", "System.out.println(\"Removed tutorial data and closed server connection.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Takeaways\n", "The tutorial illustrated with many examples the secondary index functionality with CDTs that is available in Aerospike Database 6.1.0.0+. The CDT indexing enables faster queries on JSON documents and other complex objects stored as CDTs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Further Exploration and Resources\n", "Here are some links for further exploration\n", "\n", "Resources\n", "- [Query JSON Documents Faster (and More) with CDT Indexing](https://developer.aerospike.com/blog/query-json-documents-faster)\n", "- [Aerospike Developer Hub](https://developer.aerospike.com)\n" ] }, { "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": 4 }