Skip to main content

Aerospike Document API for JSON Documents

For an interactive Jupyter notebook experience: Launch in Binder#

This notebook illustrates the Aerospike Document API, which is used to store a JSON document in Aerospike database and perform operations on a stored JSON document, with code examples.

This notebook requires Aerospike database running on localhost. Visit Aerospike notebooks repo for additional details and the docker container.

Introduction#

The Document API is introduced in blog posts this and this.

This goal of this notebook is to demonstrate the examples from these blog posts and also to allow easy experimentation with Document API and JSONPath capabilities.

The main topics in this notebook include:

  • Document API methods and code examples
  • JSONPath overview and code examples
  • Additional JSONPath capabilties and code examples

Prerequisites#

This tutorial assumes familiarity with the following topics:

Setup#

Ensure database is running#

This notebook requires that Aerospike datbase is running.

import io.github.spencerpark.ijava.IJava;import io.github.spencerpark.jupyter.kernel.magic.common.Shell;IJava.getKernelInstance().getMagics().registerMagics(Shell.class);%sh asd

Download and Install Additional Components#

Aerospike Java client 5.1.3 and the document api library.

%%loadFromPOM<dependencies>  <dependency>    <groupId>com.aerospike</groupId>    <artifactId>aerospike-client</artifactId>    <version>5.1.3</version>  </dependency>  <dependency>  <groupId>com.aerospike</groupId>  <artifactId>aerospike-document-api</artifactId>  <version>1.0.0</version></dependency></dependencies>

Define Convenience Functions#

Define convenience functions readJSONFromAFile and truncateTestData.

import java.io.IOException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Paths;import java.util.stream.Stream;
// convenience function to read JSON doc from a fileString readJSONFromAFile(String filePath) throws IOException {    StringBuilder contentBuilder = new StringBuilder();    Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8);    stream.forEach(s -> contentBuilder.append(s).append("\n"));    return contentBuilder.toString();}
import com.aerospike.client.AerospikeException;
// convenience function to truncate test datavoid truncateTestData() {    try {        aerospikeClient.truncate(null, NAMESPACE, SET, null);    }    catch (AerospikeException e) {        // ignore    }}

Document API#

The Aerospike Document API provides CRUD operations at arbitrary points within a JSON document as described in this blog post.

Interface#

The document API is as represented in the interface IAerospikeDocumentClient, which is shown below for convenience. The API is fairly simple and has get, put, append, and delete methods, and yet is powerful as it allows a JSONPath argument to specify parts of the document simply and expressively to apply these methods.

public interface IAerospikeDocumentClient {
    /**     * Retrieve the object in the document with key documentKey that is referenced by the JSON path.     *     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path to get the reference from.     * returns Object referenced by jsonPath.     */    Object get(Key documentKey, String documentBinName, String jsonPath)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Retrieve the object in the document with key documentKey that is referenced by the JSON path.     *     * readPolicy  An Aerospike read policy to use for the get() operation.     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path to get the reference from.     * returns Object referenced by jsonPath.     */    Object get(Policy readPolicy, Key documentKey, String documentBinName, String jsonPath)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Put a document.     *     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonObject  A JSON object to put.     */    void put(Key documentKey, String documentBinName, JsonNode jsonObject);
    /**     * Put a document.     *     * writePolicy An Aerospike write policy to use for the put() operation.     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonObject  A JSON object to put.     */    void put(WritePolicy writePolicy, Key documentKey, String documentBinName, JsonNode jsonObject);
    /**     * Put a map representation of a JSON object at a particular path in a JSON document.     *     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path to put the given JSON object in.     * jsonObject  A JSON object to put in the given JSON path.     */    void put(Key documentKey, String documentBinName, String jsonPath, Object jsonObject)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Put a map representation of a JSON object at a particular path in a JSON document.     *     * writePolicy An Aerospike write policy to use for the put() and operate() operations.     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path to put the given JSON object in.     * jsonObject  A JSON object to put in the given JSON path.     */    void put(WritePolicy writePolicy, Key documentKey, String documentBinName, String jsonPath, Object jsonObject)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Append an object to a list in a document specified by a JSON path.     *     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path that includes a list to append the given JSON object to.     * jsonObject  A JSON object to append to the list at the given JSON path.     */    void append(Key documentKey, String documentBinName, String jsonPath, Object jsonObject)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Append an object to a list in a document specified by a JSON path.     *     * writePolicy An Aerospike write policy to use for the operate() operation.     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path that includes a list to append the given JSON object to.     * jsonObject  A JSON object to append to the list at the given JSON path.     */    void append(WritePolicy writePolicy, Key documentKey, String documentBinName, String jsonPath, Object jsonObject)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Delete an object in a document specified by a JSON path.     *     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path for the object deletion.     */    void delete(Key documentKey, String documentBinName, String jsonPath)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;
    /**     * Delete an object in a document specified by a JSON path.     *     * writePolicy An Aerospike write policy to use for the operate() operation.     * documentKey An Aerospike Key.     * documentBinName The bin name that will store the json.     * jsonPath    A JSON path for the object deletion.     */    void delete(WritePolicy writePolicy, Key documentKey, String documentBinName, String jsonPath)            throws JsonPathParser.JsonParseException, DocumentApiException, JsonProcessingException;

Code Examples#

The code examples from the blog post Aerospike Document API are shown below.

Example JSON Document#

The example JSON document used is stored in file doc_api_example_tommyleejones.json, and has this json:

{  "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"]    }  ]}

