我有一个代码,我需要使用 Jedis 获取键“product_id”的 json 值,但我不知道 jedis.get() 操作是否可以直接输出 json。这是下面的代码。
Jedis jedis = pool.getResource();
jedis.select(1);
String attributeName = jedis.get(facet);
jedis.select(0);
JSONObject productJson = jedis.get(productID);
String attributeValue = productJson.getString(attributeName);
您可以使用
String redisCachedJSON = jedis.get(productId);
从 Redis 获取字符串 JSON
然后您可以使用此字符串创建 JSONObject
JSONObject jsonObject = new JSONObject(redisCachedJSON);
请记住,Jedis 上的
get()
期望存储在该键上的值是字符串表示形式。
我不太确定你的对象是什么,但我们假设 json 如下:
// Product json String
{
"id" : 1,
"price" : "2.63",
"quantity" : 25,
"attributeName" : "attributeValue",
}
您可以将 JSON 映射到比 HashMap 更容易处理的 Java 类(除非您的 json 键动态更改),并使用 Gson
将 json 字符串映射到该类的对象public Class Product {
int id;
String price;
int quantity;
String attributeName;
}
String redisCachedJSON= jedis.get(productId);
Product product = new Gson().fromJson(redisCachedJSON, Product.class);
System.out.println(product.attributeName); // attributeValue
您也可以使用 JsonObjectMapper 来做到这一点
ObjectMapper objectMapper = new ObjectMapper();
Product product = objectMapper.readValue(redisCachedJSON, Product.class);
Redis 可以存储和索引 JSON 文档。 安装 Redis Stack 并找到 examples。按照示例学习如何将会话作为 JSON 文档进行管理,以及如何在会话内和跨会话中执行搜索
HostAndPort node = HostAndPort.from("localhost:6379");
JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
.resp3() // RESP3 protocol
.build();
UnifiedJedis client = new UnifiedJedis(node, clientConfig);
// Drop the index and all the indexed documents
client.ftDropIndexDD("session_idx");
// Modeling a shopping cart as JSON using Jedis
Schema schema = new Schema()
.addGeoField("$.location").as("location")
.addTagField("$.cart[*].id").as("item_id")
.addNumericField("$.cart[*].price").as("price")
.addTagField("$.visited[*]").as("visited")
.addNumericField("$.lastAccessedTime").as("updated")
.addNumericField("$.creationTime").as("created");
// Defining the index, it could be HASH too
IndexDefinition def = new IndexDefinition(Type.JSON).setPrefixes(new String[] {"session:"});
// Creating the index
client.ftCreate("session_idx", IndexOptions.defaultOptions().setDefinition(def), schema);
// Some initial data
JSONObject jsonObject = new JSONObject();
jsonObject.put("lastAccessedTime", System.currentTimeMillis() / 1000L);
jsonObject.put("creationTime", System.currentTimeMillis() / 1000L);
jsonObject.put("location", "34.638,31.79");
client.jsonSet("session:1", jsonObject);
// Storing a list of visited URLs
client.jsonSet("session:1", new Path2("visited"), new ArrayList<>());
List<String> visited = new ArrayList<String>();
visited.add("www.redis.io");
visited.add("www.wikipedia.com");
visited.add("www.mortensi.com");
client.jsonSetWithEscape("session:1", new Path2("visited"), visited);
// Shopping cart item
JSONObject laptop = new JSONObject();
laptop.put("id", "hp-2341");
laptop.put("price", 1990.99);
laptop.put("quantity", 1);
// Another shopping cart item
JSONObject laptopCase = new JSONObject();
laptopCase.put("id", "case-9993");
laptopCase.put("price", 19.99);
laptopCase.put("quantity", 2);
// Storing items in the shopping cart
client.jsonSet("session:1", new Path2("cart"), new ArrayList<>());
client.jsonArrAppend("session:1", new Path2("cart"), laptop);
client.jsonArrAppend("session:1", new Path2("cart"), laptopCase);
// Read the JSON document
System.out.println(client.jsonGet("session:1"));
// Read part of the document
System.out.println(client.jsonGet("session:1", new Path2("$.cart")));
// Search within a document
System.out.println(client.jsonGet("session:1", new Path2("$.cart[?(@.id==\"hp-2341\")].price")));
// Creating another session
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("lastAccessedTime", System.currentTimeMillis() / 1000L);
jsonObject2.put("creationTime", System.currentTimeMillis() / 1000L);
jsonObject2.put("location", "34.638,31.79");
client.jsonSet("session:2", jsonObject2);
// Shopping cart item
JSONObject book = new JSONObject();
book.put("id", "sking-2435");
book.put("price", 14.90);
book.put("quantity", 1);
client.jsonSet("session:2", new Path2("cart"), new ArrayList<>());
client.jsonArrAppend("session:2", new Path2("cart"), book);
// Search
Query q = new Query().addFilter(new Query.NumericFilter("price", 10, 30)).setSortBy("price", true).setNoContent();
SearchResult res = client.ftSearch("session_idx", q);
System.out.println("Number of results: " + res.getTotalResults());
List<Document> docs = res.getDocuments();
for (Document doc : docs) {
System.out.println("Found session: " + doc.getId());
}