Aerospike Java Object Mapper

Aerospike is one of, if not the fastest, NoSQL database in the world. It presents a Java API which is comprehensive and powerful, but requires a measure of boilerplate code to map the data from Java POJOs to the database. The aim of this repository is to lower the amount of code required when mapping POJOs to Aerospike and back as well as reducing some of the brittleness of the code.
Documentation
The documentation for this project can be found on javadoc.io.
Table of contents
- Compatibility with Aerospike Clients
- Motivation and a simple example
- Getting Started
- Constructors
- Keys
- Fields
- Properties
- References to other objects
- Advanced Features
- External Configuration File
- Virtual Lists
- Scans
- Queries
Compatibility with Aerospike Clients
| Java Object Mapper Version |
Aerospike Client |
Aerospike Reactor Client |
| 2.5.2, 2.6.x |
9.0.x (jdk8) |
9.0.x |
| 2.5.0, 2.5.1 |
8.1.x (jdk8) |
8.1.x |
| 2.4.x |
8.1.x (jdk8) |
7.1.x |
| 2.1.x, 2.2.x, 2.3.x |
6.1.x |
6.1.x |
| 2.0.x |
5.1.x |
5.1.x |
| 1.2.x, 1.3.x, 1.4.x |
5.1.x |
5.0.x |
| 1.1.x |
5.0.x |
|
Installing the Mapper
The easiest way to use the mapper is through Maven or Gradle. For Maven, pull it in from Maven Central:
<!-- https://mvnrepository.com/artifact/com.aerospike/java-object-mapper -->
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>java-object-mapper</artifactId>
<version>2.6.0</version>
</dependency>
For Gradle, you can use
// https://mvnrepository.com/artifact/com.aerospike/java-object-mapper
implementation group: 'com.aerospike', name: 'java-object-mapper', version: '2.6.0'
Motivation and a simple example
Consider a simple class:
public class Person {
private String ssn;
private String firstName;
private String lastName;
private int age;
private Date dob;
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
To store an instance of this class into Aerospike requires code similar to:
public void save(Person person, IAerospikeClient client) {
long dobAsLong = (person.getDob() == null) ? 0 : person.getDob().getTime();
client.put( null, new Key("test", "people", person.ssn),
new Bin("ssn", Value.get(person.getSsn())),
new Bin("lstNme", Value.get(person.getLastName())),
new Bin("frstNme", Value.get(person.getFirstName())),
new Bin("age", Value.get(person.getAge())),
new Bin("dob", Value.get(dobAsLong)));
}
Similarly, reading an object requires significant code:
public Person get(String ssn, IAerospikeClient client) {
Record record = client.get(null, new Key("test", "people", ssn));
Person person = new Person();
person.setSsn(ssn);
person.setFirstName(record.getString("frstNme"));
person.setLastName(record.getString("lstNme"));
person.setAge(record.getInt("age");
long dobAsLong = record.getLong("dob");
person.setDoB(dobAsLong == 0 ? null : new Date(dobAsLong));
return person;
}
This code is brittle. It has information such as the namespace name, the set name, and the names of the bins in multiple places. These should all be extracted as constants so they're only referenced once, but this adds more boilerplate code.
Additionally, there is complexity not shown in this simple example. Aerospike does not natively support all of Java types. Mapping a java.util.Date to the database requires additional code to convert to an Aerospike representation and back for example. Sub-objects which also need to be stored in the database must be handled separately. Changing the representation of the information between the database and the POJO requires additional work, such as storing a String representation of a date in Aerospike instead of a numeric representation.
This repository aims to overcome these issues and more by relying on annotations on the POJOs to describe how to map the data to Aerospike and back. For example, the same functionality is provided by this code:
@AerospikeRecord(namespace="test", set="people")
public class Person {
@AerospikeKey
private String ssn;
@AerospikeBin(name="frstNme")
private String firstName;
@AerospikeBin(name="lstNme")
private String lastName;
private int age;
private Date dob;
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
To write person to Aerospike, simple use:
Person p = new Person();
p.setFirstName("John");
p.setLastName("Doe");
p.setSsn("123456789");
p.setAge(17);
AerospikeClient client = new AerospikeClient("aerospike hostname",3000);
AeroMapper mapper = new AeroMapper.Builder(client).build();
mapper.save(p);
To read:
Person person = mapper.read(Person.class, "123456789");
To delete:
mapper.delete(person);
Getting Started
The first thing that needs to be done is to create an instance of the AeroMapper class. This is achieved through the Builder class which allows you to specify
various options. Once the options have been specified, build() is called to get an instance of the AeroMapper. Thus, the simplest usage is:
AeroMapper mapper = new AeroMapper.Builder(client).build();
The Builder constructor simply takes an IAerospikeClient which it uses for access to the database. Other options can be added to the mapper between the constructor for the Builder and the invocation of the build() method. These options include:
.addConverter(Object converter): Registers a class as a custom converter, which allows programmatic control over how data types are mapped to and from Aerospike. This custom converter must have @ToAerospike and @FromAerospike annotated methods. For more information, see Custom Object Converters below.
.preLoadClass(Class<?>): Used to load a class before it is needed. The process of loading a class for the first time can be moderately expensive -- there is lots of introspection which goes on to determine how to map the classes to and from the database with the help of the annotations or configuration file. The results of this process are cached so it only has to happen once, and as few introspection calls as possible are called during the actual transformation. If a class is not preloaded, this computation will happen the first time an instance of that class is encountered, resulting in slowdown on the first call.
Another reason to preload a class is situations where an abstract superclass might be read without the subclasses being seen by the AeroMapper first. For example, a list of Animal might be stored in the database, but Animal is an abstract class with concrete subclasses like Dog, Cat, etc. If the first call of the AeroMapper is to read a list of Animal from the database, there is not enough information to resolve the concrete sub-classes without preloading them.
.preLoadClasses(Class<?> ...): Use to preload several classes before they are called. This is a convenience mechanism which calls .preLoadClass for each of the classes in the list.
.preLoadClassesFromPackage(String | Class<?>): Preload all the classes in the specified package which are annotated with @AerospikeRecord. The package can be specified by passing a string of the package name or by passing a class in that package. The latter method is preferred as this is less brittle as code is refactored. Note that if a class is passed this class is used only for the package name and does not necessarily need to be a class annotated with @AerospikeRecord. Creating a 'marker' class in the package with no functionality and passing to this method is a good way of preventing breaking the preloading as classes are moved around.
withConfigurationFile: Whilst mapping information from POJOs via annotations is efficient and has the mapping code inline with the POJO code, there are times when this is not available. For example, if an external library with POJOs is being used and it is desired to map those POJOs to the database, there is no easy way of annotating the source code. Another case this applies is if different mapping parameters are needed between different environments. For example, embedded objects might be stored in a map in development for ease of debugging, but stored in a list in production for compaction of stored data. In these cases an external configuration YAML file can be used to specify how to map the data to the database. See External Configuration File for more details. There is an overload of this method which takes an additional boolean parameter -- if this is true and the configuration file is not valid, errors will be logged to stderr and the process continue. It is normally not recommended to set this parameter to true.
If multiple configuration files are used and the same class is defined in multiple configuration files, the definitions in the first configuration file for a class will be used.
withConfiguration: Similar to the withConfigurationFile above, this allows configuration to be externally specified. In this case, the configuration is passed as a YAML string.
withReadPolicy, withWritePolicy, withBatchPolicy, withScanPolicy, withQueryPolicy: This allows setting of the appropriate policy type. The following discussion uses read policies, but applies equally to all the other policies.
After the specified policy, there are 3 possible options:
forAll(): The passed policy is used for all classes. This is similar to setting the defaultReadPolicy on the IAerospikeClient but allows it to be set after the client is created.
forThisOrChildrenOf(Class<?> class): The passed policy is used for the passed class and all subclasses of the passed class.
forClasses(Class<?>... classes): The passed policy is used for the passed class(es), but no subclasses.
It is entirely possible that a class falls into more than one category, in which case the most specific policy is used. If no policy is specified, the defaultReadPolicy passed to the IAerospikeClient is used. For example, if there are classes A, B, C with C being a subclass of B, a definition could be for example:
Policy readPolicy1, readPolicy2, readPolicy3;
// ... code to set up the policies goes here...
AeroMapper.Builder(client)
.withReadPolicy(readPolicy1).forAll()
.withReadPolicy(readPolicy2).forThisOrChildrenOf(B.class)
.withReadPolicy(readPolicy3).forClasses(C.class)
.build();
In this case the forAll() would apply to A,B,C, the forThisOrChildrenOf would apply to B,C and forClasses would apply to C. So the policies used for each class would be:
- A:
readPolicy1
- B:
readPolicy2
- C:
readPolicy3
Note that each operation can also optionally take a policy if it is desired to change any of the policy settings on the fly. The explicitly provided policy will override any other settings, such as durableDelete on the @AerospikeRecord