Modeling JSON Documents with Redis and Java
Redis JSON lets you store, index, and query JSON data in Redis. Jedis, a Java driver for Redis, provides full support for Redis JSON as of the 4.0 release.
Follow along with the steps below to get started with Java and Redis JSON.
1. Run the Redis Stack Docker container
The Redis Stack docker container bundles the Redis features that power JSON. To get a running Redis instance with JSON, run the following bash command:
docker run -d -p 6379:6379 redis/redis-stack: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 Redis JSON 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:
- We define which fields to index ("firstName" and "lastName").
- We set up the index definition to recognize JSON and include only those documents whose key starts with "student:".
- 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 Redis JSON docs, the Redis Search and Query docs, and our course, "Querying, Indexing, and Full-text Search in Redis", for a lot more examples.
- RU204: Storing, Querying and Indexing JSON at Speed - a course at Redis University
- Redis JSON and Python
- How to store and retrieve nested JSON document
- Importing JSON data into Redis using NodeJS
- Learn more about Redis JSON in the QuickStart tutorial.
- How to build shopping cart app using NodeJS and Redis JSON
- Indexing, Querying, and Full-Text Search of JSON Documents with Redis