|
| 1 | +//EXAMPLE: stream_tutorial |
| 2 | +//HIDE_START |
| 3 | +package io.redis.examples; |
| 4 | + |
| 5 | +import redis.clients.jedis.StreamEntryID; |
| 6 | +import redis.clients.jedis.UnifiedJedis; |
| 7 | +//HIDE_END |
| 8 | + |
| 9 | +//REMOVE_START |
| 10 | +import org.junit.Test; |
| 11 | +import redis.clients.jedis.exceptions.JedisDataException; |
| 12 | +import redis.clients.jedis.params.*; |
| 13 | +import redis.clients.jedis.resps.*; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +//REMOVE_END |
| 19 | + |
| 20 | +public class StreamsExample { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void run(){ |
| 24 | + |
| 25 | + //HIDE_START |
| 26 | + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); |
| 27 | + //HIDE_END |
| 28 | + |
| 29 | + //REMOVE_START |
| 30 | + jedis.del("race:france", "race:italy", "race:usa"); |
| 31 | + //REMOVE_END |
| 32 | + |
| 33 | + // STEP_START xadd |
| 34 | + StreamEntryID res1 = jedis.xadd("race:france",new HashMap<String,String>(){{put("rider","Castilla");put("speed","30.2");put("position","1");put("location_id","1");}} , XAddParams.xAddParams()); |
| 35 | + |
| 36 | + System.out.println(res1); // >>> 1701760582225-0 |
| 37 | + |
| 38 | + StreamEntryID res2 = jedis.xadd("race:france",new HashMap<String,String>(){{put("rider","Norem");put("speed","28.8");put("position","3");put("location_id","1");}} , XAddParams.xAddParams()); |
| 39 | + |
| 40 | + System.out.println(res2); // >>> 1701760582225-1 |
| 41 | + |
| 42 | + StreamEntryID res3 = jedis.xadd("race:france",new HashMap<String,String>(){{put("rider","Prickett");put("speed","29.7");put("position","2");put("location_id","1");}} , XAddParams.xAddParams()); |
| 43 | + |
| 44 | + System.out.println(res3); // >>> 1701760582226-0 |
| 45 | + //STEP_END |
| 46 | + |
| 47 | + //REMOVE_START |
| 48 | + assertEquals(jedis.xlen("race:france"),3); |
| 49 | + //REMOVE_END |
| 50 | + |
| 51 | + //STEP_START xrange |
| 52 | + List<StreamEntry> res4 = jedis.xrange("race:france","1701760582225-0","+",2); |
| 53 | + |
| 54 | + 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}] |
| 55 | + //STEP_END |
| 56 | + |
| 57 | + //STEP_START xread_block |
| 58 | + List<Map.Entry<String, List<StreamEntry>>> res5= jedis.xread(XReadParams.xReadParams().block(300).count(100),new HashMap<String,StreamEntryID>(){{put("race:france",new StreamEntryID());}}); |
| 59 | + System.out.println( |
| 60 | + res5 |
| 61 | + ); // >>> [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}]] |
| 62 | + //STEP_END |
| 63 | + |
| 64 | + //STEP_START xadd_2 |
| 65 | + StreamEntryID res6 = jedis.xadd("race:france",new HashMap<String,String>(){{put("rider","Castilla");put("speed","29.9");put("position","2");put("location_id","1");}} , XAddParams.xAddParams()); |
| 66 | + System.out.println(res6); // >>> 1701762285679-0 |
| 67 | + //STEP_END |
| 68 | + |
| 69 | + //STEP_START xlen |
| 70 | + long res7 = jedis.xlen("race:france"); |
| 71 | + System.out.println(res7); // >>> 4 |
| 72 | + //STEP_END |
| 73 | + |
| 74 | + //STEP_START xadd_id |
| 75 | + StreamEntryID res8 = jedis.xadd("race:usa", new HashMap<String,String>(){{put("racer","Castilla");}},XAddParams.xAddParams().id("0-1")); |
| 76 | + System.out.println(res8); // >>> 0-1 |
| 77 | + |
| 78 | + StreamEntryID res9 = jedis.xadd("race:usa", new HashMap<String,String>(){{put("racer","Norem");}},XAddParams.xAddParams().id("0-2")); |
| 79 | + System.out.println(res9); // >>> 0-2 |
| 80 | + //STEP_END |
| 81 | + |
| 82 | + //STEP_START xadd_bad_id |
| 83 | + try { |
| 84 | + StreamEntryID res10 = jedis.xadd("race:usa", new HashMap<String,String>(){{put("racer","Prickett");}},XAddParams.xAddParams().id("0-1")); |
| 85 | + System.out.println(res10); // >>> 0-1 |
| 86 | + } |
| 87 | + catch (JedisDataException e){ |
| 88 | + System.out.println(e); // >>> ERR The ID specified in XADD is equal or smaller than the target stream top item |
| 89 | + } |
| 90 | + //STEP_END |
| 91 | + |
| 92 | + //STEP_START xadd_7 |
| 93 | + StreamEntryID res11 = jedis.xadd("race:usa", new HashMap<String,String>(){{put("racer","Norem");}},XAddParams.xAddParams().id("0-*")); |
| 94 | + System.out.println(res11); |
| 95 | + //STEP_END |
| 96 | + |
| 97 | + //STEP_START xrange_all |
| 98 | + List<StreamEntry> res12 = jedis.xrange("race:france","-","+"); |
| 99 | + System.out.println( |
| 100 | + res12 |
| 101 | + ); // >>> [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}] |
| 102 | + //STEP_END |
| 103 | + |
| 104 | + //STEP_START xrange_time |
| 105 | + List<StreamEntry> res13 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()-1000),String.valueOf(System.currentTimeMillis()+1000)); |
| 106 | + System.out.println( |
| 107 | + res13 |
| 108 | + ); // >>> [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}] |
| 109 | + //STEP_END |
| 110 | + |
| 111 | + //STEP_START xrange_step_1 |
| 112 | + List<StreamEntry> res14 = jedis.xrange("race:france","-","+",2); |
| 113 | + 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}] |
| 114 | + //STEP_END |
| 115 | + |
| 116 | + //STEP_START xrange_step_2 |
| 117 | + List<StreamEntry> res15 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()-1000)+"-0","+",2); |
| 118 | + 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}] |
| 119 | + //STEP_END |
| 120 | + |
| 121 | + //STEP_START xrange_empty |
| 122 | + List<StreamEntry> res16 = jedis.xrange("race:france",String.valueOf(System.currentTimeMillis()+1000)+"-0","+",2); |
| 123 | + System.out.println(res16); // >>> [] |
| 124 | + // STEP_END |
| 125 | + |
| 126 | + //STEP_START xrevrange |
| 127 | + List<StreamEntry> res17 = jedis.xrevrange("race:france","+","-",1); |
| 128 | + System.out.println(res17); // >>> [1701765218592-0 {rider=Castilla, speed=29.9, location_id=1, position=2}] |
| 129 | + //STEP_END |
| 130 | + |
| 131 | + //STEP_START xread |
| 132 | + List<Map.Entry<String, List<StreamEntry>>> res18= jedis.xread(XReadParams.xReadParams().count(2),new HashMap<String,StreamEntryID>(){{put("race:france",new StreamEntryID());}}); |
| 133 | + System.out.println( |
| 134 | + res18 |
| 135 | + ); // >>> [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}]] |
| 136 | + //STEP_END |
| 137 | + |
| 138 | + //STEP_START xgroup_create |
| 139 | + String res19 = jedis.xgroupCreate("race:france","france_riders",StreamEntryID.LAST_ENTRY,false); |
| 140 | + System.out.println(res19); // >>> OK |
| 141 | + //STEP_END |
| 142 | + |
| 143 | + //STEP_START xgroup_create_mkstream |
| 144 | + String res20 = jedis.xgroupCreate("race:italy","italy_riders",StreamEntryID.LAST_ENTRY,true); |
| 145 | + System.out.println(res20); // >>> OK |
| 146 | + //STEP_END |
| 147 | + |
| 148 | + //STEP_START xgroup_read |
| 149 | + StreamEntryID id1 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Castilaa");}},XAddParams.xAddParams()); |
| 150 | + StreamEntryID id2 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Royce");}},XAddParams.xAddParams()); |
| 151 | + StreamEntryID id3 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Sam-Bodden");}},XAddParams.xAddParams()); |
| 152 | + StreamEntryID id4 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Prickett");}},XAddParams.xAddParams()); |
| 153 | + StreamEntryID id5 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Norem");}},XAddParams.xAddParams()); |
| 154 | + |
| 155 | + List<Map.Entry<String, List<StreamEntry>>> res21 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap<String,StreamEntryID>(){{put("race:italy",StreamEntryID.UNRECEIVED_ENTRY);}}); |
| 156 | + System.out.println(res21); // >>> [race:italy=[1701766299006-0 {rider=Castilaa}]] |
| 157 | + //STEP_END |
| 158 | + |
| 159 | + //STEP_START xgroup_read_id |
| 160 | + List<Map.Entry<String, List<StreamEntry>>> res22 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap<String,StreamEntryID>(){{put("race:italy",new StreamEntryID());}}); |
| 161 | + System.out.println(res22); // >>> [race:italy=[1701766299006-0 {rider=Castilaa}]] |
| 162 | + //STEP_END |
| 163 | + |
| 164 | + //STEP_START xack |
| 165 | + long res23 = jedis.xack("race:italy","italy_riders",id1); |
| 166 | + System.out.println(res23); // >>> 1 |
| 167 | + |
| 168 | + List<Map.Entry<String, List<StreamEntry>>> res24 = jedis.xreadGroup("italy_riders","Alice", XReadGroupParams.xReadGroupParams().count(1),new HashMap<String,StreamEntryID>(){{put("race:italy",new StreamEntryID());}}); |
| 169 | + System.out.println(res24); // >>> [race:italy=[]] |
| 170 | + //STEP_END |
| 171 | + |
| 172 | + //STEP_START xgroup_read_bob |
| 173 | + List<Map.Entry<String, List<StreamEntry>>> res25 = jedis.xreadGroup("italy_riders","Bob", XReadGroupParams.xReadGroupParams().count(2),new HashMap<String,StreamEntryID>(){{put("race:italy",StreamEntryID.UNRECEIVED_ENTRY);}}); |
| 174 | + System.out.println(res25); // >>> [race:italy=[1701767632261-1 {rider=Royce}, 1701767632262-0 {rider=Sam-Bodden}]] |
| 175 | + //STEP_END |
| 176 | + |
| 177 | + //STEP_START xpending |
| 178 | + StreamPendingSummary res26 = jedis.xpending("race:italy","italy_riders"); |
| 179 | + System.out.println(res26.getConsumerMessageCount()); // >>> {Bob=2} |
| 180 | + //STEP_END |
| 181 | + |
| 182 | + //STEP_START xpending_plus_minus |
| 183 | + List<StreamPendingEntry> res27 = jedis.xpending("race:italy","italy_riders",XPendingParams.xPendingParams().start(StreamEntryID.MINIMUM_ID).end(StreamEntryID.MAXIMUM_ID).count(10)); |
| 184 | + System.out.println(res27); // >>> [1701768567412-1 Bob idle:0 times:1, 1701768567412-2 Bob idle:0 times:1] |
| 185 | + //STEP_END |
| 186 | + |
| 187 | + //STEP_START xrange_pending |
| 188 | + List<StreamEntry> res28 = jedis.xrange("race:italy",id2.toString(),id2.toString()); |
| 189 | + System.out.println(res28); // >>> [1701768744819-1 {rider=Royce}] |
| 190 | + //STEP_END |
| 191 | + |
| 192 | + //STEP_START xclaim |
| 193 | + List<StreamEntry> res29 = jedis.xclaim("race:italy","italy_riders","Alice", 0L, XClaimParams.xClaimParams().time(60000),id2); |
| 194 | + System.out.println(res29); // >>> [1701769004195-1 {rider=Royce}] |
| 195 | + //STEP_END |
| 196 | + |
| 197 | + //STEP_START xautoclaim |
| 198 | + Map.Entry<StreamEntryID, List<StreamEntry>> res30 = jedis.xautoclaim("race:italy","italy_riders","Alice",1L,new StreamEntryID("0-0"),XAutoClaimParams.xAutoClaimParams().count(1)); |
| 199 | + System.out.println(res30); // >>> [1701769266831-2=[1701769266831-1 {rider=Royce}] |
| 200 | + //STEP_END |
| 201 | + |
| 202 | + //STEP_START xautoclaim_cursor |
| 203 | + Map.Entry<StreamEntryID, List<StreamEntry>> res31 = jedis.xautoclaim("race:italy","italy_riders","Alice",1L,new StreamEntryID(id2.toString()),XAutoClaimParams.xAutoClaimParams().count(1)); |
| 204 | + System.out.println(res31); // >>> [0-0=[1701769605847-2 {rider=Sam-Bodden}] |
| 205 | + //STEP_END |
| 206 | + |
| 207 | + //STEP_START xinfo |
| 208 | + StreamInfo res32 = jedis.xinfoStream("race:italy"); |
| 209 | + System.out.println( |
| 210 | + res32.getStreamInfo() |
| 211 | + ); // >>> {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} |
| 212 | + //STEP_END |
| 213 | + |
| 214 | + //STEP_START xinfo_groups |
| 215 | + List<StreamGroupInfo> res33 = jedis.xinfoGroups("race:italy"); |
| 216 | + for (StreamGroupInfo a : res33){ |
| 217 | + System.out.println( |
| 218 | + a.getGroupInfo() |
| 219 | + ); // >>> {last-delivered-id=1701770253659-0, lag=2, pending=2, name=italy_riders, consumers=2, entries-read=3} |
| 220 | + } |
| 221 | + //STEP_END |
| 222 | + |
| 223 | + //STEP_START xinfo_consumers |
| 224 | + List<StreamConsumersInfo> res34 = jedis.xinfoConsumers("race:italy","italy_riders"); |
| 225 | + for (StreamConsumerInfo a : res34){ |
| 226 | + System.out.println( |
| 227 | + a.getConsumerInfo() |
| 228 | + ); // {inactive=1, idle=1, pending=1, name=Alice} , {inactive=3, idle=3, pending=1, name=Bob} |
| 229 | + } |
| 230 | + //STEP_END |
| 231 | + |
| 232 | + //STEP_START maxlen |
| 233 | + jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Jones");}},XAddParams.xAddParams().maxLen(10)); |
| 234 | + jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Wood");}},XAddParams.xAddParams().maxLen(10)); |
| 235 | + jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Henshaw");}},XAddParams.xAddParams().maxLen(10)); |
| 236 | + long res35 = jedis.xlen("race:italy"); |
| 237 | + System.out.println(res35); // >>> 8 |
| 238 | + |
| 239 | + List<StreamEntry> res36 = jedis.xrange("race:italy","-","+"); |
| 240 | + 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}] |
| 241 | + |
| 242 | + StreamEntryID id6 = jedis.xadd("race:italy", new HashMap<String,String>(){{put("rider","Smith");}},XAddParams.xAddParams().maxLen(2)); |
| 243 | + |
| 244 | + List<StreamEntry> res37 = jedis.xrange("race:italy","-","+"); |
| 245 | + System.out.println(res37); // >>> [1701771067332-1 {rider=Henshaw}, 1701771067332-2 {rider=Smith}] |
| 246 | + //STEP_END |
| 247 | + |
| 248 | + //STEP_START xtrim |
| 249 | + long res38 = jedis.xtrim("race:italy",XTrimParams.xTrimParams().maxLen(10).exactTrimming()); |
| 250 | + System.out.println(res38); /// >>> 0 |
| 251 | + //STEP_END |
| 252 | + |
| 253 | + //STEP_START xtrim2 |
| 254 | + long res39 = jedis.xtrim("race:italy",XTrimParams.xTrimParams().maxLen(10)); |
| 255 | + System.out.println(res39); /// >>> 0 |
| 256 | + //STEP_END |
| 257 | + |
| 258 | + //STEP_START xdel |
| 259 | + List<StreamEntry> res40 = jedis.xrange("race:italy","-","+"); |
| 260 | + System.out.println(res40); // >>> [1701771356428-2 {rider=Henshaw}, 1701771356429-0 {rider=Smith}] |
| 261 | + |
| 262 | + long res41 = jedis.xdel("race:italy",id6); |
| 263 | + System.out.println(res41); // >>> 1 |
| 264 | + |
| 265 | + List<StreamEntry> res42 = jedis.xrange("race:italy","-","+"); |
| 266 | + System.out.println(res42); // >>> [1701771517639-1 {rider=Henshaw}] |
| 267 | + //STEP_END |
| 268 | + } |
| 269 | + |
| 270 | +} |
0 commit comments