Last active
February 6, 2025 16:12
-
-
Save Charlie-robin/eb63dfe88390c4c1f6c07f1979b9103c to your computer and use it in GitHub Desktop.
Redis/Jedis Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.sun.jdi.InconsistentDebugInfoException; | |
import org.json.JSONObject; | |
import redis.clients.jedis.UnifiedJedis; | |
import redis.clients.jedis.json.Path2; | |
import redis.clients.jedis.search.*; | |
import redis.clients.jedis.search.schemafields.NumericField; | |
import redis.clients.jedis.search.schemafields.SchemaField; | |
import redis.clients.jedis.search.schemafields.TextField; | |
import java.sql.SQLOutput; | |
public class Main { | |
private record Pokemon(int id, String name, String url, String starter) { | |
} | |
public static void main(String[] args) throws JsonProcessingException { | |
printTitle("KEY VALUE STORE"); | |
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); | |
jedis.set("1", "bulbsaur"); | |
jedis.set("2", "ivysaur"); | |
System.out.println(jedis.get("1")); | |
jedis.set("1", "bulbasaur"); | |
System.out.println(jedis.get("1")); | |
System.out.println(jedis.keys("*")); | |
jedis.flushAll(); | |
printTitle("DOCUMENT STORE REDIS JSON"); | |
// JSON STRING | |
String bulbasaur = "{\"id\":1,\"name\":\"bulbasaur\",\"url\":\"https://pokeapi.co/api/v2/pokemon/1/\", \"starter\":null}"; | |
// JSON CLASS | |
JSONObject ivysaur = new JSONObject() | |
.put("id", 2) | |
.put("name", "ivysaur") | |
.put("url", "https://pokeapi.co/api/v2/pokemon/2/") | |
.put("starter", "bulbasaur"); | |
// INSTANCE INTO JSON | |
Pokemon venusaur = new Pokemon(3, "venusaur", "https://pokeapi.co/api/v2/pokemon/3/", "bulbasaur"); | |
ObjectMapper objectMapper = new ObjectMapper(); | |
String venusaurJSON = objectMapper.writeValueAsString(venusaur); | |
// STORE DIFFERENT TYPES OF JSON | |
jedis.jsonSet("1", bulbasaur); | |
jedis.jsonSet("2", ivysaur); | |
jedis.jsonSet("3", venusaurJSON); | |
// PRINT ALL VALUES STORED | |
for (String key : jedis.keys("*")) { | |
System.out.println(jedis.jsonGet(key)); | |
} | |
Pokemon pokemonJson = objectMapper.convertValue(jedis.jsonGet("1"), Pokemon.class); | |
System.out.println(pokemonJson.id()); | |
// COME BACK TO PATH - DEPRECATED | |
JSONObject demo = new JSONObject().put("hello", "world"); | |
// UPDATING JSON WITH NEW KEY VALUE PAIR | |
jedis.jsonSet("1", new Path2("$.test"), demo); | |
System.out.println(jedis.jsonGet("1")); | |
System.out.println(jedis.keys("*")); | |
// REPLACED VALUE AT KEY 1 | |
jedis.jsonSet("1", Path2.of("$.name"), demo); | |
System.out.println(jedis.jsonGet("1")); | |
jedis.flushAll(); | |
printTitle("SEARCH ENGINE REDIS SEARCH"); | |
// FT -> FULL TEXT | |
// DEFINE JSON SCHEMA SO VALUES SEARCHABLE | |
SchemaField[] schema = { | |
NumericField.of("$.id").as("id"), | |
TextField.of("$.name").as("name").weight(5), | |
TextField.of("$.url").as("url").weight(.1), | |
TextField.of("$.starter").as(".starter"), | |
}; | |
jedis.ftCreate("idx:pokemon", | |
FTCreateParams.createParams() | |
.on(IndexDataType.JSON) | |
.addPrefix("pokemon:"), | |
schema | |
); | |
jedis.jsonSet("pokemon:1", bulbasaur); | |
jedis.jsonSet("pokemon:2", ivysaur); | |
jedis.jsonSet("pokemon:3", venusaurJSON); | |
// QUERIES | |
String all = "*"; | |
String nameEndsWithSaur = "@name:*saur"; | |
String idWithinRange = "@id:[2 3]"; | |
String directMatch = "bulbasaur"; | |
Query query = new Query(directMatch); | |
query.getWithScores(); | |
SearchResult searchResult = jedis.ftSearch("idx:pokemon", query); | |
// SCORE IS BASED ON RELEVANCE | |
// - QUERIES | |
for (Document doc : searchResult.getDocuments()) { | |
System.out.println(doc); | |
} | |
jedis.close(); | |
} | |
public static void printTitle(String title) { | |
System.out.println(); | |
System.out.println("-".repeat(10)); | |
System.out.println(title); | |
System.out.println("-".repeat(10)); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment