Skip to main content

Modeling JSON Documents with Redis and Java

RedisJSON lets you store, index, and query JSON data in Redis. Jedis, a Java driver for Redis, provides full support for RedisJSON as of the 4.0 release.

Follow along with the steps below to get started with Java and RedisJSON.

1. Run the RedisMod Docker container

The RedisMod docker container bundles the Redis modules that power RedisJSON. To get a running Redis instance with RedisJSON, run the following bash command:

 docker run -d -p 6379:6379 redislabs/redismod:latest

2. Add Jedis as a Dependency

You'll need to add Jedis to your Java project. If you're using Maven, the dependency will look something like this:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.0.0<version>
</dependency>

Replace the version with your desired version of Jedis, but note that you'll need Jedis 4.0 or greater to get RedisJSON support.

3. Connect to Redis

You'll need to initialize you connection to Redis. This means configuring and creating a UnifiedJedis instance:

  HostAndPort config = new HostAndPort(Protocol.DEFAULT_HOST, 6379);
PooledConnectionProvider provider = new PooledConnectionProvider(config);
UnifiedJedis client = new UnifiedJedis(provider);

4. Model Your Domain

You'll need to represent your data by creating POJOs. Jedis will then help you serialize these objects to JSON.

Suppose you're building an online learning platform, and you want to represent students. Let's create a simple POJO to represent these students:

private class Student {
private String firstName;
private String lastName;

public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
}

Now we can create some students and store them in Redis as JSON:

Student maya = new Student("Maya", "Jayavant");
client.jsonSet("student:111", maya);

Student oliwia = new Student("Oliwia", "Jagoda");
client.jsonSet("student:112", oliwia);

Notice we pass the Student instances to the jsonSet() method. Jedis then serializes the objects and stores them in Redis at the specified keys (in this case, "student:111" and "student:112").

Querying and indexing JSON

If we want to be able to query this JSON, we'll need to create an index. Let's create an index on the "firstName" and "lastName" fields. To do this:

  1. We define which fields to index ("firstName" and "lastName").
  2. We set up the index definition to recognize JSON and include only those documents whose key starts with "student:".
  3. Then we actually create the index, called "student-index", by calling ftCreate ().
Schema schema = new Schema().addTextField("$.firstName", 1.0).addTextField("$" +
".lastName", 1.0);
IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON)
.setPrefixes(new String[]{"student:"});
client.ftCreate("student-index",
IndexOptions.defaultOptions().setDefinition(rule),
schema);

With an index now defined, we can query our JSON. Let's find all students whose name begins with "maya":

Query q = new Query("@\\$\\" + ".firstName:maya*");
SearchResult mayaSearch = client.ftSearch("student-index", q);

We can then iterate over our search results:

List<Document> docs = mayaSearch.getDocuments();
for (Document doc : docs) {
System.out.println(doc);
}

This example just scratches the surface. You can atomically manipulate JSON documents and query them in a variety of ways. See the RedisJSON docs, the RediSearch docs, and our course, "Querying, Indexing, and Full-text Search in Redis", for a lot more examples.