Store JSON Document#

Iniialize Document Client#

Create a document client using an Aerospike client.

import com.aerospike.client.AerospikeClient;import com.aerospike.documentapi.*;
final String NAMESPACE = "test";final String SET = "document-api";
AerospikeClient aerospikeClient = new AerospikeClient("localhost", 3000);System.out.println("Initialized Aerospike client and connected to the cluster.");AerospikeDocumentClient documentClient = new AerospikeDocumentClient(aerospikeClient);System.out.println("Initialized document client from the Aerospike client.");;

Output:

Initialized Aerospike client and connected to the cluster.Initialized document client from the Aerospike client.

Read JSON Document#

Using the convenience function readJSONFromAFile defined above, read a json file.

import com.fasterxml.jackson.databind.JsonNode;import com.aerospike.documentapi.JsonConverters;
// Read the json document into a string.String jsonString = readJSONFromAFile("doc_api_example_tommyleejones.json");System.out.println("Read JSON doc from file.");
// Convert JSON string to a JsonNodeJsonNode jsonNode = JsonConverters.convertStringToJsonNode(jsonString);System.out.format("JSON doc: %s\n", jsonNode);;

Output:

Read JSON doc from file.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"]}]}

Add JSON Document to Database#

import com.aerospike.client.Key;import com.jayway.jsonpath.JsonPath;
// Construct a keyKey tommyLeeJonesDBKey = new Key(NAMESPACE, SET, "tommy-lee-jones.json");
// Add the document to databaseString documentBinName = "documentBin";documentClient.put(tommyLeeJonesDBKey, documentBinName, jsonNode);System.out.println("Stored JSON doc in database.");

Output:

Stored JSON doc in database.

Read#

// indexed array elementsdocumentClient.get(tommyLeeJonesDBKey, documentBinName, "$.best_films_ranked[0].films[0]" );

Output:

The Fugitive
// complex component denoted by a pathdocumentClient.get(tommyLeeJonesDBKey, documentBinName, "$.selected_filmography.1980");

Output:

