User profile store
This interactive walkthrough explores creating a simple user profile store with Aerospike.
A connection is established with a real Aerospike database, allowing you to use the front end to
create
, read
, update
, and delete
user profiles.
Connect to your Aerospike Cloud database if using the supported Java and Go Aerospike clients.
View the corresponding back end functions below while moving through the various stages of the UI to see how the databse interaction occurs.
No data used in this walkthrough is saved when using a test database. Test databases are deleted after one hour. Do not put sensitive data into this walkthrough.
Connect
Enter your Aerospike Cloud connection details below, or leave blank to use a test database, and click Connect
.
Aerospike Cloud
Click connect to get started
Front end
Use the controls below to interact with the Aerospike database and create
, read
, update
, and delete
user profile data.
Sign up
Back end
Expand the following section to view the connection setup code.
Application setup
Dependencies
<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
</dependencies>
Imports
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Host;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.ResultCode;
import com.aerospike.client.exp.Exp;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.policy.QueryPolicy;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.query.KeyRecord;
import com.aerospike.client.query.RecordSet;
import com.aerospike.client.query.Statement;
import java.util.*;
import org.json.JSONArray;
import org.json.JSONObject;
Connection setup
private final String namespace = "test";
private final String set = "users";
// Instantiate client
IAerospikeClient client = new AerospikeClient("localhost", 3000);
- Create
- Read
- Update
- Delete
- Query
The code below shows the function being used to create a user profile.
public void createUser(String input, IAerospikeClient client) throws AerospikeException {
// Parse the input
JSONObject user = new JSONObject(input);
String userName = user.getString("name");
String userEmail = user.getString("email");
// This value is randomly generated upon sign up
String userBgColor = user.getString("bgColor");
boolean userConsent = user.optBoolean("consent");
// Create a write policy to only create new users
WritePolicy writePolicy = new WritePolicy();
writePolicy.recordExistsAction = RecordExistsAction.CREATE_ONLY;
// Create a key using the email
Key key = new Key(namespace, set, userEmail);
// Create the user bins
Bin name = new Bin("name", userName);
Bin email = new Bin("email", userEmail);
Bin bgColor = new Bin("bgColor", userBgColor);
Bin consent = new Bin("consent", userConsent);
try {
// Write the new user record to the database
client.put(writePolicy, key, name, email, bgColor, consent);
}
catch (AerospikeException e) {
if (e.getResultCode() == ResultCode.KEY_EXISTS_ERROR) {
throw new IllegalArgumentException("Email " + userEmail + " already exists");
}
else {
throw e;
}
}
}