|
| 1 | +# Redis |
| 2 | + |
| 3 | +## Install Redis |
| 4 | + |
| 5 | +There are a few ways of |
| 6 | +[installing Redis itself](https://redis.io/docs/latest/operate/oss_and_stack/install/). |
| 7 | + |
| 8 | +::: tip |
| 9 | + |
| 10 | +Note that Redis is a lightweight version, whereas Redis Stack contains more |
| 11 | +features. |
| 12 | + |
| 13 | +::: |
| 14 | + |
| 15 | +If you are using Docker, you should be able to access Redis without explicitly |
| 16 | +running it. If you have installed natively, you may need to run it: |
| 17 | + |
| 18 | +::: code-group |
| 19 | + |
| 20 | +```bash |
| 21 | +redis-server |
| 22 | +``` |
| 23 | + |
| 24 | +```bash |
| 25 | +# if using redis stack |
| 26 | +redis-stack-server |
| 27 | +``` |
| 28 | + |
| 29 | +::: |
| 30 | + |
| 31 | +## Install Redis Client |
| 32 | + |
| 33 | +There is a client library for Java called Jedis which makes developing against a |
| 34 | +Redis database much easier. |
| 35 | + |
| 36 | +To install it, add the following dependency to your `pom.xml`: |
| 37 | + |
| 38 | +```xml |
| 39 | +<dependency> |
| 40 | + <groupId>redis.clients</groupId> |
| 41 | + <artifactId>jedis</artifactId> |
| 42 | + <version>5.2.0</version> |
| 43 | +</dependency> |
| 44 | +``` |
| 45 | + |
| 46 | +## Basic operations |
| 47 | + |
| 48 | +```java |
| 49 | +package org.example; |
| 50 | +import redis.clients.jedis.UnifiedJedis; |
| 51 | + |
| 52 | +public class Main { |
| 53 | + public static void main(String[] args) { |
| 54 | + // Connect to Redis |
| 55 | + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); |
| 56 | + |
| 57 | + // Set a key/value pair |
| 58 | + String res1 = jedis.set("bike:1", "Deimos"); |
| 59 | + System.out.println(res1); // OK |
| 60 | + |
| 61 | + // Get a value for a specific key |
| 62 | + String res2 = jedis.get("bike:1"); |
| 63 | + System.out.println(res2); // Deimos |
| 64 | + |
| 65 | + // Close the connection |
| 66 | + jedis.close(); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Working with JSON |
| 72 | + |
| 73 | +Redis can store JSON data without stringifying it. Effectively, this makes Redis |
| 74 | +a document store. |
| 75 | + |
| 76 | +First, we create a JSON `object`: |
| 77 | + |
| 78 | +```java |
| 79 | +import org.json.JSONObject; |
| 80 | + |
| 81 | +JSONObject userJSON = new JSONObject() |
| 82 | + .put("name", "Jane Doe") |
| 83 | + .put( "email", "[email protected]") |
| 84 | + .put("age", 25) |
| 85 | +``` |
| 86 | + |
| 87 | +Then, we add the user's json to the database: |
| 88 | + |
| 89 | +```java |
| 90 | +import redis.clients.jedis.json.Path2; |
| 91 | + |
| 92 | +String res1 = jedis.jsonSet("user:1", new Path2("$"), userJSON); // OK |
| 93 | + |
| 94 | +String res2 = jedis.jsonGet("user:1") |
| 95 | +// {"name": "Jane Doe", "email": "[email protected]", "age": 25} |
| 96 | +``` |
| 97 | + |
| 98 | +Note that using `$` as the path means that this JSON is set at the root of the |
| 99 | +key `user:1`. However, the keys can be manipulated individually. |
| 100 | + |
| 101 | +```java |
| 102 | +String res1 = jedis.jsonSet("user:1", new Path2("$.age"), 26); // OK |
| 103 | + |
| 104 | +String res2 = jedis.jsonGet("user:1") |
| 105 | +// {"name": "Jane Doe", "email": "[email protected]", "age": 26} |
| 106 | +``` |
| 107 | + |
| 108 | +## Search index |
| 109 | + |
| 110 | +We can create a search index which allows us to implement full-text search: |
| 111 | + |
| 112 | +```java |
| 113 | +import redis.clients.jedis.search.*; |
| 114 | +import redis.clients.jedis.search.aggr.*; |
| 115 | +import redis.clients.jedis.search.schemafields.*; |
| 116 | + |
| 117 | +SchemaField[] schema = { |
| 118 | + TextField.of("$.name").as("name"), |
| 119 | + NumericField.of("$.age").as("age") |
| 120 | +}; |
| 121 | + |
| 122 | +String createResult = jedis.ftCreate("idx:users", |
| 123 | + FTCreateParams.createParams() |
| 124 | + .on(IndexDataType.JSON) |
| 125 | + .addPrefix("user:"), |
| 126 | + schema |
| 127 | +); |
| 128 | + |
| 129 | +System.out.println(createResult); // >>> OK |
| 130 | +``` |
| 131 | + |
| 132 | +Note that `addPrefix("user:")` restricts the index to include only documents |
| 133 | +whose key begins with `user:`, which makes it more efficient. |
| 134 | + |
| 135 | +Now, we can use the search index. For example, here is how to get all users aged |
| 136 | +from 20 to 30: |
| 137 | + |
| 138 | +```java |
| 139 | +import java.util.List; |
| 140 | + |
| 141 | +SearchResult searchResult = jedis.ftSearch("idx:users", "@age:[20 30]"); |
| 142 | + |
| 143 | +for (Document doc: searchResult.getdocuments()) { |
| 144 | + System.out.println(doc.getId()); |
| 145 | +} |
| 146 | + |
| 147 | +// users:1 |
| 148 | +``` |
| 149 | + |
| 150 | +To construct more complex queries, check out the |
| 151 | +[redis query engine](https://redis.io/docs/latest/develop/interact/search-and-query/). |
0 commit comments