[Coal Miner's Daughter, Barn Burning]

Update#

// add year 2019 and filmsList<String> _2019Films = new Vector<String>();_2019Films.add("Ad Astra");documentClient.put(tommyLeeJonesDBKey, documentBinName, "$.selected_filmography.2019", _2019Films);// read the updatedocumentClient.get(tommyLeeJonesDBKey, documentBinName, "$.selected_filmography.2019" );

Output:

[Ad Astra]
// update the imdb rankdocumentClient.put(tommyLeeJonesDBKey, documentBinName, "$.imdb_rank.rank", 45);// read the updatedocumentClient.get(tommyLeeJonesDBKey, documentBinName, "$.imdb_rank.rank" );

Output:

45
// append films to best_films_rankeddocumentClient.append(tommyLeeJonesDBKey, documentBinName, "$.best_films_ranked[0].films", "Rolling Thunder");documentClient.append(tommyLeeJonesDBKey, documentBinName, "$.best_films_ranked[0].films", "The Three Burials");// read the updatesdocumentClient.get(tommyLeeJonesDBKey, documentBinName, "$.best_films_ranked[0].films" );

Output:

[The Fugitive, No Country For Old Men, Men In Black, Coal Miner's Daughter, Lincoln, Rolling Thunder, The Three Burials]

Delete#

// print state before deletionObject docObject = documentClient.get(tommyLeeJonesDBKey, documentBinName, "$");System.out.format("Before delete: \n%s\n", docObject);// delete all descendants of the document rootdocumentClient.delete(tommyLeeJonesDBKey, documentBinName, "$..*");// print state after deletiondocObject = documentClient.get(tommyLeeJonesDBKey, documentBinName, "$");System.out.format("After delete: \n%s\n", docObject);;

Output:

Before delete: {imdb_rank={rank=45, source=https://www.imdb.com/list/ls050274118/}, forenames=[Tommy, Lee], surname=Jones, date_of_birth={month=9, day=15, year=1946}, selected_filmography={1997=[Men in Black, Volcano], 2019=[Ad Astra], 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]}, best_films_ranked=[{films=[The Fugitive, No Country For Old Men, Men In Black, Coal Miner's Daughter, Lincoln, Rolling Thunder, The Three Burials], source=http://www.rottentomatoes.com}, {films=[The Three Burials of Melquiades Estrada, The Homesman, No Country for Old Men, In the Valley of Elah, Coal Miner's Daughter], source=https://medium.com/the-greatest-films-according-to-me/10-greatest-films-of-tommy-lee-jones-97426103e3d6}]}After delete: {}

JSONPath Queries#

Please refer to this introduction to JSONPath: JSONPath, XPATH for JSON.

Syntax#

The following table summarizes its syntax.

JSONPathDescription
\$the root object/element
@the current object/element
. or []child operator
..recursive descent
*wildcard. All objects/elements regardless their names
[]subscript operator in array
[,]alternate names or array indices as a set
[start🔚step]array slice operator
[?(<expression>{=html})]boolean filter expression
(<expression>{=html})script expression

Use Examples#

The following table summarizes its key uses.

JSONPathResult
$.store.book[*].authorthe authors of all books in the store
$..authorall authors
$.store.*all things in store, which are some books and a red bicycle
$.store..pricethe price of everything in the store
$..book[2]the third book
$..book[(@.length-1)]
$..book[-1:]the last book in order
$..book[0,1]
$..book[:2]the first two books
$..book[?(@.isbn)]filter all books with isbn number
$..book[?(@.price\<10)]filter all books cheapier than 10
$..*all members of JSON structure
$..book.length()the number of books

Code Examples#

Below are the code examples from the blog post Aerospike Document API: JSONPath Queries.

Example JSON Document#

The example JSON document it uses is stored in file doc_api_example_store.json, and has this json:

{ "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}

Store JSON Document#

Read JSON Document from File#

String jsonString = readJSONFromAFile("doc_api_example_store.json");System.out.println("Read JSON doc from file.");

Output:

Read JSON doc from file.

Add JSON Document to Database#

// Store the JSON doc in the database and read it back.import com.aerospike.client.Key;import com.aerospike.documentapi.*;import com.fasterxml.jackson.databind.JsonNode;import com.jayway.jsonpath.JsonPath;
// Create a document client via an existing aerospikeClientAerospikeDocumentClient documentClient = new AerospikeDocumentClient(aerospikeClient);
// Convert JSON string to a JsonNodeJsonNode jsonNode = JsonConverters.convertStringToJsonNode(jsonString);System.out.println(jsonNode);
// Construct an appropriate keyKey documentKey = new Key(NAMESPACE, SET, "jsonExampleKey");
String documentBinName = "documentBin";// Add to databasedocumentClient.put(documentKey, documentBinName, jsonNode);System.out.println("Stored JSON doc to database.");

Output:

{"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}Stored JSON doc to database.

Query All Subnodes#

// Get all products, both books and bicyclesString jsonPath = "$.store.*";Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

{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}]}

Query Specific Field#

// Get the authors of all booksString jsonPath = "$.store.book[*].author";Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]

Modify Field#

// 3. Modify the authors of all books to “J.K. Rowling”// Get the authors of all booksString jsonPath = "$.store.book[*].author";String jsonObject = "J.K. Rowling";// Modify the authors of all books to "J.K. Rowling"documentClient.put(documentKey, documentBinName, jsonPath, jsonObject);Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

["J.K. Rowling","J.K. Rowling","J.K. Rowling","J.K. Rowling"]

Query with Exists Predicate#

// 4. Get all the books that have an ISBN numberjsonPath = "$..book[?(@.isbn)]";objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

[{"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"}]

Query with Boolean Predicate#

// 5. Get all the books in store cheaper than 10jsonPath = "$.store.book[?(@.price < 10)]";objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

[{"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"}]

Query with RegEx#

// 6. Get all the books matching regex (ignore case)jsonPath = "$..book[?(@.author =~ /.*ROWLING/i)]";objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.println(objectFromDB);

Output:

[{"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"}]

Delete Field#

// 7. Delete the price field of every object exists in store// Get the price of everythingString jsonPath = "$.store..price";Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.format("Before delete: %s\n", objectFromDB);// Delete the price field of every object exists in the storedocumentClient.delete(documentKey, documentBinName, jsonPath);Object objectFromDB = documentClient.get(documentKey, documentBinName, jsonPath);System.out.format("After delete: %s\n", objectFromDB);;

Output:

Before delete: [19.95,8.95,12.99,8.99,22.99]After delete: []

More JSONPath Capabilities and Examples#

Restore the original store document for the following examples.

documentClient.put(documentKey, documentBinName, jsonNode);documentClient.get(documentKey, documentBinName, "$");

Output:

{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}]}, expensive=10}

Functions#

JSONPath has functions min, sum, size, length, etc. See the JSONPath repo for a full list.

// length(): find number of references to Moby DickjsonPath = "$..store.book[?(@.title == 'Moby Dick')].ref.length()";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

3
// avg(): find average price of booksjsonPath = "$..book..price.avg()";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

13.48

Filter Operators#

Operators like in and anyof are supported. See the JSONPath repo for a full list.

// in: books with category in ['reference', 'biography']jsonPath = "$..book[?(@.category in ['reference', 'biography'])]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"ref":[1,2],"category":"reference","title":"Sayings of the Century","author":"Nigel Rees","price":8.95}]
// in: books that have 2 in their reference listjsonPath = "$..book[?(2 in @.ref)]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"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"}]
// anyof: books whose ref list has any of 1, 3, 7, or 100.jsonPath = "$..book[?(@.ref anyof [1, 3, 7, 100])]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"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"}]

Predicates#

// books with price between 10 and 20jsonPath = "$..book[?(@.price > 10 && @.price < 20)]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"ref":[2,4,16],"category":"fiction","title":"Sword of Honour","author":"Evelyn Waugh","price":12.99}]
// comparing with another elementjsonPath = "$..book[?(@.price < $.expensive)]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"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"}]
// composable conditions: books in fiction category with isbn and priced less than 10jsonPath = "$..book[?(@.category == 'fiction' && @.isbn && @.price < 10 )]";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

[{"ref":[1,3,5],"category":"fiction","title":"Moby Dick","author":"Herman Melville","price":8.99,"isbn":"0-553-21311-3"}]

Try Your Own Path#

// substitute your json pathjsonPath = "$";documentClient.get(documentKey, documentBinName, jsonPath );

Output:

{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}]}, expensive=10}

Cleaning Up#

Remove tutorial data and close connection.

truncateTestData();aerospikeClient.close();System.out.println("Removed tutorial data and closed server connection.");

Output:

Removed tutorial data and closed server connection.

Further Exploration and Resources#

Here are some links for further exploration.

Resources#

Exploring Other Notebooks#

Visit Aerospike notebooks repo 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.