From 1578b98bc7ca9f3a79ed45300e56f99cbd863dab Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Wed, 17 May 2023 12:10:26 +0200 Subject: [PATCH 01/19] Fix search_quickstart example --- .../java/io/redis/examples/SearchQuickstartExample.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 5ef34b7529..4013c6a909 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -1,5 +1,4 @@ -//EXAMPLE: set_and_get -//HIDE_START +//EXAMPLE: search_quickstart package io.redis.examples; import java.math.BigDecimal; @@ -36,8 +35,6 @@ public class SearchQuickstartExample { @Test public void run() { - //HIDE_END - // STEP_START connect UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); // STEP_END @@ -215,4 +212,3 @@ public void run() { // REMOVE_END } } -//HIDE_END From 50544dc67ae8e126c21fdf1d7257c3782c92bac3 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Thu, 18 May 2023 16:18:16 +0200 Subject: [PATCH 02/19] Add data_class step to SearchQuickstartExample --- .../io/redis/examples/SearchQuickstartExample.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 4013c6a909..5ae32508b4 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -15,6 +15,7 @@ import static org.junit.Assert.assertEquals; //REMOVE_END +// STEP_START data_class class Bicycle { public String brand; public String model; @@ -30,6 +31,7 @@ public Bicycle(String brand, String model, BigDecimal price, String description, this.condition = condition; } } +// STEP_END public class SearchQuickstartExample { @@ -181,10 +183,10 @@ public void run() { // REMOVE_END // STEP_START query_single_term_limit_fields - Query cargo_query = new Query("cargo").returnFields("price"); - List cargo_result = jedis.ftSearch( - "idx:bicycle", cargo_query).getDocuments(); - System.out.println(cargo_result); + Query cargoQuery = new Query("cargo").returnFields("price"); + List cargoResult = jedis.ftSearch( + "idx:bicycle", cargoQuery).getDocuments(); + System.out.println(cargoResult); // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] // STEP_END // REMOVE_START From a0043ffc646e32ad1148614d9f98f9ce6425d2a6 Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Tue, 4 Jul 2023 23:04:59 +0100 Subject: [PATCH 03/19] Update examples with new bikes and align to new tutorial --- .../examples/SearchQuickstartExample.java | 332 ++++++++++-------- 1 file changed, 191 insertions(+), 141 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 5ae32508b4..a14569588c 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -15,7 +15,6 @@ import static org.junit.Assert.assertEquals; //REMOVE_END -// STEP_START data_class class Bicycle { public String brand; public String model; @@ -27,151 +26,195 @@ public Bicycle(String brand, String model, BigDecimal price, String description, this.brand = brand; this.model = model; this.price = price; - this.description = description; this.condition = condition; + this.description = description; } } -// STEP_END public class SearchQuickstartExample { @Test public void run() { // STEP_START connect - UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + JedisPooled jedis = new JedisPooled("localhost", 6379); // STEP_END // REMOVE_START try { - jedis.ftDropIndex("idx:bicycle"); + jedis.ftDropIndex( "idx:bicycle"); } catch (JedisDataException e) { - // ignore + System.out.println("Can't connect to Redis: " + e.getMessage()); } // REMOVE_END - // STEP_START data_sample - Bicycle bike1 = new Bicycle( - "Diaz Ltd", - "Dealer Sl", - new BigDecimal(7315.58), - "The Diaz Ltd Dealer Sl is a reliable choice" + - " for urban cycling. The Diaz Ltd Dealer Sl " + - "is a comfortable choice for urban cycling.", - "used" - ); - // STEP_END - - Bicycle[] bicycles = { - bike1, - new Bicycle( - "Bridges Group", - "Project Pro", - new BigDecimal(3610.82), - "This mountain bike is perfect for mountain biking. The Bridges" + - " Group Project Pro is a responsive choice for mountain biking.", - "used" - ), - new Bicycle( - "Vega, Cole and Miller", - "Group Advanced", - new BigDecimal(8961.42), - "The Vega, Cole and Miller Group Advanced provides a excellent" + - " ride. With its fast carbon frame and 24 gears, this bicycle is" + - " perfect for any terrain.", - "used" - ), - new Bicycle( - "Powell-Montgomery", - "Angle Race", - new BigDecimal(4050.27), - "The Powell-Montgomery Angle Race is a smooth choice for road" + - " cycling. The Powell-Montgomery Angle Race provides a durable" + - " ride.", - "used" - ), - new Bicycle( - "Gill-Lewis", - "Action Evo", - new BigDecimal(283.68), - "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis" + - " Action Evo provides a excellent ride.", - "used" - ), - new Bicycle( - "Rodriguez-Guerrero", - "Drama Comp", - new BigDecimal(4462.55), - "This kids bike is perfect for young riders. With its excellent" + - " aluminum frame and 12 gears, this bicycle is perfect for any" + - " terrain.", - "new" - ), - new Bicycle( - "Moore PLC", - "Award Race", - new BigDecimal(3790.76), - - "This olive folding bike features a carbon frame and 27.5 inch" + - " wheels. This folding bike is perfect for compact storage and" + - " transportation.", - "new" - ), - new Bicycle( - "Hall, Haley and Hayes", - "Weekend Plus", - new BigDecimal(2008.4), - "The Hall, Haley and Hayes Weekend Plus provides a comfortable" + - " ride. This blue kids bike features a steel frame and 29.0 inch" + - " wheels.", - "new" - ), - new Bicycle( - "Peck-Carson", - "Sun Hybrid", - new BigDecimal(9874.95), - "With its comfortable aluminum frame and 25 gears, this bicycle is" + - " perfect for any terrain. The Peck-Carson Sun Hybrid provides a" + - " comfortable ride.", - "new" - ), - new Bicycle( - "Fowler Ltd", - "Weekend Trail", - new BigDecimal(3833.71), - "The Fowler Ltd Letter Trail is a comfortable choice for" + - " transporting cargo. This cargo bike is perfect for transporting" + - " cargo.", - "refurbished" - ) - }; - - // STEP_START define_index + // STEP_START create_index SchemaField[] schema = { - TextField.of("$.brand").as("brand"), - TextField.of("$.model").as("model"), - TextField.of("$.description").as("description"), - NumericField.of("$.price").as("price"), - TagField.of("$.condition").as("condition") + TextField.of("$.brand").as("brand"), + TextField.of("$.model").as("model"), + TextField.of("$.description").as("description"), + NumericField.of("$.price").as("price"), + TagField.of("$.condition").as("condition") }; - // STEP_END - // STEP_START create_index jedis.ftCreate("idx:bicycle", - FTCreateParams.createParams() - .on(IndexDataType.JSON) - .addPrefix("bicycle:"), - schema + FTCreateParams.createParams() + .on(IndexDataType.JSON) + .addPrefix("bicycle:"), + schema ); // STEP_END + + Bicycle[] bicycles = { + new Bicycle( + "Velorim", + "Jigger", + new BigDecimal(270), + "new", + "Small and powerful, the Jigger is the best ride " + + "for the smallest of tikes! This is the tiniest " + + "kids’ pedal bike on the market available without" + + " a coaster brake, the Jigger is the vehicle of " + + "choice for the rare tenacious little rider " + + "raring to go." + ), + new Bicycle( + "Bicyk", + "Hillcraft", + new BigDecimal(1200), + "used", + "Kids want to ride with as little weight as possible." + + " Especially on an incline! They may be at the age " + + "when a 27.5 inch wheel bike is just too clumsy coming " + + "off a 24 inch bike. The Hillcraft 26 is just the solution" + + " they need!" + ), + new Bicycle( + "Nord", + "Chook air 5", + new BigDecimal(815), + "used", + "The Chook Air 5 gives kids aged six years and older " + + "a durable and uberlight mountain bike for their first" + + " experience on tracks and easy cruising through forests" + + " and fields. The lower top tube makes it easy to mount" + + " and dismount in any situation, giving your kids greater" + + " safety on the trails." + ), + new Bicycle( + "Eva", + "Eva 291", + new BigDecimal(3400), + "used", + "The sister company to Nord, Eva launched in 2005 as the" + + " first and only women-dedicated bicycle brand. Designed" + + " by women for women, allEva bikes are optimized for the" + + " feminine physique using analytics from a body metrics" + + " database. If you like 29ers, try the Eva 291. It's a " + + "brand new bike for 2022.. This full-suspension, " + + "cross-country ride has been designed for velocity. The" + + " 291 has 100mm of front and rear travel, a superlight " + + "aluminum frame and fast-rolling 29-inch wheels. Yippee!" + ), + new Bicycle( + "Noka Bikes", + "Kahuna", + new BigDecimal(3200), + "used", + "Whether you want to try your hand at XC racing or are " + + "looking for a lively trail bike that's just as inspiring" + + " on the climbs as it is over rougher ground, the Wilder" + + " is one heck of a bike built specifically for short women." + + " Both the frames and components have been tweaked to " + + "include a women’s saddle, different bars and unique " + + "colourway." + ), + new Bicycle( + "Breakout", + "XBN 2.1 Alloy", + new BigDecimal(810), + "new", + "The XBN 2.1 Alloy is our entry-level road bike – but that’s" + + " not to say that it’s a basic machine. With an internal " + + "weld aluminium frame, a full carbon fork, and the slick-shifting" + + " Claris gears from Shimano’s, this is a bike which doesn’t" + + " break the bank and delivers craved performance." + ), + new Bicycle( + "ScramBikes", + "WattBike", + new BigDecimal(2300), + "new", + "The WattBike is the best e-bike for people who still feel young" + + " at heart. It has a Bafang 1000W mid-drive system and a 48V" + + " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" + + " more than 60 miles on one charge. It’s great for tackling hilly" + + " terrain or if you just fancy a more leisurely ride. With three" + + " working modes, you can choose between E-bike, assisted bicycle," + + " and normal bike modes." + ), + new Bicycle( + "Peaknetic", + "Secto", + new BigDecimal(430), + "new", + "If you struggle with stiff fingers or a kinked neck or back after" + + " a few minutes on the road, this lightweight, aluminum bike" + + " alleviates those issues and allows you to enjoy the ride. From" + + " the ergonomic grips to the lumbar-supporting seat position, the" + + " Roll Low-Entry offers incredible comfort. The rear-inclined seat" + + " tube facilitates stability by allowing you to put a foot on the" + + " ground to balance at a stop, and the low step-over frame makes it" + + " accessible for all ability and mobility levels. The saddle is" + + " very soft, with a wide back to support your hip joints and a" + + " cutout in the center to redistribute that pressure. Rim brakes" + + " deliver satisfactory braking control, and the wide tires provide" + + " a smooth, stable ride on paved roads and gravel. Rack and fender" + + " mounts facilitate setting up the Roll Low-Entry as your preferred" + + " commuter, and the BMX-like handlebar offers space for mounting a" + + " flashlight, bell, or phone holder." + ), + new Bicycle( + "nHill", + "Summit", + new BigDecimal(1200), + "new", + "This budget mountain bike from nHill performs well both on bike" + + " paths and on the trail. The fork with 100mm of travel absorbs" + + " rough terrain. Fat Kenda Booster tires give you grip in corners" + + " and on wet trails. The Shimano Tourney drivetrain offered enough" + + " gears for finding a comfortable pace to ride uphill, and the" + + " Tektro hydraulic disc brakes break smoothly. Whether you want an" + + " affordable bike that you can take to work, but also take trail in" + + " mountains on the weekends or you’re just after a stable," + + " comfortable ride for the bike path, the Summit gives a good value" + + " for money." + ), + new Bicycle( + "ThrillCycle", + "BikeShind", + new BigDecimal(815), + "refurbished", + "An artsy, retro-inspired bicycle that’s as functional as it is" + + " pretty: The ThrillCycle steel frame offers a smooth ride. A" + + " 9-speed drivetrain has enough gears for coasting in the city, but" + + " we wouldn’t suggest taking it to the mountains. Fenders protect" + + " you from mud, and a rear basket lets you transport groceries," + + " flowers and books. The ThrillCycle comes with a limited lifetime" + + " warranty, so this little guy will last you long past graduation." + ), + }; + // STEP_START add_documents for (int i = 0; i < bicycles.length; i++) { jedis.jsonSetWithEscape(String.format("bicycle:%d", i), bicycles[i]); } // STEP_END - // STEP_START query_single_term_and_num_range - Query query = new Query("folding @price:[1000 4000]"); - List result = jedis.ftSearch("idx:bicycle", query).getDocuments(); - System.out.println(result); + // STEP_START wildcard_query + Query query1 = new Query("*"); + List < Document > result1 = jedis.ftSearch("idx:bicycle", query1).getDocuments(); +System.out.println("Result1:\n------------------\n"); + System.out.println(result1); // Prints: [id:bicycle:6, score: 1.0, payload:null, properties:[ // $={"brand":"Moore PLC","model":"Award Race","price":3790.76, // "description":"This olive folding bike features a carbon frame and 27.5 inch wheels. @@ -179,38 +222,45 @@ public void run() { // ] // STEP_END // REMOVE_START - assertEquals("Validate folding bike id", "bicycle:6", result.get(0).getId()); + assertEquals("Validate total results", 10, result1.size()); // REMOVE_END - // STEP_START query_single_term_limit_fields - Query cargoQuery = new Query("cargo").returnFields("price"); - List cargoResult = jedis.ftSearch( - "idx:bicycle", cargoQuery).getDocuments(); - System.out.println(cargoResult); + // STEP_START query_single_term + Query query2 = new Query("@model:Jigger"); + List < Document > result2 = jedis.ftSearch( + "idx:bicycle", query2).getDocuments(); +System.out.println("Result2:\n------------------\n"); + System.out.println(result2); // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] // STEP_END // REMOVE_START - assertEquals("Validate cargo bike id", "bicycle:9", cargo_result.get(0).getId()); + assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); // REMOVE_END - // STEP_START simple_aggregation - AggregationBuilder ab = new AggregationBuilder("*") - .groupBy("@condition", Reducers.count().as("count")); - AggregationResult ar = jedis.ftAggregate("idx:bicycle", ab); + // STEP_START query_single_term_limit_fields + Query query3 = new Query("@model:Jigger").returnFields("price"); + List < Document > result3 = jedis.ftSearch( + "idx:bicycle", query3).getDocuments(); +System.out.println("Result3:\n------------------\n"); + System.out.println(result3); + // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] + // STEP_END + // REMOVE_START + assertEquals("Validate cargo bike id", "bicycle:0", result3.get(0).getId()); + // REMOVE_END - for (int i = 0; i < ar.getTotalResults(); i++) { - System.out.println( - ar.getRow(i).getString("condition") - + " - " - + ar.getRow(i).getString("count")); - } - // Prints: - // refurbished - 1 - // used - 5 - // new - 4 + // STEP_START query_single_term_and_num_range + Query query4 = new Query("basic @price:[500 1000]"); + List < Document > result4 = jedis.ftSearch( + "idx:bicycle", query4).getDocuments(); +System.out.println("Result3:\n------------------\n"); + System.out.println(result3); + // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] // STEP_END // REMOVE_START - assertEquals("Validate aggregation results", 3, ar.getTotalResults()); + assertEquals("Validate bike id", "bicycle:5", result4.get(0).getId()); // REMOVE_END + + jedis.close(); } -} +} \ No newline at end of file From c5dd7712fedb677e39bd5b5b71cb66362fe7abdf Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Tue, 4 Jul 2023 23:15:23 +0100 Subject: [PATCH 04/19] Fixes doctests for search tutorial --- .../examples/SearchQuickstartExample.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index a14569588c..10009168b0 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -22,7 +22,7 @@ class Bicycle { public String description; public String condition; - public Bicycle(String brand, String model, BigDecimal price, String description, String condition) { + public Bicycle(String brand, String model, BigDecimal price, String condition, String description) { this.brand = brand; this.model = model; this.price = price; @@ -213,13 +213,8 @@ public void run() { // STEP_START wildcard_query Query query1 = new Query("*"); List < Document > result1 = jedis.ftSearch("idx:bicycle", query1).getDocuments(); -System.out.println("Result1:\n------------------\n"); System.out.println(result1); - // Prints: [id:bicycle:6, score: 1.0, payload:null, properties:[ - // $={"brand":"Moore PLC","model":"Award Race","price":3790.76, - // "description":"This olive folding bike features a carbon frame and 27.5 inch wheels. - // This folding bike is perfect for compact storage and transportation.","condition":"new"}] - // ] + // Prints: [id:bicycle:1, score: 1.0, payload:null, properties:[$={"brand":"Bicyk","... // STEP_END // REMOVE_START assertEquals("Validate total results", 10, result1.size()); @@ -229,9 +224,8 @@ public void run() { Query query2 = new Query("@model:Jigger"); List < Document > result2 = jedis.ftSearch( "idx:bicycle", query2).getDocuments(); -System.out.println("Result2:\n------------------\n"); System.out.println(result2); - // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] + // Prints: [id:bicycle:0, score: 1.0, payload:null, properties:[$={"brand":"Velorim","model":"Jigger","price":270,"description":"Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.","condition":"new"}]] // STEP_END // REMOVE_START assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); @@ -241,9 +235,8 @@ public void run() { Query query3 = new Query("@model:Jigger").returnFields("price"); List < Document > result3 = jedis.ftSearch( "idx:bicycle", query3).getDocuments(); -System.out.println("Result3:\n------------------\n"); System.out.println(result3); - // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] + // Prints: [id:bicycle:0, score: 1.0, payload:null, properties:[price=270]] // STEP_END // REMOVE_START assertEquals("Validate cargo bike id", "bicycle:0", result3.get(0).getId()); @@ -253,9 +246,8 @@ public void run() { Query query4 = new Query("basic @price:[500 1000]"); List < Document > result4 = jedis.ftSearch( "idx:bicycle", query4).getDocuments(); -System.out.println("Result3:\n------------------\n"); - System.out.println(result3); - // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] + System.out.println(result4); + // Prints: [id:bicycle:5, score: 1.0, payload:null, properties:[$={"brand":"Breakout","model":"XBN 2.1 Alloy","price":810,"description":"The XBN 2.1 Alloy is our entry-level road bike – but that’s not to say that it’s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance.","condition":"new"}]] // STEP_END // REMOVE_START assertEquals("Validate bike id", "bicycle:5", result4.get(0).getId()); From 2ba7bdb09916952d5d9c8207cb68853ee938d241 Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Tue, 4 Jul 2023 23:38:57 +0100 Subject: [PATCH 05/19] Small fixes doctests --- .../redis/examples/SearchQuickstartExample.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 10009168b0..8a9aca6adb 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -220,7 +220,7 @@ public void run() { assertEquals("Validate total results", 10, result1.size()); // REMOVE_END - // STEP_START query_single_term + // STEP_START query_single_term_all_fields Query query2 = new Query("@model:Jigger"); List < Document > result2 = jedis.ftSearch( "idx:bicycle", query2).getDocuments(); @@ -231,7 +231,7 @@ public void run() { assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); // REMOVE_END - // STEP_START query_single_term_limit_fields + // STEP_START query_single_term Query query3 = new Query("@model:Jigger").returnFields("price"); List < Document > result3 = jedis.ftSearch( "idx:bicycle", query3).getDocuments(); @@ -253,6 +253,17 @@ public void run() { assertEquals("Validate bike id", "bicycle:5", result4.get(0).getId()); // REMOVE_END + // STEP_START query_exact_matching + Query query5 = new Query("@brand:\"Noka Bikes\""); + List < Document > result5 = jedis.ftSearch( + "idx:bicycle", query5).getDocuments(); + System.out.println(result5); + // Prints: [id:bicycle:4, score: 1.0, payload:null, properties:[$={"brand":"Noka Bikes","model":"Kahuna","price":3200,"description":"Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a women’s saddle, different bars and unique colourway.","condition":"used"}]] + // STEP_END + // REMOVE_START + assertEquals("Validate bike id", "bicycle:4", result5.get(0).getId()); + // REMOVE_END + jedis.close(); } } \ No newline at end of file From 5b0a7f8e8c8778bbf3f4f40f0a39428cf7a9c64e Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Wed, 5 Jul 2023 10:53:18 +0100 Subject: [PATCH 06/19] Updates search tutorial doc examples to new format and data --- .../examples/SearchQuickstartExample.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 8a9aca6adb..09323c2685 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -213,14 +213,14 @@ public void run() { // STEP_START wildcard_query Query query1 = new Query("*"); List < Document > result1 = jedis.ftSearch("idx:bicycle", query1).getDocuments(); - System.out.println(result1); - // Prints: [id:bicycle:1, score: 1.0, payload:null, properties:[$={"brand":"Bicyk","... + System.out.println("Documents found:" + result1.size()); + // Prints: Documents found: 10 // STEP_END // REMOVE_START assertEquals("Validate total results", 10, result1.size()); // REMOVE_END - // STEP_START query_single_term_all_fields + // STEP_START query_single_term Query query2 = new Query("@model:Jigger"); List < Document > result2 = jedis.ftSearch( "idx:bicycle", query2).getDocuments(); @@ -231,7 +231,7 @@ public void run() { assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); // REMOVE_END - // STEP_START query_single_term + // STEP_START query_single_term_limit_fields Query query3 = new Query("@model:Jigger").returnFields("price"); List < Document > result3 = jedis.ftSearch( "idx:bicycle", query3).getDocuments(); @@ -264,6 +264,24 @@ public void run() { assertEquals("Validate bike id", "bicycle:4", result5.get(0).getId()); // REMOVE_END + + // STEP_START simple_aggregation + AggregationBuilder ab = new AggregationBuilder("*") + .groupBy("@condition", Reducers.count().as("count")); + AggregationResult ar = jedis.ftAggregate("idx:bicycle", ab); + for (int i = 0; i < ar.getTotalResults(); i++) { + System.out.println( + ar.getRow(i).getString("condition") + + " - " + + ar.getRow(i).getString("count")); + } + // Prints: + // refurbished - 1 + // used - 5 + // new - 4 + assertEquals("Validate aggregation results", 3, ar.getTotalResults()); + // STEP_END + jedis.close(); } } \ No newline at end of file From ec0e1a3492a0690d16fe16e504ad293f631a6c92 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Tue, 18 Jul 2023 16:41:18 +0200 Subject: [PATCH 07/19] Fix formatting in doc tests --- .../examples/SearchQuickstartExample.java | 467 ++++++++---------- .../java/io/redis/examples/SetGetExample.java | 41 +- 2 files changed, 221 insertions(+), 287 deletions(-) diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 09323c2685..1ba6e7ee1e 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -1,4 +1,4 @@ -//EXAMPLE: search_quickstart +// EXAMPLE: search_quickstart package io.redis.examples; import java.math.BigDecimal; @@ -9,279 +9,214 @@ import redis.clients.jedis.search.*; import redis.clients.jedis.search.aggr.*; import redis.clients.jedis.search.schemafields.*; -//REMOVE_START +// REMOVE_START import org.junit.Test; import static org.junit.Assert.assertEquals; -//REMOVE_END -class Bicycle { - public String brand; - public String model; - public BigDecimal price; - public String description; - public String condition; +// REMOVE_END - public Bicycle(String brand, String model, BigDecimal price, String condition, String description) { - this.brand = brand; - this.model = model; - this.price = price; - this.condition = condition; - this.description = description; - } +class Bicycle { + public String brand; + public String model; + public BigDecimal price; + public String description; + public String condition; + + public Bicycle(String brand, String model, BigDecimal price, String condition, String description) { + this.brand = brand; + this.model = model; + this.price = price; + this.condition = condition; + this.description = description; + } } public class SearchQuickstartExample { - @Test - public void run() { - // STEP_START connect - // UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); - JedisPooled jedis = new JedisPooled("localhost", 6379); - // STEP_END - // REMOVE_START - try { - jedis.ftDropIndex( "idx:bicycle"); - } catch (JedisDataException e) { - System.out.println("Can't connect to Redis: " + e.getMessage()); - } - // REMOVE_END - - // STEP_START create_index - SchemaField[] schema = { - TextField.of("$.brand").as("brand"), - TextField.of("$.model").as("model"), - TextField.of("$.description").as("description"), - NumericField.of("$.price").as("price"), - TagField.of("$.condition").as("condition") - }; - - jedis.ftCreate("idx:bicycle", - FTCreateParams.createParams() - .on(IndexDataType.JSON) - .addPrefix("bicycle:"), - schema - ); - // STEP_END - - Bicycle[] bicycles = { - new Bicycle( - "Velorim", - "Jigger", - new BigDecimal(270), - "new", - "Small and powerful, the Jigger is the best ride " + - "for the smallest of tikes! This is the tiniest " + - "kids’ pedal bike on the market available without" + - " a coaster brake, the Jigger is the vehicle of " + - "choice for the rare tenacious little rider " + - "raring to go." - ), - new Bicycle( - "Bicyk", - "Hillcraft", - new BigDecimal(1200), - "used", - "Kids want to ride with as little weight as possible." + - " Especially on an incline! They may be at the age " + - "when a 27.5 inch wheel bike is just too clumsy coming " + - "off a 24 inch bike. The Hillcraft 26 is just the solution" + - " they need!" - ), - new Bicycle( - "Nord", - "Chook air 5", - new BigDecimal(815), - "used", - "The Chook Air 5 gives kids aged six years and older " + - "a durable and uberlight mountain bike for their first" + - " experience on tracks and easy cruising through forests" + - " and fields. The lower top tube makes it easy to mount" + - " and dismount in any situation, giving your kids greater" + - " safety on the trails." - ), - new Bicycle( - "Eva", - "Eva 291", - new BigDecimal(3400), - "used", - "The sister company to Nord, Eva launched in 2005 as the" + - " first and only women-dedicated bicycle brand. Designed" + - " by women for women, allEva bikes are optimized for the" + - " feminine physique using analytics from a body metrics" + - " database. If you like 29ers, try the Eva 291. It's a " + - "brand new bike for 2022.. This full-suspension, " + - "cross-country ride has been designed for velocity. The" + - " 291 has 100mm of front and rear travel, a superlight " + - "aluminum frame and fast-rolling 29-inch wheels. Yippee!" - ), - new Bicycle( - "Noka Bikes", - "Kahuna", - new BigDecimal(3200), - "used", - "Whether you want to try your hand at XC racing or are " + - "looking for a lively trail bike that's just as inspiring" + - " on the climbs as it is over rougher ground, the Wilder" + - " is one heck of a bike built specifically for short women." + - " Both the frames and components have been tweaked to " + - "include a women’s saddle, different bars and unique " + - "colourway." - ), - new Bicycle( - "Breakout", - "XBN 2.1 Alloy", - new BigDecimal(810), - "new", - "The XBN 2.1 Alloy is our entry-level road bike – but that’s" + - " not to say that it’s a basic machine. With an internal " + - "weld aluminium frame, a full carbon fork, and the slick-shifting" + - " Claris gears from Shimano’s, this is a bike which doesn’t" + - " break the bank and delivers craved performance." - ), - new Bicycle( - "ScramBikes", - "WattBike", - new BigDecimal(2300), - "new", - "The WattBike is the best e-bike for people who still feel young" + - " at heart. It has a Bafang 1000W mid-drive system and a 48V" + - " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" + - " more than 60 miles on one charge. It’s great for tackling hilly" + - " terrain or if you just fancy a more leisurely ride. With three" + - " working modes, you can choose between E-bike, assisted bicycle," + - " and normal bike modes." - ), - new Bicycle( - "Peaknetic", - "Secto", - new BigDecimal(430), - "new", - "If you struggle with stiff fingers or a kinked neck or back after" + - " a few minutes on the road, this lightweight, aluminum bike" + - " alleviates those issues and allows you to enjoy the ride. From" + - " the ergonomic grips to the lumbar-supporting seat position, the" + - " Roll Low-Entry offers incredible comfort. The rear-inclined seat" + - " tube facilitates stability by allowing you to put a foot on the" + - " ground to balance at a stop, and the low step-over frame makes it" + - " accessible for all ability and mobility levels. The saddle is" + - " very soft, with a wide back to support your hip joints and a" + - " cutout in the center to redistribute that pressure. Rim brakes" + - " deliver satisfactory braking control, and the wide tires provide" + - " a smooth, stable ride on paved roads and gravel. Rack and fender" + - " mounts facilitate setting up the Roll Low-Entry as your preferred" + - " commuter, and the BMX-like handlebar offers space for mounting a" + - " flashlight, bell, or phone holder." - ), - new Bicycle( - "nHill", - "Summit", - new BigDecimal(1200), - "new", - "This budget mountain bike from nHill performs well both on bike" + - " paths and on the trail. The fork with 100mm of travel absorbs" + - " rough terrain. Fat Kenda Booster tires give you grip in corners" + - " and on wet trails. The Shimano Tourney drivetrain offered enough" + - " gears for finding a comfortable pace to ride uphill, and the" + - " Tektro hydraulic disc brakes break smoothly. Whether you want an" + - " affordable bike that you can take to work, but also take trail in" + - " mountains on the weekends or you’re just after a stable," + - " comfortable ride for the bike path, the Summit gives a good value" + - " for money." - ), - new Bicycle( - "ThrillCycle", - "BikeShind", - new BigDecimal(815), - "refurbished", - "An artsy, retro-inspired bicycle that’s as functional as it is" + - " pretty: The ThrillCycle steel frame offers a smooth ride. A" + - " 9-speed drivetrain has enough gears for coasting in the city, but" + - " we wouldn’t suggest taking it to the mountains. Fenders protect" + - " you from mud, and a rear basket lets you transport groceries," + - " flowers and books. The ThrillCycle comes with a limited lifetime" + - " warranty, so this little guy will last you long past graduation." - ), - }; - - // STEP_START add_documents - for (int i = 0; i < bicycles.length; i++) { - jedis.jsonSetWithEscape(String.format("bicycle:%d", i), bicycles[i]); - } - // STEP_END - - // STEP_START wildcard_query - Query query1 = new Query("*"); - List < Document > result1 = jedis.ftSearch("idx:bicycle", query1).getDocuments(); - System.out.println("Documents found:" + result1.size()); - // Prints: Documents found: 10 - // STEP_END - // REMOVE_START - assertEquals("Validate total results", 10, result1.size()); - // REMOVE_END - - // STEP_START query_single_term - Query query2 = new Query("@model:Jigger"); - List < Document > result2 = jedis.ftSearch( - "idx:bicycle", query2).getDocuments(); - System.out.println(result2); - // Prints: [id:bicycle:0, score: 1.0, payload:null, properties:[$={"brand":"Velorim","model":"Jigger","price":270,"description":"Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.","condition":"new"}]] - // STEP_END - // REMOVE_START - assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); - // REMOVE_END - - // STEP_START query_single_term_limit_fields - Query query3 = new Query("@model:Jigger").returnFields("price"); - List < Document > result3 = jedis.ftSearch( - "idx:bicycle", query3).getDocuments(); - System.out.println(result3); - // Prints: [id:bicycle:0, score: 1.0, payload:null, properties:[price=270]] - // STEP_END - // REMOVE_START - assertEquals("Validate cargo bike id", "bicycle:0", result3.get(0).getId()); - // REMOVE_END - - // STEP_START query_single_term_and_num_range - Query query4 = new Query("basic @price:[500 1000]"); - List < Document > result4 = jedis.ftSearch( - "idx:bicycle", query4).getDocuments(); - System.out.println(result4); - // Prints: [id:bicycle:5, score: 1.0, payload:null, properties:[$={"brand":"Breakout","model":"XBN 2.1 Alloy","price":810,"description":"The XBN 2.1 Alloy is our entry-level road bike – but that’s not to say that it’s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance.","condition":"new"}]] - // STEP_END - // REMOVE_START - assertEquals("Validate bike id", "bicycle:5", result4.get(0).getId()); - // REMOVE_END - - // STEP_START query_exact_matching - Query query5 = new Query("@brand:\"Noka Bikes\""); - List < Document > result5 = jedis.ftSearch( - "idx:bicycle", query5).getDocuments(); - System.out.println(result5); - // Prints: [id:bicycle:4, score: 1.0, payload:null, properties:[$={"brand":"Noka Bikes","model":"Kahuna","price":3200,"description":"Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a women’s saddle, different bars and unique colourway.","condition":"used"}]] - // STEP_END - // REMOVE_START - assertEquals("Validate bike id", "bicycle:4", result5.get(0).getId()); - // REMOVE_END - - - // STEP_START simple_aggregation - AggregationBuilder ab = new AggregationBuilder("*") - .groupBy("@condition", Reducers.count().as("count")); - AggregationResult ar = jedis.ftAggregate("idx:bicycle", ab); - for (int i = 0; i < ar.getTotalResults(); i++) { - System.out.println( - ar.getRow(i).getString("condition") - + " - " - + ar.getRow(i).getString("count")); - } - // Prints: - // refurbished - 1 - // used - 5 - // new - 4 - assertEquals("Validate aggregation results", 3, ar.getTotalResults()); - // STEP_END - - jedis.close(); + @Test + public void run() { + // STEP_START connect + // UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + JedisPooled jedis = new JedisPooled("localhost", 6379); + // STEP_END + // REMOVE_START + try { + jedis.ftDropIndex("idx:bicycle"); + } catch (JedisDataException e) { + System.out.println("Can't connect to Redis: " + e.getMessage()); + } + // REMOVE_END + + // STEP_START create_index + SchemaField[] schema = { TextField.of("$.brand").as("brand"), + TextField.of("$.model").as("model"), TextField.of("$.description").as("description"), + NumericField.of("$.price").as("price"), TagField.of("$.condition").as("condition") }; + + jedis.ftCreate("idx:bicycle", + FTCreateParams.createParams().on(IndexDataType.JSON).addPrefix("bicycle:"), schema); + // STEP_END + + Bicycle[] bicycles = { + new Bicycle("Velorim", "Jigger", new BigDecimal(270), "new", + "Small and powerful, the Jigger is the best ride " + + "for the smallest of tikes! This is the tiniest " + + "kids’ pedal bike on the market available without" + + " a coaster brake, the Jigger is the vehicle of " + + "choice for the rare tenacious little rider " + "raring to go."), + new Bicycle("Bicyk", "Hillcraft", new BigDecimal(1200), "used", + "Kids want to ride with as little weight as possible." + + " Especially on an incline! They may be at the age " + + "when a 27.5 inch wheel bike is just too clumsy coming " + + "off a 24 inch bike. The Hillcraft 26 is just the solution" + " they need!"), + new Bicycle("Nord", "Chook air 5", new BigDecimal(815), "used", + "The Chook Air 5 gives kids aged six years and older " + + "a durable and uberlight mountain bike for their first" + + " experience on tracks and easy cruising through forests" + + " and fields. The lower top tube makes it easy to mount" + + " and dismount in any situation, giving your kids greater" + + " safety on the trails."), + new Bicycle("Eva", "Eva 291", new BigDecimal(3400), "used", + "The sister company to Nord, Eva launched in 2005 as the" + + " first and only women-dedicated bicycle brand. Designed" + + " by women for women, allEva bikes are optimized for the" + + " feminine physique using analytics from a body metrics" + + " database. If you like 29ers, try the Eva 291. It's a " + + "brand new bike for 2022.. This full-suspension, " + + "cross-country ride has been designed for velocity. The" + + " 291 has 100mm of front and rear travel, a superlight " + + "aluminum frame and fast-rolling 29-inch wheels. Yippee!"), + new Bicycle("Noka Bikes", "Kahuna", new BigDecimal(3200), "used", + "Whether you want to try your hand at XC racing or are " + + "looking for a lively trail bike that's just as inspiring" + + " on the climbs as it is over rougher ground, the Wilder" + + " is one heck of a bike built specifically for short women." + + " Both the frames and components have been tweaked to " + + "include a women’s saddle, different bars and unique " + "colourway."), + new Bicycle("Breakout", "XBN 2.1 Alloy", new BigDecimal(810), "new", + "The XBN 2.1 Alloy is our entry-level road bike – but that’s" + + " not to say that it’s a basic machine. With an internal " + + "weld aluminium frame, a full carbon fork, and the slick-shifting" + + " Claris gears from Shimano’s, this is a bike which doesn’t" + + " break the bank and delivers craved performance."), + new Bicycle("ScramBikes", "WattBike", new BigDecimal(2300), "new", + "The WattBike is the best e-bike for people who still feel young" + + " at heart. It has a Bafang 1000W mid-drive system and a 48V" + + " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" + + " more than 60 miles on one charge. It’s great for tackling hilly" + + " terrain or if you just fancy a more leisurely ride. With three" + + " working modes, you can choose between E-bike, assisted bicycle," + + " and normal bike modes."), + new Bicycle("Peaknetic", "Secto", new BigDecimal(430), "new", + "If you struggle with stiff fingers or a kinked neck or back after" + + " a few minutes on the road, this lightweight, aluminum bike" + + " alleviates those issues and allows you to enjoy the ride. From" + + " the ergonomic grips to the lumbar-supporting seat position, the" + + " Roll Low-Entry offers incredible comfort. The rear-inclined seat" + + " tube facilitates stability by allowing you to put a foot on the" + + " ground to balance at a stop, and the low step-over frame makes it" + + " accessible for all ability and mobility levels. The saddle is" + + " very soft, with a wide back to support your hip joints and a" + + " cutout in the center to redistribute that pressure. Rim brakes" + + " deliver satisfactory braking control, and the wide tires provide" + + " a smooth, stable ride on paved roads and gravel. Rack and fender" + + " mounts facilitate setting up the Roll Low-Entry as your preferred" + + " commuter, and the BMX-like handlebar offers space for mounting a" + + " flashlight, bell, or phone holder."), + new Bicycle("nHill", "Summit", new BigDecimal(1200), "new", + "This budget mountain bike from nHill performs well both on bike" + + " paths and on the trail. The fork with 100mm of travel absorbs" + + " rough terrain. Fat Kenda Booster tires give you grip in corners" + + " and on wet trails. The Shimano Tourney drivetrain offered enough" + + " gears for finding a comfortable pace to ride uphill, and the" + + " Tektro hydraulic disc brakes break smoothly. Whether you want an" + + " affordable bike that you can take to work, but also take trail in" + + " mountains on the weekends or you’re just after a stable," + + " comfortable ride for the bike path, the Summit gives a good value" + + " for money."), + new Bicycle("ThrillCycle", "BikeShind", new BigDecimal(815), "refurbished", + "An artsy, retro-inspired bicycle that’s as functional as it is" + + " pretty: The ThrillCycle steel frame offers a smooth ride. A" + + " 9-speed drivetrain has enough gears for coasting in the city, but" + + " we wouldn’t suggest taking it to the mountains. Fenders protect" + + " you from mud, and a rear basket lets you transport groceries," + + " flowers and books. The ThrillCycle comes with a limited lifetime" + + " warranty, so this little guy will last you long past graduation."), }; + + // STEP_START add_documents + for (int i = 0; i < bicycles.length; i++) { + jedis.jsonSetWithEscape(String.format("bicycle:%d", i), bicycles[i]); + } + // STEP_END + + // STEP_START wildcard_query + Query query1 = new Query("*"); + List result1 = jedis.ftSearch("idx:bicycle", query1).getDocuments(); + System.out.println("Documents found:" + result1.size()); + // Prints: Documents found: 10 + // STEP_END + // REMOVE_START + assertEquals("Validate total results", 10, result1.size()); + // REMOVE_END + + // STEP_START query_single_term + Query query2 = new Query("@model:Jigger"); + List result2 = jedis.ftSearch("idx:bicycle", query2).getDocuments(); + System.out.println(result2); + // Prints: [id:bicycle:0, score: 1.0, payload:null, + // properties:[$={"brand":"Velorim","model":"Jigger","price":270,"description":"Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.","condition":"new"}]] + // STEP_END + // REMOVE_START + assertEquals("Validate bike id", "bicycle:0", result2.get(0).getId()); + // REMOVE_END + + // STEP_START query_single_term_limit_fields + Query query3 = new Query("@model:Jigger").returnFields("price"); + List result3 = jedis.ftSearch("idx:bicycle", query3).getDocuments(); + System.out.println(result3); + // Prints: [id:bicycle:0, score: 1.0, payload:null, properties:[price=270]] + // STEP_END + // REMOVE_START + assertEquals("Validate cargo bike id", "bicycle:0", result3.get(0).getId()); + // REMOVE_END + + // STEP_START query_single_term_and_num_range + Query query4 = new Query("basic @price:[500 1000]"); + List result4 = jedis.ftSearch("idx:bicycle", query4).getDocuments(); + System.out.println(result4); + // Prints: [id:bicycle:5, score: 1.0, payload:null, + // properties:[$={"brand":"Breakout","model":"XBN 2.1 Alloy","price":810,"description":"The XBN 2.1 Alloy is our entry-level road bike – but that’s not to say that it’s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano’s, this is a bike which doesn’t break the bank and delivers craved performance.","condition":"new"}]] + // STEP_END + // REMOVE_START + assertEquals("Validate bike id", "bicycle:5", result4.get(0).getId()); + // REMOVE_END + + // STEP_START query_exact_matching + Query query5 = new Query("@brand:\"Noka Bikes\""); + List result5 = jedis.ftSearch("idx:bicycle", query5).getDocuments(); + System.out.println(result5); + // Prints: [id:bicycle:4, score: 1.0, payload:null, + // properties:[$={"brand":"Noka Bikes","model":"Kahuna","price":3200,"description":"Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a women’s saddle, different bars and unique colourway.","condition":"used"}]] + // STEP_END + // REMOVE_START + assertEquals("Validate bike id", "bicycle:4", result5.get(0).getId()); + // REMOVE_END + + // STEP_START simple_aggregation + AggregationBuilder ab = new AggregationBuilder("*").groupBy("@condition", + Reducers.count().as("count")); + AggregationResult ar = jedis.ftAggregate("idx:bicycle", ab); + for (int i = 0; i < ar.getTotalResults(); i++) { + System.out.println(ar.getRow(i).getString("condition") + " - " + + ar.getRow(i).getString("count")); } + // Prints: + // refurbished - 1 + // used - 5 + // new - 4 + assertEquals("Validate aggregation results", 3, ar.getTotalResults()); + // STEP_END + + jedis.close(); + } } \ No newline at end of file diff --git a/src/test/java/io/redis/examples/SetGetExample.java b/src/test/java/io/redis/examples/SetGetExample.java index 282f1aebb2..fa4485ef46 100644 --- a/src/test/java/io/redis/examples/SetGetExample.java +++ b/src/test/java/io/redis/examples/SetGetExample.java @@ -1,36 +1,35 @@ -//EXAMPLE: set_and_get -//HIDE_START +// EXAMPLE: set_and_get +// HIDE_START package io.redis.examples; import redis.clients.jedis.UnifiedJedis; -//REMOVE_START +// REMOVE_START import org.junit.Test; import static org.junit.Assert.assertEquals; -//REMOVE_END + +// REMOVE_END public class SetGetExample { - @Test - public void run() { + @Test + public void run() { - UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); - //HIDE_END - String status = jedis.set("bike:1", "Process 134"); + // HIDE_END + String status = jedis.set("bike:1", "Process 134"); - if ("OK".equals(status)) - System.out.println("Successfully added a bike."); + if ("OK".equals(status)) System.out.println("Successfully added a bike."); - String value = jedis.get("bike:1"); + String value = jedis.get("bike:1"); - if ( value != null) - System.out.println("The name of the bike is: " + value + "."); - //HIDE_START + if (value != null) System.out.println("The name of the bike is: " + value + "."); + // HIDE_START - //REMOVE_START - assertEquals("OK", status); - assertEquals("Process 134", value); - //REMOVE_END - } + // REMOVE_START + assertEquals("OK", status); + assertEquals("Process 134", value); + // REMOVE_END + } } -//HIDE_END +// HIDE_END From 983b677dc43e4c517b22dcea41fa18c80201febd Mon Sep 17 00:00:00 2001 From: Brian Sam-Bodden Date: Thu, 20 Jul 2023 05:47:28 -0700 Subject: [PATCH 08/19] Add Hashes and String Example (#3477) * Add Hashes Example * Fixes examples for hash tutorial * Refactor Hash Tutorial to Java 8 (from 17) * Change indentation to 4 spaces * Add Strings Example * Reformat samples to 2-space indentation --------- Co-authored-by: Elena Kolevska --- .../java/io/redis/examples/HashExample.java | 101 ++++++++ .../examples/SearchQuickstartExample.java | 238 +++++++++++------- .../java/io/redis/examples/StringExample.java | 75 ++++++ 3 files changed, 326 insertions(+), 88 deletions(-) create mode 100644 src/test/java/io/redis/examples/HashExample.java create mode 100644 src/test/java/io/redis/examples/StringExample.java diff --git a/src/test/java/io/redis/examples/HashExample.java b/src/test/java/io/redis/examples/HashExample.java new file mode 100644 index 0000000000..2d02849cf4 --- /dev/null +++ b/src/test/java/io/redis/examples/HashExample.java @@ -0,0 +1,101 @@ +//EXAMPLE: hash_tutorial +package io.redis.examples; + +import redis.clients.jedis.UnifiedJedis; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +//REMOVE_START +import org.junit.Test; +import static org.junit.Assert.assertEquals; +//REMOVE_END + +public class HashExample { + @Test + public void run() { + try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) { + // REMOVE_START + jedis.del("bike:1", "bike:1:stats"); + // REMOVE_END + + // STEP_START set_get_all + Map bike1 = new HashMap<>(); + bike1.put("model", "Deimos"); + bike1.put("brand", "Ergonom"); + bike1.put("type", "Enduro bikes"); + bike1.put("price", "4972"); + + Long res1 = jedis.hset("bike:1", bike1); + System.out.println(res1); // 4 + + String res2 = jedis.hget("bike:1", "model"); + System.out.println(res2); // Deimos + + String res3 = jedis.hget("bike:1", "price"); + System.out.println(res3); // 4972 + + Map res4 = jedis.hgetAll("bike:1"); + System.out.println(res4); // {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos} + // STEP_END + + // REMOVE_START + assertEquals(4, res1.longValue()); + assertEquals("Deimos", res2); + assertEquals("4972", res3); + assertEquals("Deimos", res4.get("model")); + assertEquals("Ergonom", res4.get("brand")); + assertEquals("Enduro bikes", res4.get("type")); + assertEquals("4972", res4.get("price")); + // REMOVE_END + + // STEP_START hmget + List res5 = jedis.hmget("bike:1", "model", "price"); + System.out.println(res5); // [Deimos, 4972] + // STEP_END + + // REMOVE_START + assert res5.toString().equals("[Deimos, 4972]"); + // REMOVE_END + + // STEP_START hincrby + Long res6 = jedis.hincrBy("bike:1", "price", 100); + System.out.println(res6); // 5072 + Long res7 = jedis.hincrBy("bike:1", "price", -100); + System.out.println(res7); // 4972 + // STEP_END + + // REMOVE_START + assertEquals(5072, res6.longValue()); + assertEquals(4972, res7.longValue()); + // REMOVE_END + + // STEP_START incrby_get_mget + Long res8 = jedis.hincrBy("bike:1:stats", "rides", 1); + System.out.println(res8); // 1 + Long res9 = jedis.hincrBy("bike:1:stats", "rides", 1); + System.out.println(res9); // 2 + Long res10 = jedis.hincrBy("bike:1:stats", "rides", 1); + System.out.println(res10); // 3 + Long res11 = jedis.hincrBy("bike:1:stats", "crashes", 1); + System.out.println(res11); // 1 + Long res12 = jedis.hincrBy("bike:1:stats", "owners", 1); + System.out.println(res12); // 1 + String res13 = jedis.hget("bike:1:stats", "rides"); + System.out.println(res13); // 3 + List res14 = jedis.hmget("bike:1:stats", "crashes", "owners"); + System.out.println(res14); // [1, 1] + // STEP_END + + // REMOVE_START + assertEquals(1, res8.longValue()); + assertEquals(2, res9.longValue()); + assertEquals(3, res10.longValue()); + assertEquals(1, res11.longValue()); + assertEquals(1, res12.longValue()); + assertEquals("3", res13); + assertEquals("[1, 1]", res14.toString()); + // REMOVE_END + } + } +} diff --git a/src/test/java/io/redis/examples/SearchQuickstartExample.java b/src/test/java/io/redis/examples/SearchQuickstartExample.java index 1ba6e7ee1e..c53fa2a847 100644 --- a/src/test/java/io/redis/examples/SearchQuickstartExample.java +++ b/src/test/java/io/redis/examples/SearchQuickstartExample.java @@ -49,99 +49,161 @@ public void run() { // REMOVE_END // STEP_START create_index - SchemaField[] schema = { TextField.of("$.brand").as("brand"), - TextField.of("$.model").as("model"), TextField.of("$.description").as("description"), - NumericField.of("$.price").as("price"), TagField.of("$.condition").as("condition") }; + SchemaField[] schema = { + TextField.of("$.brand").as("brand"), + TextField.of("$.model").as("model"), + TextField.of("$.description").as("description"), + NumericField.of("$.price").as("price"), + TagField.of("$.condition").as("condition") + }; jedis.ftCreate("idx:bicycle", - FTCreateParams.createParams().on(IndexDataType.JSON).addPrefix("bicycle:"), schema); + FTCreateParams.createParams() + .on(IndexDataType.JSON) + .addPrefix("bicycle:"), + schema + ); // STEP_END Bicycle[] bicycles = { - new Bicycle("Velorim", "Jigger", new BigDecimal(270), "new", - "Small and powerful, the Jigger is the best ride " - + "for the smallest of tikes! This is the tiniest " - + "kids’ pedal bike on the market available without" - + " a coaster brake, the Jigger is the vehicle of " - + "choice for the rare tenacious little rider " + "raring to go."), - new Bicycle("Bicyk", "Hillcraft", new BigDecimal(1200), "used", - "Kids want to ride with as little weight as possible." - + " Especially on an incline! They may be at the age " - + "when a 27.5 inch wheel bike is just too clumsy coming " - + "off a 24 inch bike. The Hillcraft 26 is just the solution" + " they need!"), - new Bicycle("Nord", "Chook air 5", new BigDecimal(815), "used", - "The Chook Air 5 gives kids aged six years and older " - + "a durable and uberlight mountain bike for their first" - + " experience on tracks and easy cruising through forests" - + " and fields. The lower top tube makes it easy to mount" - + " and dismount in any situation, giving your kids greater" - + " safety on the trails."), - new Bicycle("Eva", "Eva 291", new BigDecimal(3400), "used", - "The sister company to Nord, Eva launched in 2005 as the" - + " first and only women-dedicated bicycle brand. Designed" - + " by women for women, allEva bikes are optimized for the" - + " feminine physique using analytics from a body metrics" - + " database. If you like 29ers, try the Eva 291. It's a " - + "brand new bike for 2022.. This full-suspension, " - + "cross-country ride has been designed for velocity. The" - + " 291 has 100mm of front and rear travel, a superlight " - + "aluminum frame and fast-rolling 29-inch wheels. Yippee!"), - new Bicycle("Noka Bikes", "Kahuna", new BigDecimal(3200), "used", - "Whether you want to try your hand at XC racing or are " - + "looking for a lively trail bike that's just as inspiring" - + " on the climbs as it is over rougher ground, the Wilder" - + " is one heck of a bike built specifically for short women." - + " Both the frames and components have been tweaked to " - + "include a women’s saddle, different bars and unique " + "colourway."), - new Bicycle("Breakout", "XBN 2.1 Alloy", new BigDecimal(810), "new", - "The XBN 2.1 Alloy is our entry-level road bike – but that’s" - + " not to say that it’s a basic machine. With an internal " - + "weld aluminium frame, a full carbon fork, and the slick-shifting" - + " Claris gears from Shimano’s, this is a bike which doesn’t" - + " break the bank and delivers craved performance."), - new Bicycle("ScramBikes", "WattBike", new BigDecimal(2300), "new", - "The WattBike is the best e-bike for people who still feel young" - + " at heart. It has a Bafang 1000W mid-drive system and a 48V" - + " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" - + " more than 60 miles on one charge. It’s great for tackling hilly" - + " terrain or if you just fancy a more leisurely ride. With three" - + " working modes, you can choose between E-bike, assisted bicycle," - + " and normal bike modes."), - new Bicycle("Peaknetic", "Secto", new BigDecimal(430), "new", - "If you struggle with stiff fingers or a kinked neck or back after" - + " a few minutes on the road, this lightweight, aluminum bike" - + " alleviates those issues and allows you to enjoy the ride. From" - + " the ergonomic grips to the lumbar-supporting seat position, the" - + " Roll Low-Entry offers incredible comfort. The rear-inclined seat" - + " tube facilitates stability by allowing you to put a foot on the" - + " ground to balance at a stop, and the low step-over frame makes it" - + " accessible for all ability and mobility levels. The saddle is" - + " very soft, with a wide back to support your hip joints and a" - + " cutout in the center to redistribute that pressure. Rim brakes" - + " deliver satisfactory braking control, and the wide tires provide" - + " a smooth, stable ride on paved roads and gravel. Rack and fender" - + " mounts facilitate setting up the Roll Low-Entry as your preferred" - + " commuter, and the BMX-like handlebar offers space for mounting a" - + " flashlight, bell, or phone holder."), - new Bicycle("nHill", "Summit", new BigDecimal(1200), "new", - "This budget mountain bike from nHill performs well both on bike" - + " paths and on the trail. The fork with 100mm of travel absorbs" - + " rough terrain. Fat Kenda Booster tires give you grip in corners" - + " and on wet trails. The Shimano Tourney drivetrain offered enough" - + " gears for finding a comfortable pace to ride uphill, and the" - + " Tektro hydraulic disc brakes break smoothly. Whether you want an" - + " affordable bike that you can take to work, but also take trail in" - + " mountains on the weekends or you’re just after a stable," - + " comfortable ride for the bike path, the Summit gives a good value" - + " for money."), - new Bicycle("ThrillCycle", "BikeShind", new BigDecimal(815), "refurbished", - "An artsy, retro-inspired bicycle that’s as functional as it is" - + " pretty: The ThrillCycle steel frame offers a smooth ride. A" - + " 9-speed drivetrain has enough gears for coasting in the city, but" - + " we wouldn’t suggest taking it to the mountains. Fenders protect" - + " you from mud, and a rear basket lets you transport groceries," - + " flowers and books. The ThrillCycle comes with a limited lifetime" - + " warranty, so this little guy will last you long past graduation."), }; + new Bicycle( + "Velorim", + "Jigger", + new BigDecimal(270), + "new", + "Small and powerful, the Jigger is the best ride " + + "for the smallest of tikes! This is the tiniest " + + "kids’ pedal bike on the market available without" + + " a coaster brake, the Jigger is the vehicle of " + + "choice for the rare tenacious little rider " + + "raring to go." + ), + new Bicycle( + "Bicyk", + "Hillcraft", + new BigDecimal(1200), + "used", + "Kids want to ride with as little weight as possible." + + " Especially on an incline! They may be at the age " + + "when a 27.5 inch wheel bike is just too clumsy coming " + + "off a 24 inch bike. The Hillcraft 26 is just the solution" + + " they need!" + ), + new Bicycle( + "Nord", + "Chook air 5", + new BigDecimal(815), + "used", + "The Chook Air 5 gives kids aged six years and older " + + "a durable and uberlight mountain bike for their first" + + " experience on tracks and easy cruising through forests" + + " and fields. The lower top tube makes it easy to mount" + + " and dismount in any situation, giving your kids greater" + + " safety on the trails." + ), + new Bicycle( + "Eva", + "Eva 291", + new BigDecimal(3400), + "used", + "The sister company to Nord, Eva launched in 2005 as the" + + " first and only women-dedicated bicycle brand. Designed" + + " by women for women, allEva bikes are optimized for the" + + " feminine physique using analytics from a body metrics" + + " database. If you like 29ers, try the Eva 291. It's a " + + "brand new bike for 2022.. This full-suspension, " + + "cross-country ride has been designed for velocity. The" + + " 291 has 100mm of front and rear travel, a superlight " + + "aluminum frame and fast-rolling 29-inch wheels. Yippee!" + ), + new Bicycle( + "Noka Bikes", + "Kahuna", + new BigDecimal(3200), + "used", + "Whether you want to try your hand at XC racing or are " + + "looking for a lively trail bike that's just as inspiring" + + " on the climbs as it is over rougher ground, the Wilder" + + " is one heck of a bike built specifically for short women." + + " Both the frames and components have been tweaked to " + + "include a women’s saddle, different bars and unique " + + "colourway." + ), + new Bicycle( + "Breakout", + "XBN 2.1 Alloy", + new BigDecimal(810), + "new", + "The XBN 2.1 Alloy is our entry-level road bike – but that’s" + + " not to say that it’s a basic machine. With an internal " + + "weld aluminium frame, a full carbon fork, and the slick-shifting" + + " Claris gears from Shimano’s, this is a bike which doesn’t" + + " break the bank and delivers craved performance." + ), + new Bicycle( + "ScramBikes", + "WattBike", + new BigDecimal(2300), + "new", + "The WattBike is the best e-bike for people who still feel young" + + " at heart. It has a Bafang 1000W mid-drive system and a 48V" + + " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" + + " more than 60 miles on one charge. It’s great for tackling hilly" + + " terrain or if you just fancy a more leisurely ride. With three" + + " working modes, you can choose between E-bike, assisted bicycle," + + " and normal bike modes." + ), + new Bicycle( + "Peaknetic", + "Secto", + new BigDecimal(430), + "new", + "If you struggle with stiff fingers or a kinked neck or back after" + + " a few minutes on the road, this lightweight, aluminum bike" + + " alleviates those issues and allows you to enjoy the ride. From" + + " the ergonomic grips to the lumbar-supporting seat position, the" + + " Roll Low-Entry offers incredible comfort. The rear-inclined seat" + + " tube facilitates stability by allowing you to put a foot on the" + + " ground to balance at a stop, and the low step-over frame makes it" + + " accessible for all ability and mobility levels. The saddle is" + + " very soft, with a wide back to support your hip joints and a" + + " cutout in the center to redistribute that pressure. Rim brakes" + + " deliver satisfactory braking control, and the wide tires provide" + + " a smooth, stable ride on paved roads and gravel. Rack and fender" + + " mounts facilitate setting up the Roll Low-Entry as your preferred" + + " commuter, and the BMX-like handlebar offers space for mounting a" + + " flashlight, bell, or phone holder." + ), + new Bicycle( + "nHill", + "Summit", + new BigDecimal(1200), + "new", + "This budget mountain bike from nHill performs well both on bike" + + " paths and on the trail. The fork with 100mm of travel absorbs" + + " rough terrain. Fat Kenda Booster tires give you grip in corners" + + " and on wet trails. The Shimano Tourney drivetrain offered enough" + + " gears for finding a comfortable pace to ride uphill, and the" + + " Tektro hydraulic disc brakes break smoothly. Whether you want an" + + " affordable bike that you can take to work, but also take trail in" + + " mountains on the weekends or you’re just after a stable," + + " comfortable ride for the bike path, the Summit gives a good value" + + " for money." + ), + new Bicycle( + "ThrillCycle", + "BikeShind", + new BigDecimal(815), + "refurbished", + "An artsy, retro-inspired bicycle that’s as functional as it is" + + " pretty: The ThrillCycle steel frame offers a smooth ride. A" + + " 9-speed drivetrain has enough gears for coasting in the city, but" + + " we wouldn’t suggest taking it to the mountains. Fenders protect" + + " you from mud, and a rear basket lets you transport groceries," + + " flowers and books. The ThrillCycle comes with a limited lifetime" + + " warranty, so this little guy will last you long past graduation." + ), + }; // STEP_START add_documents for (int i = 0; i < bicycles.length; i++) { diff --git a/src/test/java/io/redis/examples/StringExample.java b/src/test/java/io/redis/examples/StringExample.java new file mode 100644 index 0000000000..d868568a74 --- /dev/null +++ b/src/test/java/io/redis/examples/StringExample.java @@ -0,0 +1,75 @@ +// EXAMPLE: set_tutorial +package io.redis.examples; + +//REMOVE_START +import org.junit.Test; +import static org.junit.Assert.*; +//REMOVE_END + +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.params.SetParams; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class StringExample { + @Test + public void run() { + try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) { + + // STEP_START set_get + String res1 = jedis.set("bike:1", "Deimos"); + System.out.println(res1); // OK + String res2 = jedis.get("bike:1"); + System.out.println(res2); // Deimos + // STEP_END + + // REMOVE_START + assertEquals("OK", res1); + assertEquals("Deimos", res2); + // REMOVE_END + + // STEP_START setnx_xx + Long res3 = jedis.setnx("bike:1", "bike"); + System.out.println(res3); // 0 (because key already exists) + System.out.println(jedis.get("bike:1")); // Deimos (value is unchanged) + String res4 = jedis.set("bike:1", "bike", SetParams.setParams().xx()); // set the value to "bike" if it + // already + // exists + System.out.println(res4); // OK + // STEP_END + + // REMOVE_START + assertEquals(0L, res3.longValue()); + assertEquals("OK", res4); + // REMOVE_END + + // STEP_START mset + String res5 = jedis.mset("bike:1", "Deimos", "bike:2", "Ares", "bike:3", "Vanth"); + System.out.println(res5); // OK + List res6 = jedis.mget("bike:1", "bike:2", "bike:3"); + System.out.println(res6); // [Deimos, Ares, Vanth] + // STEP_END + + // REMOVE_START + assertEquals("OK", res5); + List expected = new ArrayList<>(Arrays.asList("Deimos", "Ares", "Vanth")); + assertEquals(expected, res6); + // REMOVE_END + + // STEP_START incr + jedis.set("total_crashes", "0"); + Long res7 = jedis.incr("total_crashes"); + System.out.println(res7); // 1 + Long res8 = jedis.incrBy("total_crashes", 10); + System.out.println(res8); // 11 + // STEP_END + + // REMOVE_START + assertEquals(1L, res7.longValue()); + assertEquals(11L, res8.longValue()); + // REMOVE_END + } + } +} From 4ddc2151c0332836e0c8cb713f70b126ad0d6127 Mon Sep 17 00:00:00 2001 From: Brian Sam-Bodden Date: Tue, 8 Aug 2023 05:17:04 -0700 Subject: [PATCH 09/19] Add Geospatial data type example (#3487) * Add Geospatial data type example * Add missing REMOVE_END --- .../java/io/redis/examples/GeoExample.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/java/io/redis/examples/GeoExample.java diff --git a/src/test/java/io/redis/examples/GeoExample.java b/src/test/java/io/redis/examples/GeoExample.java new file mode 100644 index 0000000000..f985c7f946 --- /dev/null +++ b/src/test/java/io/redis/examples/GeoExample.java @@ -0,0 +1,60 @@ +//EXAMPLE: geo_tutorial +package io.redis.examples; + +// REMOVE_START +import org.junit.Test; +// REMOVE_END +import redis.clients.jedis.GeoCoordinate; +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.args.GeoUnit; +import redis.clients.jedis.resps.GeoRadiusResponse; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; + +public class GeoExample { + @Test + public void run() { + try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) { + // REMOVE_START + jedis.del("bikes:rentable"); + // REMOVE_END + + // STEP_START geoadd + long res1 = jedis.geoadd("bikes:rentable", -122.27652, 37.805186, "station:1"); + System.out.println(res1); // 1 + + long res2 = jedis.geoadd("bikes:rentable", -122.2674626, 37.8062344, "station:2"); + System.out.println(res2); // 1 + + long res3 = jedis.geoadd("bikes:rentable", -122.2469854, 37.8104049, "station:3"); + System.out.println(res2); // 1 + // STEP_END + + // REMOVE_START + assertEquals(1, res1); + assertEquals(1, res1); + assertEquals(1, res1); + // REMOVE_END + + // STEP_START geosearch + List res4 = jedis.geosearch( + "bikes:rentable", + new GeoCoordinate(-122.27652, 37.805186), + 5, + GeoUnit.KM + ); + List members = res4.stream() // + .map(GeoRadiusResponse::getMemberByString) // + .collect(Collectors.toList()); + System.out.println(members); // [station:1, station:2, station:3] + // STEP_END + + // REMOVE_START + assertEquals("[station:1, station:2, station:3]", members.toString()); + // REMOVE_END + } + } +} From 0526f6c8fa4709aab479342d95b342422937d99d Mon Sep 17 00:00:00 2001 From: Harshvardhan Parmar Date: Wed, 6 Dec 2023 23:04:21 +0530 Subject: [PATCH 10/19] Added example Java code snippets for Streams (#3641) --- .../io/redis/examples/StreamsExample.java | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 src/test/java/io/redis/examples/StreamsExample.java diff --git a/src/test/java/io/redis/examples/StreamsExample.java b/src/test/java/io/redis/examples/StreamsExample.java new file mode 100644 index 0000000000..5190a1ba7d --- /dev/null +++ b/src/test/java/io/redis/examples/StreamsExample.java @@ -0,0 +1,270 @@ +//EXAMPLE: stream_tutorial +//HIDE_START +package io.redis.examples; + +import redis.clients.jedis.StreamEntryID; +import redis.clients.jedis.UnifiedJedis; +//HIDE_END + +//REMOVE_START +import org.junit.Test; +import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.jedis.params.*; +import redis.clients.jedis.resps.*; + +import java.util.*; + +import static org.junit.Assert.assertEquals; +//REMOVE_END + +public class StreamsExample { + + @Test + public void run(){ + + //HIDE_START + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + //HIDE_END + + //REMOVE_START + jedis.del("race:france", "race:italy", "race:usa"); + //REMOVE_END + + // STEP_START xadd + StreamEntryID res1 = jedis.xadd("race:france",new HashMap(){{put("rider","Castilla");put("speed","30.2");put("position","1");put("location_id","1");}} , XAddParams.xAddParams()); + + System.out.println(res1); // >>> 1701760582225-0 + + StreamEntryID res2 = jedis.xadd("race:france",new HashMap(){{put("rider","Norem");put("speed","28.8");put("position","3");put("location_id","1");}} , XAddParams.xAddParams()); + + System.out.println(res2); // >>> 1701760582225-1 + + StreamEntryID res3 = jedis.xadd("race:france",new HashMap(){{put("rider","Prickett");put("speed","29.7");put("position","2");put("location_id","1");}} , XAddParams.xAddParams()); + + System.out.println(res3); // >>> 1701760582226-0 + //STEP_END + + //REMOVE_START + assertEquals(jedis.xlen("race:france"),3); + //REMOVE_END + + //STEP_START xrange + List res4 = jedis.xrange("race:france","1701760582225-0","+",2); + + System.out.println(res4); // >>> [1701760841292-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701760841292-1 {rider=Norem, speed=28.8, location_id=1, position=3}] + //STEP_END + + //STEP_START xread_block + List>> res5= jedis.xread(XReadParams.xReadParams().block(300).count(100),new HashMap(){{put("race:france",new StreamEntryID());}}); + System.out.println( + res5 + ); // >>> [race:france=[1701761996660-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701761996661-0 {rider=Norem, speed=28.8, location_id=1, position=3}, 1701761996661-1 {rider=Prickett, speed=29.7, location_id=1, position=2}]] + //STEP_END + + //STEP_START xadd_2 + StreamEntryID res6 = jedis.xadd("race:france",new HashMap(){{put("rider","Castilla");put("speed","29.9");put("position","2");put("location_id","1");}} , XAddParams.xAddParams()); + System.out.println(res6); // >>> 1701762285679-0 + //STEP_END + + //STEP_START xlen + long res7 = jedis.xlen("race:france"); + System.out.println(res7); // >>> 4 + //STEP_END + + //STEP_START xadd_id + StreamEntryID res8 = jedis.xadd("race:usa", new HashMap(){{put("racer","Castilla");}},XAddParams.xAddParams().id("0-1")); + System.out.println(res8); // >>> 0-1 + + StreamEntryID res9 = jedis.xadd("race:usa", new HashMap(){{put("racer","Norem");}},XAddParams.xAddParams().id("0-2")); + System.out.println(res9); // >>> 0-2 + //STEP_END + + //STEP_START xadd_bad_id + try { + StreamEntryID res10 = jedis.xadd("race:usa", new HashMap(){{put("racer","Prickett");}},XAddParams.xAddParams().id("0-1")); + System.out.println(res10); // >>> 0-1 + } + catch (JedisDataException e){ + System.out.println(e); // >>> ERR The ID specified in XADD is equal or smaller than the target stream top item + } + //STEP_END + + //STEP_START xadd_7 + StreamEntryID res11 = jedis.xadd("race:usa", new HashMap(){{put("racer","Norem");}},XAddParams.xAddParams().id("0-*")); + System.out.println(res11); + //STEP_END + + //STEP_START xrange_all + List res12 = jedis.xrange("race:france","-","+"); + System.out.println( + res12 + ); // >>> [1701764734160-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701764734160-1 {rider=Norem, speed=28.8, location_id=1, position=3}, 1701764734161-0 {rider=Prickett, speed=29.7, location_id=1, position=2}, 1701764734162-0 {rider=Castilla, speed=29.9, location_id=1, position=2}] + //STEP_END + + //STEP_START xrange_time + List res13 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()-1000),String.valueOf(System.currentTimeMillis()+1000)); + System.out.println( + res13 + ); // >>> [1701764734160-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701764734160-1 {rider=Norem, speed=28.8, location_id=1, position=3}, 1701764734161-0 {rider=Prickett, speed=29.7, location_id=1, position=2}, 1701764734162-0 {rider=Castilla, speed=29.9, location_id=1, position=2}] + //STEP_END + + //STEP_START xrange_step_1 + List res14 = jedis.xrange("race:france","-","+",2); + System.out.println(res14); // >>> [1701764887638-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701764887638-1 {rider=Norem, speed=28.8, location_id=1, position=3}] + //STEP_END + + //STEP_START xrange_step_2 + List res15 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()-1000)+"-0","+",2); + System.out.println(res15); // >>> [1701764887638-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701764887638-1 {rider=Norem, speed=28.8, location_id=1, position=3}] + //STEP_END + + //STEP_START xrange_empty + List res16 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()+1000)+"-0","+",2); + System.out.println(res16); // >>> [] + // STEP_END + + //STEP_START xrevrange + List res17 = jedis.xrevrange("race:france","+","-",1); + System.out.println(res17); // >>> [1701765218592-0 {rider=Castilla, speed=29.9, location_id=1, position=2}] + //STEP_END + + //STEP_START xread + List>> res18= jedis.xread(XReadParams.xReadParams().count(2),new HashMap(){{put("race:france",new StreamEntryID());}}); + System.out.println( + res18 + ); // >>> [race:france=[1701765384638-0 {rider=Castilla, speed=30.2, location_id=1, position=1}, 1701765384638-1 {rider=Norem, speed=28.8, location_id=1, position=3}]] + //STEP_END + + //STEP_START xgroup_create + String res19 = jedis.xgroupCreate("race:france","france_riders",StreamEntryID.LAST_ENTRY,false); + System.out.println(res19); // >>> OK + //STEP_END + + //STEP_START xgroup_create_mkstream + String res20 = jedis.xgroupCreate("race:italy","italy_riders",StreamEntryID.LAST_ENTRY,true); + System.out.println(res20); // >>> OK + //STEP_END + + //STEP_START xgroup_read + StreamEntryID id1 = jedis.xadd("race:italy", new HashMap(){{put("rider","Castilaa");}},XAddParams.xAddParams()); + StreamEntryID id2 = jedis.xadd("race:italy", new HashMap(){{put("rider","Royce");}},XAddParams.xAddParams()); + StreamEntryID id3 = jedis.xadd("race:italy", new HashMap(){{put("rider","Sam-Bodden");}},XAddParams.xAddParams()); + StreamEntryID id4 = jedis.xadd("race:italy", new HashMap(){{put("rider","Prickett");}},XAddParams.xAddParams()); + StreamEntryID id5 = jedis.xadd("race:italy", new HashMap(){{put("rider","Norem");}},XAddParams.xAddParams()); + + List>> res21 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap(){{put("race:italy",StreamEntryID.UNRECEIVED_ENTRY);}}); + System.out.println(res21); // >>> [race:italy=[1701766299006-0 {rider=Castilaa}]] + //STEP_END + + //STEP_START xgroup_read_id + List>> res22 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap(){{put("race:italy",new StreamEntryID());}}); + System.out.println(res22); // >>> [race:italy=[1701766299006-0 {rider=Castilaa}]] + //STEP_END + + //STEP_START xack + long res23 = jedis.xack("race:italy","italy_riders",id1); + System.out.println(res23); // >>> 1 + + List>> res24 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap(){{put("race:italy",new StreamEntryID());}}); + System.out.println(res24); // >>> [race:italy=[]] + //STEP_END + + //STEP_START xgroup_read_bob + List>> res25 = jedis.xreadGroup("italy_riders","Bob", XReadGroupParams.xReadGroupParams().count(2),new HashMap(){{put("race:italy",StreamEntryID.UNRECEIVED_ENTRY);}}); + System.out.println(res25); // >>> [race:italy=[1701767632261-1 {rider=Royce}, 1701767632262-0 {rider=Sam-Bodden}]] + //STEP_END + + //STEP_START xpending + StreamPendingSummary res26 = jedis.xpending("race:italy","italy_riders"); + System.out.println(res26.getConsumerMessageCount()); // >>> {Bob=2} + //STEP_END + + //STEP_START xpending_plus_minus + List res27 = jedis.xpending("race:italy","italy_riders",XPendingParams.xPendingParams().start(StreamEntryID.MINIMUM_ID).end(StreamEntryID.MAXIMUM_ID).count(10)); + System.out.println(res27); // >>> [1701768567412-1 Bob idle:0 times:1, 1701768567412-2 Bob idle:0 times:1] + //STEP_END + + //STEP_START xrange_pending + List res28 = jedis.xrange("race:italy",id2.toString(),id2.toString()); + System.out.println(res28); // >>> [1701768744819-1 {rider=Royce}] + //STEP_END + + //STEP_START xclaim + List res29 = jedis.xclaim("race:italy","italy_riders","Alice", 0L, XClaimParams.xClaimParams().time(60000),id2); + System.out.println(res29); // >>> [1701769004195-1 {rider=Royce}] + //STEP_END + + //STEP_START xautoclaim + Map.Entry> res30 = jedis.xautoclaim("race:italy","italy_riders","Alice",1L,new StreamEntryID("0-0"),XAutoClaimParams.xAutoClaimParams().count(1)); + System.out.println(res30); // >>> [1701769266831-2=[1701769266831-1 {rider=Royce}] + //STEP_END + + //STEP_START xautoclaim_cursor + Map.Entry> res31 = jedis.xautoclaim("race:italy","italy_riders","Alice",1L,new StreamEntryID(id2.toString()),XAutoClaimParams.xAutoClaimParams().count(1)); + System.out.println(res31); // >>> [0-0=[1701769605847-2 {rider=Sam-Bodden}] + //STEP_END + + //STEP_START xinfo + StreamInfo res32 = jedis.xinfoStream("race:italy"); + System.out.println( + res32.getStreamInfo() + ); // >>> {radix-tree-keys=1, radix-tree-nodes=2, entries-added=5, length=5, groups=1, max-deleted-entry-id=0-0, first-entry=1701769637612-0 {rider=Castilaa}, last-generated-id=1701769637612-4, last-entry=1701769637612-4 {rider=Norem}, recorded-first-entry-id=1701769637612-0} + //STEP_END + + //STEP_START xinfo_groups + List res33 = jedis.xinfoGroups("race:italy"); + for (StreamGroupInfo a : res33){ + System.out.println( + a.getGroupInfo() + ); // >>> {last-delivered-id=1701770253659-0, lag=2, pending=2, name=italy_riders, consumers=2, entries-read=3} + } + //STEP_END + + //STEP_START xinfo_consumers + List res34 = jedis.xinfoConsumers("race:italy","italy_riders"); + for (StreamConsumerInfo a : res34){ + System.out.println( + a.getConsumerInfo() + ); // {inactive=1, idle=1, pending=1, name=Alice} , {inactive=3, idle=3, pending=1, name=Bob} + } + //STEP_END + + //STEP_START maxlen + jedis.xadd("race:italy", new HashMap(){{put("rider","Jones");}},XAddParams.xAddParams().maxLen(10)); + jedis.xadd("race:italy", new HashMap(){{put("rider","Wood");}},XAddParams.xAddParams().maxLen(10)); + jedis.xadd("race:italy", new HashMap(){{put("rider","Henshaw");}},XAddParams.xAddParams().maxLen(10)); + long res35 = jedis.xlen("race:italy"); + System.out.println(res35); // >>> 8 + + List res36 = jedis.xrange("race:italy","-","+"); + System.out.println(res36); // >>> [1701771219852-0 {rider=Castilaa}, 1701771219852-1 {rider=Royce}, 1701771219853-0 {rider=Sam-Bodden}, 1701771219853-1 {rider=Prickett}, 1701771219853-2 {rider=Norem}, 1701771219858-0 {rider=Jones}, 1701771219858-1 {rider=Wood}, 1701771219859-0 {rider=Henshaw}] + + StreamEntryID id6 = jedis.xadd("race:italy", new HashMap(){{put("rider","Smith");}},XAddParams.xAddParams().maxLen(2)); + + List res37 = jedis.xrange("race:italy","-","+"); + System.out.println(res37); // >>> [1701771067332-1 {rider=Henshaw}, 1701771067332-2 {rider=Smith}] + //STEP_END + + //STEP_START xtrim + long res38 = jedis.xtrim("race:italy",XTrimParams.xTrimParams().maxLen(10).exactTrimming()); + System.out.println(res38); /// >>> 0 + //STEP_END + + //STEP_START xtrim2 + long res39 = jedis.xtrim("race:italy",XTrimParams.xTrimParams().maxLen(10)); + System.out.println(res39); /// >>> 0 + //STEP_END + + //STEP_START xdel + List res40 = jedis.xrange("race:italy","-","+"); + System.out.println(res40); // >>> [1701771356428-2 {rider=Henshaw}, 1701771356429-0 {rider=Smith}] + + long res41 = jedis.xdel("race:italy",id6); + System.out.println(res41); // >>> 1 + + List res42 = jedis.xrange("race:italy","-","+"); + System.out.println(res42); // >>> [1701771517639-1 {rider=Henshaw}] + //STEP_END + } + +} From ccf74cf2f6e4075b03da30f798ca2c8c6fa71780 Mon Sep 17 00:00:00 2001 From: Ranjeet SIngh <32495484+Ranjeet0611@users.noreply.github.com> Date: Fri, 29 Dec 2023 21:41:43 +0530 Subject: [PATCH 11/19] Java example code snippets for T-Digest and TopK (#3660) * Created example for t digest * Added example for topK --------- Co-authored-by: Ranjeet Singh Co-authored-by: Ranjeet Singh --- .../io/redis/examples/TDigestExample.java | 85 +++++++++++++++++++ .../java/io/redis/examples/TopKExample.java | 47 ++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/test/java/io/redis/examples/TDigestExample.java create mode 100644 src/test/java/io/redis/examples/TopKExample.java diff --git a/src/test/java/io/redis/examples/TDigestExample.java b/src/test/java/io/redis/examples/TDigestExample.java new file mode 100644 index 0000000000..28c851a25b --- /dev/null +++ b/src/test/java/io/redis/examples/TDigestExample.java @@ -0,0 +1,85 @@ +//EXAMPLE: tdigest_tutorial +//HIDE_START +package io.redis.examples; +//HIDE_END + +//REMOVE_START +import org.junit.Assert; +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; +import java.util.List; +//REMOVE_END + +public class TDigestExample { + @Test + public void run(){ + //HIDE_START + UnifiedJedis unifiedJedis = new UnifiedJedis("redis://127.0.0.1:6379"); + //HIDE_END + + //REMOVE_START + unifiedJedis.del("racer_ages"); + unifiedJedis.del("bikes:sales"); + //REMOVE_END + + //STEP_START tdig_start + String res1 = unifiedJedis.tdigestCreate("bikes:sales", 100); + System.out.println(res1); // >>> True + + String res2 = unifiedJedis.tdigestAdd("bikes:sales", 21); + System.out.println(res2); // >>> OK + + String res3 = unifiedJedis.tdigestAdd("bikes:sales", 150, 95, 75, 34); + System.out.println(res3); // >>> OK + //STEP_END + + //REMOVE_START + Assert.assertEquals("OK","OK"); + //REMOVE_END + + //STEP_START tdig_cdf + String res4 = unifiedJedis.tdigestCreate("racer_ages"); + System.out.println(res4); // >>> True + + String res5 = unifiedJedis.tdigestAdd("racer_ages", 45.88, + 44.2, + 58.03, + 19.76, + 39.84, + 69.28, + 50.97, + 25.41, + 19.27, + 85.71, + 42.63); + System.out.println(res5); // >>> OK + + List res6 = unifiedJedis.tdigestRank("racer_ages", 50); + System.out.println(res6); // >>> [7] + + List res7 = unifiedJedis.tdigestRank("racer_ages", 50, 40); + System.out.println(res7); // >>> [7, 4] + //STEP_END + + //STEP_START tdig_quant + List res8 = unifiedJedis.tdigestQuantile("racer_ages", 0.5); + System.out.println(res8); // >>> [44.2] + + List res9 = unifiedJedis.tdigestByRank("racer_ages", 4); + System.out.println(res9); // >>> [42.63] + //STEP_END + + //STEP_START tdig_min + double res10 = unifiedJedis.tdigestMin("racer_ages"); + System.out.println(res10); // >>> 19.27 + + double res11 = unifiedJedis.tdigestMax("racer_ages"); + System.out.println(res11); // >>> 85.71 + //STEP_END + + //STEP_START tdig_reset + String res12 = unifiedJedis.tdigestReset("racer_ages"); + System.out.println(res12); // >>> OK + //STEP_END + } +} diff --git a/src/test/java/io/redis/examples/TopKExample.java b/src/test/java/io/redis/examples/TopKExample.java new file mode 100644 index 0000000000..e7b243094d --- /dev/null +++ b/src/test/java/io/redis/examples/TopKExample.java @@ -0,0 +1,47 @@ +//EXAMPLE: topk_tutorial +//HIDE_START +package io.redis.examples; +//HIDE_END + +//REMOVE_START +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; + +import java.util.List; +//REMOVE_END + +public class TopKExample { + @Test + public void run(){ + //HIDE_START + UnifiedJedis unifiedJedis = new UnifiedJedis("redis://127.0.0.1:6379"); + //HIDE_END + + //REMOVE_START + unifiedJedis.del("bikes:keywords"); + //REMOVE_END + + //STEP_START topk + String res1 = unifiedJedis.topkReserve("bikes:keywords", 5L, 2000L, 7L, 0.925D); + System.out.println(res1); // >>> True + + List res2 = unifiedJedis.topkAdd("bikes:keywords", + "store", + "seat", + "handlebars", + "handles", + "pedals", + "tires", + "store", + "seat"); + + System.out.println(res2); // >>> [None, None, None, None, None, 'handlebars', None, None] + + List res3 = unifiedJedis.topkList("bikes:keywords"); + System.out.println(res3); // >>> ['store', 'seat', 'pedals', 'tires', 'handles'] + + List res4 = unifiedJedis.topkQuery("bikes:keywords", "store", "handlebars"); + System.out.println(res4); // >>> [1, 0] + //STEP_END + } +} From cea57ff183a1587961608c4f36850b0f958031e5 Mon Sep 17 00:00:00 2001 From: Suraj <112368828+suraj11chauhan@users.noreply.github.com> Date: Tue, 2 Jan 2024 21:25:56 +0530 Subject: [PATCH 12/19] Create Java code snippets for sets redis#3626 (#3661) * Create Java code snippets for sets redis#3626 * Apply suggestions from code review to add // STEP_END lines. Co-authored-by: David Dougherty * format imports --------- Co-authored-by: David Dougherty Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../java/io/redis/examples/SetsExample.java | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 src/test/java/io/redis/examples/SetsExample.java diff --git a/src/test/java/io/redis/examples/SetsExample.java b/src/test/java/io/redis/examples/SetsExample.java new file mode 100644 index 0000000000..ceb8152017 --- /dev/null +++ b/src/test/java/io/redis/examples/SetsExample.java @@ -0,0 +1,185 @@ +//EXAMPLE: sets_tutorial +//HIDE_START +package io.redis.examples; + +import redis.clients.jedis.UnifiedJedis; +import org.junit.Test; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; + +public class SetsExample { + + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // HIDE_END + // REMOVE_START + jedis.del("bikes:racing:france"); + jedis.del("bikes:racing:usa"); + // REMOVE_END + // STEP_START sadd + long res1 = jedis.sadd("bikes:racing:france", "bike:1"); + System.out.println(res1); // >>> 1 + + long res2 = jedis.sadd("bikes:racing:france", "bike:1"); + System.out.println(res2); // >>> 0 + + long res3 = jedis.sadd("bikes:racing:france", "bike:2", "bike:3"); + System.out.println(res3); // >>> 2 + + long res4 = jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); + System.out.println(res4); // >>> 2 + // STEP_END + + // REMOVE_START + assertEquals(1,res1); + assertEquals(0,res2); + assertEquals(2,res3); + assertEquals(2,res4); + // REMOVE_END + + // STEP_START sismember + // HIDE_START + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); + // HIDE_END + + boolean res5 = jedis.sismember("bikes:racing:usa", "bike:1"); + System.out.println(res5); // >>> true + + boolean res6 = jedis.sismember("bikes:racing:usa", "bike:2"); + System.out.println(res6); // >>> false + // STEP_END + + // REMOVE_START + assertTrue(res5); + assertFalse(res6); + // REMOVE_END + + // STEP_START sinter + // HIDE_START + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); + // HIDE_END + + Set res7 = jedis.sinter("bikes:racing:france", "bikes:racing:usa"); + System.out.println(res7); // >>> [bike:1] + // STEP_END + + // REMOVE_START + assertEquals("[bike:1]",res7.toString()); + // REMOVE_END + + // STEP_START scard + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + + long res8 = jedis.scard("bikes:racing:france"); + System.out.println(res8); // >>> 3 + // STEP_END + + // REMOVE_START + assertEquals(3,res8); + jedis.del("bikes:racing:france"); + // REMOVE_END + + // STEP_START sadd_smembers + long res9 = jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + System.out.println(res9); // >>> 3 + + Set res10 = jedis.smembers("bikes:racing:france"); + System.out.println(res10); // >>> [bike:1, bike:2, bike:3] + // STEP_END + + // REMOVE_START + assertEquals(3,res9); + assertEquals("[bike:1, bike:2, bike:3]",res10.toString()); + // REMOVE_END + + // STEP_START smismember + boolean res11 = jedis.sismember("bikes:racing:france", "bike:1"); + System.out.println(res11); // >>> true + + List res12 = jedis.smismember("bikes:racing:france", "bike:2", "bike:3", "bike:4"); + System.out.println(res12); // >>> [true,true,false] + // STEP_END + + // REMOVE_START + assertTrue(res11); + assertEquals("[true, true, false]",res12.toString()); + // REMOVE_END + + // STEP_START sdiff + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); + + Set res13 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); + System.out.println(res13); // >>> [bike:2, bike:3] + + // REMOVE_START + assertEquals("[bike:2, bike:3]",res13.toString()); + jedis.del("bikes:racing:france"); + jedis.del("bikes:racing:usa"); + // REMOVE_END + // STEP_END + + // STEP_START multisets + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3"); + jedis.sadd("bikes:racing:usa", "bike:1", "bike:4"); + jedis.sadd("bikes:racing:italy", "bike:1", "bike:2", "bike:3", "bike:4"); + + Set res14 = jedis.sinter("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + System.out.println(res14); // >>> [bike:1] + + Set res15 = jedis.sunion("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + System.out.println(res15); // >>> [bike:1, bike:2, bike:3, bike:4] + + Set res16 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa", "bikes:racing:italy"); + System.out.println(res16); // >>> [] + + Set res17 = jedis.sdiff("bikes:racing:usa", "bikes:racing:france"); + System.out.println(res17); // >>> [bike:4] + + Set res18 = jedis.sdiff("bikes:racing:france", "bikes:racing:usa"); + System.out.println(res18); // >>> [bike:2, bike:3] + + // REMOVE_START + assertEquals("[bike:1]",res14.toString()); + assertEquals("[bike:1, bike:2, bike:3, bike:4]",res15.toString()); + assertEquals("[]",res16.toString()); + assertEquals("[bike:4]",res17.toString()); + assertEquals("[bike:2, bike:3]",res18.toString()); + jedis.del("bikes:racing:france"); + jedis.del("bikes:racing:usa"); + jedis.del("bikes:racing:italy"); + // REMOVE_END + // STEP_END + + // STEP_START srem + jedis.sadd("bikes:racing:france", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); + + long res19 = jedis.srem("bikes:racing:france", "bike:1"); + System.out.println(res18); // >>> 1 + + String res20 = jedis.spop("bikes:racing:france"); + System.out.println(res20); // >>> bike:3 + + Set res21 = jedis.smembers("bikes:racing:france"); + System.out.println(res21); // >>> [bike:2, bike:4, bike:5] + + String res22 = jedis.srandmember("bikes:racing:france"); + System.out.println(res22); // >>> bike:4 + // STEP_END + + // REMOVE_START + assertEquals(1,res19); + // REMOVE_END + + // HIDE_START + jedis.close(); + // HIDE_END + } +} From da8e25cebc50ee794bc63f5f6f931d201f796b2b Mon Sep 17 00:00:00 2001 From: Harshvardhan Parmar Date: Sat, 13 Jan 2024 20:03:59 +0530 Subject: [PATCH 13/19] Create Java code snippets for count min sketch (#3644) * added code snippet for CMS in Java * Update CMSExample.java * format spaces * use cmsIncrBy simple variant * more format spaces --------- Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../java/io/redis/examples/CMSExample.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/io/redis/examples/CMSExample.java diff --git a/src/test/java/io/redis/examples/CMSExample.java b/src/test/java/io/redis/examples/CMSExample.java new file mode 100644 index 0000000000..aa9e93714b --- /dev/null +++ b/src/test/java/io/redis/examples/CMSExample.java @@ -0,0 +1,49 @@ +//EXAMPLE: cms_tutorial +//HIDE_START +package io.redis.examples; +//HIDE_END + +//REMOVE_START +import redis.clients.jedis.UnifiedJedis; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +//REMOVE_END + +public class CMSExample { + + @Test + public void run() { + + //HIDE_START + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + //HIDE_END + + //REMOVE_START + jedis.del("bikes:profit"); + //REMOVE_END + + //STEP_START cms + String res1 = jedis.cmsInitByProb("bikes:profit", 0.001d, 0.002d); + System.out.println(res1); // >>> OK + + long res2 = jedis.cmsIncrBy("bikes:profit", "Smoky Mountain Striker", 100L); + System.out.println(res2); // >>> 100 + + List res3 = jedis.cmsIncrBy("bikes:profit", new HashMap() {{ + put("Rocky Mountain Racer", 200L); + put("Cloudy City Cruiser", 150L); + }}); + System.out.println(res3); // >>> [200, 150] + + List res4 = jedis.cmsQuery("bikes:profit", "Smoky Mountain Striker"); + System.out.println(res4); // >>> [100] + + Map res5 = jedis.cmsInfo("bikes:profit"); + System.out.println(res5.get("width") + " " + res5.get("depth") + " " + res5.get("count")); // >>> 2000 9 450 + //STEP_END + } + +} From 7878e46c1318006a3c93ac5728d38da977cbb92f Mon Sep 17 00:00:00 2001 From: Ranjeet SIngh <32495484+Ranjeet0611@users.noreply.github.com> Date: Sat, 13 Jan 2024 20:05:14 +0530 Subject: [PATCH 14/19] Added Java code snippets for Bloom #3630 (#3671) * Added java example for bloom filter * Removed space * fix and format imports * fix compile error * format spaces * Add del command and rename obj * Add del command and fix format * change to junit assert * Update BloomFilterExample.java * Add correct output comment --------- Co-authored-by: Ranjeet Singh Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Co-authored-by: Ranjeet Singh --- .../io/redis/examples/BloomFilterExample.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/io/redis/examples/BloomFilterExample.java diff --git a/src/test/java/io/redis/examples/BloomFilterExample.java b/src/test/java/io/redis/examples/BloomFilterExample.java new file mode 100644 index 0000000000..a6abe79b90 --- /dev/null +++ b/src/test/java/io/redis/examples/BloomFilterExample.java @@ -0,0 +1,49 @@ +// EXAMPLE: bf_tutorial + +// HIDE_START +package io.redis.examples; + +import redis.clients.jedis.UnifiedJedis; +import org.junit.Test; +import org.junit.Assert; +import java.util.List; + +public class BloomFilterExample { + + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // HIDE_END + + // REMOVE_START + jedis.del("bikes:models"); + // REMOVE_END + + // STEP_START bloom + String res1 = jedis.bfReserve("bikes:models", 0.01, 1000); + System.out.println(res1); // >>> OK + + // REMOVE_START + Assert.assertEquals("OK", res1); + // REMOVE_END + + boolean res2 = jedis.bfAdd("bikes:models", "Smoky Mountain Striker"); + System.out.println(res2); // >>> True + + boolean res3 = jedis.bfExists("bikes:models", "Smoky Mountain Striker"); + System.out.println(res3); // >>> True + + List res4 = jedis.bfMAdd("bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet"); + System.out.println(res4); // >>> [True, True, True] + + List res5 = jedis.bfMExists("bikes:models", + "Rocky Mountain Racer", + "Cloudy City Cruiser", + "Windy City Wippet"); + System.out.println(res5); // >>> [True, True, True] + // STEP_END + } +} From 783f92e3801d51d1f90fdc3f8e75303a7bcf2930 Mon Sep 17 00:00:00 2001 From: Ranjeet SIngh <32495484+Ranjeet0611@users.noreply.github.com> Date: Sat, 13 Jan 2024 20:06:25 +0530 Subject: [PATCH 15/19] Added java code snippet for hyperloglog (#3674) * Added Java code snippet for HyperLogLog * fix format and imports * fix spaces * change to junit assert * Added correct output comment * Update HyperLogLogExample.java --------- Co-authored-by: Ranjeet Singh Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../io/redis/examples/HyperLogLogExample.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/io/redis/examples/HyperLogLogExample.java diff --git a/src/test/java/io/redis/examples/HyperLogLogExample.java b/src/test/java/io/redis/examples/HyperLogLogExample.java new file mode 100644 index 0000000000..97e0d2a4c0 --- /dev/null +++ b/src/test/java/io/redis/examples/HyperLogLogExample.java @@ -0,0 +1,41 @@ +// EXAMPLE: hll_tutorial +package io.redis.examples; + +import org.junit.Assert; +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; + +public class HyperLogLogExample { + + @Test + public void run() { + // HIDE_START + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // HIDE_END + + // REMOVE_START + jedis.del("bikes", "commuter_bikes", "all_bikes"); + // REMOVE_END + + // STEP_START pfadd + long res1 = jedis.pfadd("bikes", "Hyperion", "Deimos", "Phoebe", "Quaoar"); + System.out.println(res1); // >>> 1 + + long res2 = jedis.pfcount("bikes"); + System.out.println(res2); // >>> 4 + + long res3 = jedis.pfadd("commuter_bikes", "Salacia", "Mimas", "Quaoar"); + System.out.println(res3); // >>> 1 + + String res4 = jedis.pfmerge("all_bikes", "bikes", "commuter_bikes"); + System.out.println(res4); // >>> OK + + // REMOVE_START + Assert.assertEquals("OK", res4); + // REMOVE_END + + long res5 = jedis.pfcount("all_bikes"); + System.out.println(res5); // >>> 6 + // STEP_END + } +} From b21daabf92d744cf49f34391fcf5108aa2342e03 Mon Sep 17 00:00:00 2001 From: Ranjeet SIngh <32495484+Ranjeet0611@users.noreply.github.com> Date: Sat, 13 Jan 2024 20:16:42 +0530 Subject: [PATCH 16/19] Added Java code snippets for cuckoo filters (#3679) * Add CuckooFilterExample * Add step start comment * Update CuckooFilterExample.java --------- Co-authored-by: Ranjeet Singh Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../redis/examples/CuckooFilterExample.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/test/java/io/redis/examples/CuckooFilterExample.java diff --git a/src/test/java/io/redis/examples/CuckooFilterExample.java b/src/test/java/io/redis/examples/CuckooFilterExample.java new file mode 100644 index 0000000000..33b8403843 --- /dev/null +++ b/src/test/java/io/redis/examples/CuckooFilterExample.java @@ -0,0 +1,45 @@ +// EXAMPLE: cuckoo_tutorial + +// HIDE_START +package io.redis.examples; + +import org.junit.Assert; +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; + +public class CuckooFilterExample { + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // HIDE_END + + // REMOVE_START + jedis.del("bikes:models"); + // REMOVE_END + + // STEP_START cuckoo + String res1 = jedis.cfReserve("bikes:models", 1000000); + System.out.println(res1); // >>> OK + + // REMOVE_START + Assert.assertEquals(res1, "OK"); + // REMOVE_END + + boolean res2 = jedis.cfAdd("bikes:models", "Smoky Mountain Striker"); + System.out.println(res2); // >>> True + + boolean res3 = jedis.cfExists("bikes:models", "Smoky Mountain Striker"); + System.out.println(res3); // >>> True + + boolean res4 = jedis.cfExists("bikes:models", "Terrible Bike Name"); + System.out.println(res4); // >>> False + + boolean res5 = jedis.cfDel("bikes:models", "Smoky Mountain Striker"); + System.out.println(res5); // >>> True + + // REMOVE_START + Assert.assertTrue(res5); + // REMOVE_END + // STEP_END + } +} From fbed1665c9941a6009b14b8d8ef46ee7e41ef769 Mon Sep 17 00:00:00 2001 From: Suraj <112368828+suraj11chauhan@users.noreply.github.com> Date: Sat, 13 Jan 2024 20:48:38 +0530 Subject: [PATCH 17/19] Create Java code snippet for lists #3625 (#3677) * Added example Java code snippets for the List#3625 * format spaces --------- Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../java/io/redis/examples/ListExample.java | 323 ++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 src/test/java/io/redis/examples/ListExample.java diff --git a/src/test/java/io/redis/examples/ListExample.java b/src/test/java/io/redis/examples/ListExample.java new file mode 100644 index 0000000000..f8095b0c91 --- /dev/null +++ b/src/test/java/io/redis/examples/ListExample.java @@ -0,0 +1,323 @@ +// EXAMPLE: list_tutorial +// HIDE_START +package io.redis.examples; + +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.args.ListDirection; +import java.util.List; +import static org.junit.Assert.*; + +public class ListExample { + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + + // HIDE_END + // REMOVE_START + jedis.del("bikes:repairs"); + jedis.del("bikes:finished"); + // REMOVE_END + + // STEP_START queue + long res1 = jedis.lpush("bikes:repairs", "bike:1"); + System.out.println(res1); // >>> 1 + + long res2 = jedis.lpush("bikes:repairs", "bike:2"); + System.out.println(res2); // >>> 2 + + String res3 = jedis.rpop("bikes:repairs"); + System.out.println(res3); // >>> bike:1 + + String res4 = jedis.rpop("bikes:repairs"); + System.out.println(res4); // >>> bike:2 + // STEP_END + + // REMOVE_START + assertEquals(1, res1); + assertEquals(2, res2); + assertEquals("bike:1", res3); + assertEquals("bike:2", res4); + // REMOVE_END + + // STEP_START stack + long res5 = jedis.lpush("bikes:repairs", "bike:1"); + System.out.println(res5); // >>> 1 + + long res6 = jedis.lpush("bikes:repairs", "bike:2"); + System.out.println(res6); // >>> 2 + + String res7 = jedis.lpop("bikes:repairs"); + System.out.println(res7); // >>> bike:2 + + String res8 = jedis.lpop("bikes:repairs"); + System.out.println(res8); // >>> bike:1 + // STEP_END + + // REMOVE_START + assertEquals(1, res5); + assertEquals(2, res6); + assertEquals("bike:2", res7); + assertEquals("bike:1", res8); + // REMOVE_END + + // STEP_START llen + long res9 = jedis.llen("bikes:repairs"); + System.out.println(res9); // >>> 0 + // STEP_END + + // REMOVE_START + assertEquals(0, res9); + // REMOVE_END + + // STEP_START lmove_lrange + long res10 = jedis.lpush("bikes:repairs", "bike:1"); + System.out.println(res10); // >>> 1 + + long res11 = jedis.lpush("bikes:repairs", "bike:2"); + System.out.println(res11); // >>> 2 + + String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); + System.out.println(res12); // >>> bike:2 + + List res13 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res13); // >>> [bike:1] + + List res14 = jedis.lrange("bikes:finished", 0, -1); + System.out.println(res14); // >>> [bike:2] + // STEP_END + + // REMOVE_START + assertEquals(1, res10); + assertEquals(2, res11); + assertEquals("bike:2", res12); + assertEquals("[bike:1]", res13.toString()); + assertEquals("[bike:2]", res14.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + // STEP_START lpush_rpush + long res15 = jedis.rpush("bikes:repairs", "bike:1"); + System.out.println(res15); // >>> 1 + + long res16 = jedis.rpush("bikes:repairs", "bike:2"); + System.out.println(res16); // >>> 2 + + long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); + System.out.println(res17); // >>> 3 + + List res18 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] + // STEP_END + + // REMOVE_START + assertEquals(1, res15); + assertEquals(2, res16); + assertEquals(3, res17); + assertEquals("[bike:important_bike, bike:1, bike:2]", res18.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + // STEP_START variadic + long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); + System.out.println(res19); // >>> 3 + + long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); + System.out.println(res20); // >>> 5 + + List res21 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] + // STEP_END + + // REMOVE_START + assertEquals(3, res19); + assertEquals(5, res20); + assertEquals("[bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]",res21.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + // STEP_START lpop_rpop + long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); + System.out.println(res22); // >>> 3 + + String res23 = jedis.rpop("bikes:repairs"); + System.out.println(res23); // >>> bike:3 + + String res24 = jedis.lpop("bikes:repairs"); + System.out.println(res24); // >>> bike:1 + + String res25 = jedis.rpop("bikes:repairs"); + System.out.println(res25); // >>> bike:2 + + String res26 = jedis.rpop("bikes:repairs"); + System.out.println(res26); // >>> null + // STEP_END + + // REMOVE_START + assertEquals(3, res22); + assertEquals("bike:3", res23); + assertEquals("bike:1", res24); + assertEquals("bike:2", res25); + assertNull(res26); + // REMOVE_END + + // STEP_START ltrim + long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); + System.out.println(res27); // >>> 5 + + String res28 = jedis.ltrim("bikes:repairs", 0, 2); + System.out.println(res28); // >>> OK + + List res29 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res29); // >>> [bike:5, bike:4, bike:3] + // STEP_END + + // REMOVE_START + assertEquals(5, res27); + assertEquals("OK", res28); + assertEquals("[bike:5, bike:4, bike:3]", res29.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + // STEP_START ltrim_end_of_list + res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); + System.out.println(res27); // >>> 5 + + res28 = jedis.ltrim("bikes:repairs", -3, -1); + System.out.println(res2); // >>> OK + + res29 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res29); // >>> [bike:3, bike:4, bike:5] + // STEP_END + + // REMOVE_START + assertEquals(5, res27); + assertEquals("OK", res28); + assertEquals("[bike:3, bike:4, bike:5]", res29.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + // STEP_START brpop + long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); + System.out.println(res31); // >>> 2 + + List res32 = jedis.brpop(1, "bikes:repairs"); + System.out.println(res32); // >>> (bikes:repairs, bike:2) + + List res33 = jedis.brpop(1,"bikes:repairs"); + System.out.println(res33); // >>> (bikes:repairs, bike:1) + + List res34 = jedis.brpop(1,"bikes:repairs"); + System.out.println(res34); // >>> null + // STEP_END + + // REMOVE_START + assertEquals(2, res31); + assertEquals("[bikes:repairs, bike:2]", res32.toString()); + assertEquals( "[bikes:repairs, bike:1]", res33.toString()); + assertNull(res34); + jedis.del("bikes:repairs"); + jedis.del("new_bikes"); + // REMOVE_END + + // STEP_START rule_1 + long res35 = jedis.del("new_bikes"); + System.out.println(res35); // >>> 0 + + long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); + System.out.println(res36); // >>> 3 + // STEP_END + + // REMOVE_START + assertEquals(0, res35); + assertEquals(3, res36); + jedis.del("new_bikes"); + // REMOVE_END + + // STEP_START rule_1.1 + String res37 = jedis.set("new_bikes", "bike:1"); + System.out.println(res37); // >>> OK + + String res38 = jedis.type("new_bikes"); + System.out.println(res38); // >>> string + + try { + long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); + } catch (Exception e) { + e.printStackTrace(); + // >>> redis.clients.jedis.exceptions.JedisDataException: + // >>> WRONGTYPE Operation against a key holding the wrong kind of value + } + // STEP_END + + // REMOVE_START + assertEquals("OK",res37); + assertEquals("string",res38); + jedis.del("new_bikes"); + // REMOVE_END + + // STEP_START rule_2 + jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); + System.out.println(res36); // >>> 3 + + boolean res40 = jedis.exists("bikes:repairs"); + System.out.println(res40); // >>> true + + String res41 = jedis.lpop("bikes:repairs"); + System.out.println(res41); // >>> bike:3 + + String res42 = jedis.lpop("bikes:repairs"); + System.out.println(res42); // >>> bike:2 + + String res43 = jedis.lpop("bikes:repairs"); + System.out.println(res43); // >>> bike:1 + + boolean res44 = jedis.exists("bikes:repairs"); + System.out.println(res44); // >>> false + // STEP_END + + // REMOVE_START + assertTrue(res40); + assertEquals(res41, "bike:3"); + assertEquals(res42, "bike:2"); + assertEquals(res43, "bike:1"); + assertFalse(res44); + // REMOVE_END + + // STEP_START rule_3 + long res45 = jedis.del("bikes:repairs"); + System.out.println(res45); // >>> 0 + + long res46 = jedis.llen("bikes:repairs"); + System.out.println(res46); // >>> 0 + + String res47 = jedis.lpop("bikes:repairs"); + System.out.println(res47); // >>> null + // STEP_END + + // REMOVE_START + assertEquals(0, res45); + assertEquals(0, res46); + assertNull(res47); + // REMOVE_END + + // STEP_START ltrim.1 + long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); + System.out.println(res48); // >>> 5 + + String res49 = jedis.ltrim("bikes:repairs", 0, 2); + System.out.println(res49); // >>> OK + + List res50 = jedis.lrange("bikes:repairs", 0, -1); + System.out.println(res50); // >>> [bike:5, bike:4, bike:3] + // STEP_END + + // REMOVE_START + assertEquals(5, res48); + assertEquals("OK", res49); + assertEquals("[bike:5, bike:4, bike:3]", res50.toString()); + jedis.del("bikes:repairs"); + // REMOVE_END + + } +} From c467015a310eaeb743dc609e8ea895b79f4118be Mon Sep 17 00:00:00 2001 From: Harshvardhan Parmar Date: Tue, 16 Jan 2024 23:43:12 +0530 Subject: [PATCH 18/19] Create Java code snippet for sorted sets (#3680) * added an example for SortedSets in java * Update SortedSetsExample.java * made changes as per the review. * Format spaces in SortedSetsExample.java --------- Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../io/redis/examples/SortedSetsExample.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/test/java/io/redis/examples/SortedSetsExample.java diff --git a/src/test/java/io/redis/examples/SortedSetsExample.java b/src/test/java/io/redis/examples/SortedSetsExample.java new file mode 100644 index 0000000000..946e181011 --- /dev/null +++ b/src/test/java/io/redis/examples/SortedSetsExample.java @@ -0,0 +1,124 @@ +//EXAMPLE: ss_tutorial +//HIDE_START +package io.redis.examples; + +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.resps.Tuple; +//HIDE_END + +//REMOVE_START +import org.junit.Test; +import java.util.HashMap; +import java.util.List; + +import static org.junit.Assert.assertEquals; +//REMOVE_END + +public class SortedSetsExample { + + @Test + public void run() { + + //HIDE_START + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + //HIDE_END + + //REMOVE_START + jedis.del("racer_scores"); + //REMOVE_END + + //STEP_START zadd + long res1 = jedis.zadd("racer_scores", 10d, "Norem"); + System.out.println(res1); // >>> 1 + + long res2 = jedis.zadd("racer_scores", 12d, "Castilla"); + System.out.println(res2); // >>> 1 + + long res3 = jedis.zadd("racer_scores", new HashMap() {{ + put("Sam-Bodden", 8d); + put("Royce", 10d); + put("Ford", 6d); + put("Prickett", 14d); + put("Castilla", 12d); + }}); + System.out.println(res3); // >>> 4 + //STEP_END + + //STEP_START zrange + List res4 = jedis.zrange("racer_scores", 0, -1); + System.out.println(res4); // >>> [Ford, Sam-Bodden, Norem, Royce, Castil, Castilla, Prickett] + + List res5 = jedis.zrevrange("racer_scores", 0, -1); + System.out.println(res5); // >>> [Prickett, Castilla, Castil, Royce, Norem, Sam-Bodden, Ford] + //STEP_END + + //STEP_START zrange_withscores + List res6 = jedis.zrangeWithScores("racer_scores", 0, -1); + System.out.println(res6); // >>> [[Ford,6.0], [Sam-Bodden,8.0], [Norem,10.0], [Royce,10.0], [Castil,12.0], [Castilla,12.0], [Prickett,14.0]] + //STEP_END + + //STEP_START zrangebyscore + List res7 = jedis.zrangeByScore("racer_scores", Double.MIN_VALUE, 10d); + System.out.println(res7); // >>> [Ford, Sam-Bodden, Norem, Royce] + //STEP_END + + //STEP_START zremrangebyscore + long res8 = jedis.zrem("racer_scores", "Castilla"); + System.out.println(res8); // >>> 1 + + long res9 = jedis.zremrangeByScore("racer_scores", Double.MIN_VALUE, 9d); + System.out.println(res9); // >>> 2 + + List res10 = jedis.zrange("racer_scores", 0, -1); + System.out.println(res10); // >>> [Norem, Royce, Prickett] + //STEP_END + + //REMOVE_START + assertEquals(3, jedis.zcard("racer_scores")); + //REMOVE_END + + //STEP_START zrank + long res11 = jedis.zrank("racer_scores", "Norem"); + System.out.println(res11); // >>> 0 + + long res12 = jedis.zrevrank("racer_scores", "Norem"); + System.out.println(res12); // >>> 2 + //STEP_END + + //STEP_START zadd_lex + long res13 = jedis.zadd("racer_scores", new HashMap() {{ + put("Norem", 0d); + put("Sam-Bodden", 0d); + put("Royce", 0d); + put("Ford", 0d); + put("Prickett", 0d); + put("Castilla", 0d); + }}); + System.out.println(res13); // >>> 3 + + List res14 = jedis.zrange("racer_scores", 0, -1); + System.out.println(res14); // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden] + + List res15 = jedis.zrangeByLex("racer_scores", "[A", "[L"); + System.out.println(res15); // >>> [Castilla, Ford] + //STEP_END + + //STEP_START leaderboard + long res16 = jedis.zadd("racer_scores", 100d, "Wood"); + System.out.println(res16); // >>> 1 + + long res17 = jedis.zadd("racer_scores", 100d, "Henshaw"); + System.out.println(res17); // >>> 1 + + long res18 = jedis.zadd("racer_scores", 100d, "Henshaw"); + System.out.println(res18); // >>> 0 + + double res19 = jedis.zincrby("racer_scores", 50d, "Wood"); + System.out.println(res19); // >>> 150.0 + + double res20 = jedis.zincrby("racer_scores", 50d, "Henshaw"); + System.out.println(res20); // >>> 200.0 + //STEP_END + } +} + From 118cc1d1ae07872a4868bfba39ecf975f3c5fbec Mon Sep 17 00:00:00 2001 From: Ranjeet SIngh <32495484+Ranjeet0611@users.noreply.github.com> Date: Tue, 16 Jan 2024 23:44:48 +0530 Subject: [PATCH 19/19] Added Java code snippet for bitmap (#3687) * Add bitmap example * Change class name * Add assert for res3 and res4 * Update BitMapsExample.java * Format spaces in BitMapsExample.java --------- Co-authored-by: Ranjeet Singh Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- .../io/redis/examples/BitMapsExample.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/test/java/io/redis/examples/BitMapsExample.java diff --git a/src/test/java/io/redis/examples/BitMapsExample.java b/src/test/java/io/redis/examples/BitMapsExample.java new file mode 100644 index 0000000000..a1b80600e0 --- /dev/null +++ b/src/test/java/io/redis/examples/BitMapsExample.java @@ -0,0 +1,47 @@ +// EXAMPLE: bitmap_tutorial +// HIDE_START +package io.redis.examples; + +import org.junit.Assert; +import org.junit.Test; +import redis.clients.jedis.UnifiedJedis; + +public class BitMapsExample { + + @Test + public void run() { + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); + // HIDE_END + + // REMOVE_START + jedis.del("pings:2024-01-01-00:00"); + // REMOVE_END + + + // STEP_START ping + boolean res1 = jedis.setbit("pings:2024-01-01-00:00", 123, true); + System.out.println(res1); // >>> false + + boolean res2 = jedis.getbit("pings:2024-01-01-00:00", 123); + System.out.println(res2); // >>> true + + boolean res3 = jedis.getbit("pings:2024-01-01-00:00", 456); + System.out.println(res3); // >>> false + // STEP_END + + // REMOVE_START + Assert.assertFalse(res1); + Assert.assertTrue(res2); + Assert.assertFalse(res3); + // REMOVE_END + + // STEP_START bitcount + long res4 = jedis.bitcount("pings:2024-01-01-00:00"); + System.out.println(res4); // >>> 1 + // STEP_END + + // REMOVE_START + Assert.assertEquals(res4, 1); + // REMOVE_END + } +}