{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "
\n",
" /**\n",
" * Retrieve an object matched by JSON path.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binName name of a bin storing json.\n",
" * @param jsonPath JSON path matching the required elements.\n",
" * @return object matched by jsonPath.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" Object get(Key key, String binName, String jsonPath);\n",
"\n",
" /**\n",
" * Retrieve a map of objects matched by JSON path.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binNames names of bins storing json (all bins with the same document structure).\n",
" * @param jsonPath JSON path matching the required elements.\n",
" * @return A map of objects matched by jsonPath with bin names as keys.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" Map get(Key key, Collection binNames, String jsonPath);\n",
"\n",
" /**\n",
" * Put a JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binName name of a bin to store json.\n",
" * @param jsonObject JSON object (document) to put.\n",
" */\n",
" void put(Key key, String binName, JsonNode jsonObject);\n",
"\n",
" /**\n",
" * Put an object at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binName name of a bin storing json.\n",
" * @param jsonPath A JSON path to put the given JSON object in.\n",
" * @param object An object to put in the given JSON path.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void put(Key key, String binName, String jsonPath, Object object);\n",
"\n",
" /**\n",
" * Put an object at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binNames names of bins storing json (all bins with the same document structure).\n",
" * @param jsonPath JSON path to put the given JSON object in.\n",
" * @param object the object to be put at the given JSON path.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void put(Key key, Collection binNames, String jsonPath, Object object);\n",
"\n",
" /**\n",
" * Append an object to a collection at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binName name of a bin storing json.\n",
" * @param jsonPath JSON path that includes a collection to append the given JSON object to.\n",
" * @param object the object to be appended at the given JSON path.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void append(Key key, String binName, String jsonPath, Object object);\n",
"\n",
" /**\n",
" * Append an object to a collection at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binNames names of bins storing json (all bins with the same document structure).\n",
" * @param jsonPath JSON path that includes a collection to append the given JSON object to.\n",
" * @param object the object to be appended at the given JSON path.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void append(Key key, Collection binNames, String jsonPath, Object object);\n",
"\n",
" /**\n",
" * Delete an object at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binName name of a bin storing json.\n",
" * @param jsonPath JSON path for the object deletion.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void delete(Key key, String binName, String jsonPath);\n",
"\n",
" /**\n",
" * Delete an object at a particular path in JSON document.\n",
" *\n",
" * @param key Aerospike Key.\n",
" * @param binNames names of bins storing json (all bins with the same document structure).\n",
" * @param jsonPath JSON path for the object deletion.\n",
" * @throws DocumentApiException if there was an error.\n",
" */\n",
" void delete(Key key, Collection binNames, String jsonPath);\n",
"\n",
" /**\n",
" * Perform batch operations.\n",
" *\n",
" * Operations order is preserved only for those 1-step operations\n",
" * (with JSONPath that contains only array and/or map elements)\n",
" * that have unique Aerospike keys within a batch.
\n",
" * Every 2-step operation (with JSONPath containing wildcards, recursive descent, filters, functions, scripts)\n",
" * should have unique Aerospike key within a batch.\n",
" *\n",
" * @param batchOperations a list of batch operations to apply.\n",
" * @param parallel whether batch processing stream operations should run in parallel.\n",
" * @return a list of corresponding {@link BatchRecord} results.\n",
" * @throws DocumentApiException if there was an error.\n",
" * @throws IllegalArgumentException if the batch has multiple two-step operations with the same key.\n",
" */\n",
" List batchPerform(List batchOperations, boolean parallel);\n",
"}\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example JSON Document\n",
"The example JSON document used is stored in file `doc_api_example_tommyleejones.json`, and has this json:\n",
"\n",
"\n",
"{\n",
" \"forenames\": [\n",
" \"Tommy\",\n",
" \"Lee\"\n",
" ],\n",
" \"surname\": \"Jones\",\n",
" \"date_of_birth\": {\n",
" \"day\": 15,\n",
" \"month\": 9,\n",
" \"year\": 1946\n",
" },\n",
" \"selected_filmography\":{\n",
" \"2012\":[\"Lincoln\",\"Men In Black 3\"],\n",
" \"2007\":[\"No Country For Old Men\"],\n",
" \"2002\":[\"Men in Black 2\"],\n",
" \"1997\":[\"Men in Black\",\"Volcano\"],\n",
" \"1994\":[\"Natural Born Killers\",\"Cobb\"],\n",
" \"1991\":[\"JFK\"],\n",
" \"1980\":[\"Coal Miner's Daughter\",\"Barn Burning\"]\n",
" },\n",
" \"imdb_rank\":{\n",
" \"source\":\"https://www.imdb.com/list/ls050274118/\",\n",
" \"rank\":51\n",
" },\n",
" \"best_films_ranked\": [\n",
" {\n",
" \"source\": \"http://www.rottentomatoes.com\",\n",
" \"films\": [\"The Fugitive\",\"No Country For Old Men\",\"Men In Black\",\"Coal Miner's Daughter\",\"Lincoln\"]\n",
" },\n",
" {\n",
" \"source\":\"https://medium.com/the-greatest-films-according-to-me/10-greatest-films-of-tommy-lee-jones-97426103e3d6\",\n",
" \"films\":[\"The Three Burials of Melquiades Estrada\",\"The Homesman\",\"No Country for Old Men\",\"In the Valley of Elah\",\"Coal Miner's Daughter\"]\n",
" }\n",
" ]\n",
"}\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Store JSON Document"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iniialize Document Client\n",
"Create a document client using an Aerospike client."
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized Aerospike client and connected to the cluster.\n",
"Initialized document client from the Aerospike client.\n"
]
}
],
"source": [
"final String NAMESPACE = \"test\";\n",
"final String SET = \"document-api\";\n",
"\n",
"AerospikeClient aerospikeClient = new AerospikeClient(\"localhost\", 3000);\n",
"System.out.println(\"Initialized Aerospike client and connected to the cluster.\");\n",
"AerospikeDocumentClient documentClient = new AerospikeDocumentClient(aerospikeClient);\n",
"System.out.println(\"Initialized document client from the Aerospike client.\");;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read JSON Document\n",
"Using the convenience function `readJSONFromAFile` defined above, read a json file."
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read JSON doc from file.\n",
"JSON doc: {\"forenames\":[\"Tommy\",\"Lee\"],\"surname\":\"Jones\",\"date_of_birth\":{\"day\":15,\"month\":9,\"year\":1946},\"selected_filmography\":{\"2012\":[\"Lincoln\",\"Men In Black 3\"],\"2007\":[\"No Country For Old Men\"],\"2002\":[\"Men in Black 2\"],\"1997\":[\"Men in Black\",\"Volcano\"],\"1994\":[\"Natural Born Killers\",\"Cobb\"],\"1991\":[\"JFK\"],\"1980\":[\"Coal Miner's Daughter\",\"Barn Burning\"]},\"imdb_rank\":{\"source\":\"https://www.imdb.com/list/ls050274118/\",\"rank\":51},\"best_films_ranked\":[{\"source\":\"http://www.rottentomatoes.com\",\"films\":[\"The Fugitive\",\"No Country For Old Men\",\"Men In Black\",\"Coal Miner's Daughter\",\"Lincoln\"]},{\"source\":\"https://medium.com/the-greatest-films-according-to-me/10-greatest-films-of-tommy-lee-jones-97426103e3d6\",\"films\":[\"The Three Burials of Melquiades Estrada\",\"The Homesman\",\"No Country for Old Men\",\"In the Valley of Elah\",\"Coal Miner's Daughter\"]}]}\n"
]
}
],
"source": [
"// Read the json document into a string.\n",
"String jsonString = readJSONFromAFile(\"doc_api_example_tommyleejones.json\");\n",
"System.out.println(\"Read JSON doc from file.\");\n",
"\n",
"// Convert JSON string to a JsonNode\n",
"JsonNode jsonNode = JsonConverters.convertStringToJsonNode(jsonString);\n",
"System.out.format(\"JSON doc: %s\\n\", jsonNode);;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add JSON Document to Database"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stored JSON doc in database.\n"
]
}
],
"source": [
"// Construct a key\n",
"Key tommyLeeJonesDBKey = new Key(NAMESPACE, SET, \"tommy-lee-jones.json\");\n",
"\n",
"// Add the document to database\n",
"String documentBinName = \"documentBin\";\n",
"documentClient.put(tommyLeeJonesDBKey, documentBinName, jsonNode);\n",
"System.out.println(\"Stored JSON doc in database.\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"The Fugitive"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// indexed array elements\n",
"documentClient.get(tommyLeeJonesDBKey, documentBinName, \"$.best_films_ranked[0].films[0]\" );"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Coal Miner's Daughter, Barn Burning]"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// an array element denoted by a simple path\n",
"documentClient.get(tommyLeeJonesDBKey, documentBinName, \"$.selected_filmography.1980\");"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1997=[Men in Black, Volcano], 2007=[No Country For Old Men], 1994=[Natural Born Killers, Cobb], 2002=[Men in Black 2], 1991=[JFK], 1980=[Coal Miner's Daughter, Barn Burning], 2012=[Lincoln, Men In Black 3]}"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// complex json path: wild-card\n",
"documentClient.get(tommyLeeJonesDBKey, documentBinName, \"$.selected_filmography.*\");"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"http:\\/\\/www.rottentomatoes.com\",\"https:\\/\\/medium.com\\/the-greatest-films-according-to-me\\/10-greatest-films-of-tommy-lee-jones-97426103e3d6\",\"https:\\/\\/www.imdb.com\\/list\\/ls050274118\\/\"]"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// complex json path: matching elements in a subtree\n",
"documentClient.get(tommyLeeJonesDBKey, documentBinName, \"$..source\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Ad Astra]"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// add year 2019 and films\n",
"List\n",
"{\n",
" \"store\": {\n",
" \"book\": [\n",
" {\n",
" \"category\": \"reference\",\n",
" \"author\": \"Nigel Rees\",\n",
" \"title\": \"Sayings of the Century\",\n",
" \"price\": 8.95,\n",
" \"ref\": [1,2]\n",
" },\n",
" {\n",
" \"category\": \"fiction\",\n",
" \"author\": \"Evelyn Waugh\",\n",
" \"title\": \"Sword of Honour\",\n",
" \"price\": 12.99,\n",
" \"ref\": [2,4,16]\n",
" },\n",
" {\n",
" \"category\": \"fiction\",\n",
" \"author\": \"Herman Melville\",\n",
" \"title\": \"Moby Dick\",\n",
" \"isbn\": \"0-553-21311-3\",\n",
" \"price\": 8.99,\n",
" \"ref\": [1,3,5]\n",
" },\n",
" {\n",
" \"category\": \"fiction\",\n",
" \"author\": \"J. R. R. Tolkien\",\n",
" \"title\": \"The Lord of the Rings\",\n",
" \"isbn\": \"0-395-19395-8\",\n",
" \"price\": 22.99,\n",
" \"ref\": [1,2,7]\n",
" }\n",
" ],\n",
" \"bicycle\": {\n",
" \"color\": \"red\",\n",
" \"price\": 19.95\n",
" }\n",
" },\n",
" \"expensive\": 10\n",
"}\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Store JSON Document\n",
"#### Read JSON Document from File"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read JSON doc from file.\n"
]
}
],
"source": [
"String jsonString = readJSONFromAFile(\"doc_api_example_store.json\");\n",
"System.out.println(\"Read JSON doc from file.\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Add JSON Document to Database"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95,\"ref\":[1,2]},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99,\"ref\":[2,4,16]},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99,\"ref\":[1,3,5]},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99,\"ref\":[1,2,7]}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}\n",
"Stored JSON doc to database.\n"
]
}
],
"source": [
"// Store the JSON doc in the database and read it back.\n",
"\n",
"// Create a document client via an existing aerospikeClient\n",
"AerospikeDocumentClient documentClient = new AerospikeDocumentClient(aerospikeClient);\n",
"\n",
"// Convert JSON string to a JsonNode\n",
"JsonNode jsonNode = JsonConverters.convertStringToJsonNode(jsonString);\n",
"System.out.println(jsonNode);\n",
"\n",
"// Construct an appropriate key\n",
"Key documentKey = new Key(NAMESPACE, SET, \"jsonExampleKey\");\n",
"\n",
"String documentBinName = \"documentBin\";\n",
"// Add to database\n",
"documentClient.put(documentKey, documentBinName, jsonNode);\n",
"System.out.println(\"Stored JSON doc to database.\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query All Subnodes"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{bicycle={color=red, price=19.95}, book=[{ref=[1, 2], category=reference, title=Sayings of the Century, author=Nigel Rees, price=8.95}, {ref=[2, 4, 16], category=fiction, title=Sword of Honour, author=Evelyn Waugh, price=12.99}, {ref=[1, 3, 5], category=fiction, title=Moby Dick, author=Herman Melville, price=8.99, isbn=0-553-21311-3}, {ref=[1, 2, 7], category=fiction, title=The Lord of the Rings, author=J. R. R. Tolkien, price=22.99, isbn=0-395-19395-8}]}\n"
]
}
],
"source": [
"// Get all products, both books and bicycles\n",
"String jsonPath = \"$.store.*\";\n",
"Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query Specific Field"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\"Nigel Rees\",\"Evelyn Waugh\",\"Herman Melville\",\"J. R. R. Tolkien\"]\n"
]
}
],
"source": [
"// Get the authors of all books\n",
"String jsonPath = \"$.store.book[*].author\";\n",
"Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modify Field"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\"J.K. Rowling\",\"J.K. Rowling\",\"J.K. Rowling\",\"J.K. Rowling\"]\n"
]
}
],
"source": [
"// 3. Modify the authors of all books to “J.K. Rowling”\n",
"// Get the authors of all books\n",
"String jsonPath = \"$.store.book[*].author\";\n",
"String jsonObject = \"J.K. Rowling\";\n",
"// Modify the authors of all books to \"J.K. Rowling\"\n",
"documentClient.put(documentKey, documentBinName, jsonPath, jsonObject);\n",
"Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query with Exists Predicate"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"J.K. Rowling\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"},{\"ref\":[1,2,7],\"category\":\"fiction\",\"title\":\"The Lord of the Rings\",\"author\":\"J.K. Rowling\",\"price\":22.99,\"isbn\":\"0-395-19395-8\"}]\n"
]
}
],
"source": [
"// 4. Get all the books that have an ISBN number\n",
"jsonPath = \"$..book[?(@.isbn)]\";\n",
"objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query with Boolean Predicate"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"J.K. Rowling\",\"price\":8.95},{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"J.K. Rowling\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"}]\n"
]
}
],
"source": [
"// 5. Get all the books in store cheaper than 10\n",
"jsonPath = \"$.store.book[?(@.price < 10)]\";\n",
"objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query with RegEx"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"J.K. Rowling\",\"price\":8.95},{\"ref\":[2,4,16],\"category\":\"fiction\",\"title\":\"Sword of Honour\",\"author\":\"J.K. Rowling\",\"price\":12.99},{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"J.K. Rowling\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"},{\"ref\":[1,2,7],\"category\":\"fiction\",\"title\":\"The Lord of the Rings\",\"author\":\"J.K. Rowling\",\"price\":22.99,\"isbn\":\"0-395-19395-8\"}]\n"
]
}
],
"source": [
"// 6. Get all the books matching regex (ignore case)\n",
"jsonPath = \"$..book[?(@.author =~ /.*ROWLING/i)]\";\n",
"objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.println(objectFromDB);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delete Field"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before delete: [19.95,8.95,12.99,8.99,22.99]\n",
"After delete: []\n"
]
}
],
"source": [
"// 7. Delete the price field of every object exists in store\n",
"// Get the price of everything\n",
"String jsonPath = \"$.store..price\";\n",
"Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.format(\"Before delete: %s\\n\", objectFromDB);\n",
"// Delete the price field of every object exists in the store\n",
"documentClient.delete(documentKey, documentBinName, jsonPath);\n",
"Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);\n",
"System.out.format(\"After delete: %s\\n\", objectFromDB);;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### More Query and Transformation Examples\n",
"Consider the document: \n",
"```\n",
" {arr=[{a=1, b=10}, \n",
" {a=2, b=20}], \n",
" id=4, \n",
" obs={\n",
" c=[100, 200], \n",
" id=x}\n",
" }\n",
"```\n",
"\n",
"JSON queries:\n",
"```\n",
"\"$.arr[*].a\" -> \n",
" [1, 2]\n",
" \n",
"\"$.obs.c[\\*]\" -> \n",
" [100, 200]\n",
" \n",
"\"$..id\" -> \n",
" [4, x]\n",
" \n",
"\"$..c.length()\" -> \n",
" 2\n",
"```\n",
"\n",
"Transformations using JSON queries:\n",
"```\n",
"put(“$.arr[*].a”, 0) -> \n",
" {arr=[{a=0, b=10}, {a=0, b=20}], id=4, obs={c=[100, 200], id=x}}\n",
" \n",
"put(“$.obj.c[\\*]”, 0) -> \n",
" {arr=[{a=0, b=10}, {a=0, b=20}], id=4, obs={c=[0, 0], id=x}}\n",
" \n",
"put(“$..id”, 0) -> \n",
" {arr=[{a=0, b=10}, {a=0, b=20}], id=0, obs={c=[0, 0], id=0}}\n",
" \n",
"put)”$..a”, 1) -> \n",
" {arr=[{a=1, b=10}, {a=1, b=20}], id=0, obs={c=[0, 0], id=0}}\n",
" \n",
"put(\"$..c\", List.of(11, 111)) -> \n",
" {arr=[{a=1, b=10}, {a=1, b=20}], id=0, obs={c=[11, 111], id=0}}\n",
" \n",
"append(\"$.obs.c\", 0) -> \n",
" arr=[{a=1, b=10}, {a=1, b=20}], id=0, obs={c=[11, 111, 0], id=0}}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More JSONPath Capabilities and Examples\n",
"Restore the original `store` document for the following examples."
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{expensive=10, store={bicycle={color=red, price=19.95}, book=[{ref=[1, 2], category=reference, title=Sayings of the Century, author=Nigel Rees, price=8.95}, {ref=[2, 4, 16], category=fiction, title=Sword of Honour, author=Evelyn Waugh, price=12.99}, {ref=[1, 3, 5], category=fiction, title=Moby Dick, author=Herman Melville, price=8.99, isbn=0-553-21311-3}, {ref=[1, 2, 7], category=fiction, title=The Lord of the Rings, author=J. R. R. Tolkien, price=22.99, isbn=0-395-19395-8}]}}"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documentClient.put(documentKey, documentBinName, jsonNode);\n",
"documentClient.get(documentKey, documentBinName, \"$\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions\n",
"JSONPath has functions `min`, `sum`, `size`, `length`, etc. See the [JSONPath repo](https://github.com/json-path/JsonPath_) for a full list."
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// length(): find number of references to Moby Dick\n",
"jsonPath = \"$..store.book[?(@.title == 'Moby Dick')].ref.length()\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13.48"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// avg(): find average price of books\n",
"jsonPath = \"$..book..price.avg()\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filter Operators\n",
"Operators like `in` and `anyof` are supported. See the [JSONPath repo](https://github.com/json-path/JsonPath) for a full list."
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"Nigel Rees\",\"price\":8.95}]"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// in: books with category in ['reference', 'biography']\n",
"jsonPath = \"$..book[?(@.category in ['reference', 'biography'])]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"Nigel Rees\",\"price\":8.95},{\"ref\":[2,4,16],\"category\":\"fiction\",\"title\":\"Sword of Honour\",\"author\":\"Evelyn Waugh\",\"price\":12.99},{\"ref\":[1,2,7],\"category\":\"fiction\",\"title\":\"The Lord of the Rings\",\"author\":\"J. R. R. Tolkien\",\"price\":22.99,\"isbn\":\"0-395-19395-8\"}]"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// in: books that have 2 in their reference list\n",
"jsonPath = \"$..book[?(2 in @.ref)]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"Nigel Rees\",\"price\":8.95},{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"Herman Melville\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"},{\"ref\":[1,2,7],\"category\":\"fiction\",\"title\":\"The Lord of the Rings\",\"author\":\"J. R. R. Tolkien\",\"price\":22.99,\"isbn\":\"0-395-19395-8\"}]"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// anyof: books whose ref list has any of 1, 3, 7, or 100.\n",
"jsonPath = \"$..book[?(@.ref anyof [1, 3, 7, 100])]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Predicates"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[2,4,16],\"category\":\"fiction\",\"title\":\"Sword of Honour\",\"author\":\"Evelyn Waugh\",\"price\":12.99}]"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// books with price between 10 and 20\n",
"jsonPath = \"$..book[?(@.price > 10 && @.price < 20)]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[1,2],\"category\":\"reference\",\"title\":\"Sayings of the Century\",\"author\":\"Nigel Rees\",\"price\":8.95},{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"Herman Melville\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"}]"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// comparing with another element\n",
"jsonPath = \"$..book[?(@.price < $.expensive)]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{\"ref\":[1,3,5],\"category\":\"fiction\",\"title\":\"Moby Dick\",\"author\":\"Herman Melville\",\"price\":8.99,\"isbn\":\"0-553-21311-3\"}]"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// composable conditions: books in fiction category with isbn and priced less than 10\n",
"jsonPath = \"$..book[?(@.category == 'fiction' && @.isbn && @.price < 10 )]\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try Your Own Path"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{expensive=10, store={bicycle={color=red, price=19.95}, book=[{ref=[1, 2], category=reference, title=Sayings of the Century, author=Nigel Rees, price=8.95}, {ref=[2, 4, 16], category=fiction, title=Sword of Honour, author=Evelyn Waugh, price=12.99}, {ref=[1, 3, 5], category=fiction, title=Moby Dick, author=Herman Melville, price=8.99, isbn=0-553-21311-3}, {ref=[1, 2, 7], category=fiction, title=The Lord of the Rings, author=J. R. R. Tolkien, price=22.99, isbn=0-395-19395-8}]}}"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// substitute your json path\n",
"jsonPath = \"$\";\n",
"documentClient.get(documentKey, documentBinName, jsonPath );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cleaning Up\n",
"Remove tutorial data and close connection."
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
"//truncateTestData();\n",
"//aerospikeClient.close();\n",
"//System.out.println(\"Removed tutorial data and closed server connection.\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Further Exploration and Resources\n",
"Here are some links for further exploration.\n",
"\n",
"## Resources\n",
"- Blog posts\n",
" - [Aerospike Document API](https://medium.com/aerospike-developer-blog/aerospike-document-api-fd8870b4106c)\n",
" - [Aerospike Document API: JSONPath Queries](https://medium.com/aerospike-developer-blog/aerospike-document-api-jsonpath-queries-bd6260b2d076)\n",
"- Related notebooks\n",
" - [Working with Lists](java-working_with_lists.ipynb)\n",
" - [Working with Maps](java-working_with_maps.ipynb)\n",
" - [Implementing SQL Operations: SELECT](sql_select.ipynb), \n",
" - [Implementing SQL Operations: CREATE, UPDATE, DELETE](sql_updates.ipynb)\n",
"- Aerospike Developer Hub\n",
" - [Java Developers Resources](https://developer.aerospike.com/java-developers)\n",
"- Github repos\n",
" - [Aerospike Document API Library](https://github.com/aerospike/aerospike-document-lib)\n",
" - [Jayway JSONPath](https://github.com/json-path/JsonPath)\n",
" - [Java code examples](https://github.com/aerospike/aerospike-client-java/tree/master/examples/src/com/aerospike/examples)\n",
" - [Java Client](https://www.aerospike.com/docs/client/java/index.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploring Other Notebooks\n",
"\n",
"Visit [Aerospike notebooks repo](https://github.com/aerospike/aerospike-dev-notebooks.docker) 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 in the notebook menu, and select Upload."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "11.0.8+10-LTS"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}