Skip to content

Commit 7ac10a7

Browse files
DOC-5501 Jedis prob examples
1 parent a13e2b2 commit 7ac10a7

File tree

1 file changed

+173
-12
lines changed
  • content/develop/clients/jedis

1 file changed

+173
-12
lines changed

content/develop/clients/jedis/prob.md

Lines changed: 173 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,50 @@ set's membership with a fixed memory size, regardless of how many items you
9898
add. The following example adds some names to a Bloom filter representing
9999
a list of users and checks for the presence or absence of users in the list.
100100

101-
{{< clients-example home_prob_dts bloom Java-Sync >}}
102-
{{< /clients-example >}}
101+
```java
102+
List<Boolean> res1 = jedis.bfMAdd(
103+
"recorded_users",
104+
"andy", "cameron", "david", "michelle"
105+
);
106+
System.out.println(res1); // >>> [true, true, true, true]
107+
108+
boolean res2 = jedis.bfExists("recorded_users", "cameron");
109+
System.out.println(res2); // >>> true
110+
111+
boolean res3 = jedis.bfExists("recorded_users", "kaitlyn");
112+
System.out.println(res3); // >>> false
113+
```
114+
<!--< clients-example home_prob_dts bloom Java-Sync >}}
115+
< /clients-example >}} -->
103116

104117
A Cuckoo filter has similar features to a Bloom filter, but also supports
105118
a deletion operation to remove hashes from a set, as shown in the example
106119
below.
107120

108-
{{< clients-example home_prob_dts cuckoo Java-Sync >}}
109-
{{< /clients-example >}}
121+
```java
122+
boolean res4 = jedis.cfAdd("other_users", "paolo");
123+
System.out.println(res4); // >>> true
124+
125+
boolean res5 = jedis.cfAdd("other_users", "kaitlyn");
126+
System.out.println(res5); // >>> true
127+
128+
boolean res6 = jedis.cfAdd("other_users", "rachel");
129+
System.out.println(res6); // >>> true
130+
131+
List<Boolean> res7 = jedis.cfMExists(
132+
"other_users",
133+
"paolo", "rachel", "andy"
134+
);
135+
System.out.println(res7); // >>> [true, true, false]
136+
137+
boolean res8 = jedis.cfDel("other_users", "paolo");
138+
System.out.println(res8); // >>> true
139+
140+
boolean res9 = jedis.cfExists("other_users", "paolo");
141+
System.out.println(res9); // >>> false
142+
```
143+
<!--< clients-example home_prob_dts cuckoo Java-Sync >}}
144+
< /clients-example >}} -->
110145

111146
Which of these two data types you choose depends on your use case.
112147
Bloom filters are generally faster than Cuckoo filters when adding new items,
@@ -126,8 +161,30 @@ You can also merge two or more HyperLogLogs to find the cardinality of the
126161
[union](https://en.wikipedia.org/wiki/Union_(set_theory)) of the sets they
127162
represent.
128163

129-
{{< clients-example home_prob_dts hyperloglog Java-Sync >}}
130-
{{< /clients-example >}}
164+
```java
165+
long res10 = jedis.pfadd("group:1", "andy", "cameron", "david");
166+
System.out.println(res10); // >>> 1
167+
168+
long res11 = jedis.pfcount("group:1");
169+
System.out.println(res11); // >>> 3
170+
171+
long res12 = jedis.pfadd(
172+
"group:2",
173+
"kaitlyn", "michelle", "paolo", "rachel"
174+
);
175+
System.out.println(res12); // >>> 1
176+
177+
long res13 = jedis.pfcount("group:2");
178+
System.out.println(res13); // >>> 4
179+
180+
String res14 = jedis.pfmerge("both_groups", "group:1", "group:2");
181+
System.out.println(res14); // >>> OK
182+
183+
long res15 = jedis.pfcount("both_groups");
184+
System.out.println(res15); // >>> 7
185+
```
186+
<!--< clients-example home_prob_dts hyperloglog Java-Sync >}}
187+
< /clients-example >}} -->
131188

132189
The main benefit that HyperLogLogs offer is their very low
133190
memory usage. They can count up to 2^64 items with less than
@@ -165,8 +222,44 @@ stay within 0.1% of the true value and have a 0.05% probability
165222
of going outside this limit. The example below shows how to create
166223
a Count-min sketch object, add data to it, and then query it.
167224

168-
{{< clients-example home_prob_dts cms Java-Sync >}}
169-
{{< /clients-example >}}
225+
```java
226+
// Specify that you want to keep the counts within 0.01
227+
// (1%) of the true value with a 0.005 (0.5%) chance
228+
// of going outside this limit.
229+
String res16 = jedis.cmsInitByProb("items_sold", 0.01, 0.005);
230+
System.out.println(res16); // >>> OK
231+
232+
Map<String, Long> firstItemIncrements = new HashMap<>();
233+
firstItemIncrements.put("bread", 300L);
234+
firstItemIncrements.put("tea", 200L);
235+
firstItemIncrements.put("coffee", 200L);
236+
firstItemIncrements.put("beer", 100L);
237+
238+
List<Long> res17 = jedis.cmsIncrBy("items_sold",
239+
firstItemIncrements
240+
);
241+
res17.sort(null);
242+
System.out.println(); // >>> [100, 200, 200, 300]
243+
244+
Map<String, Long> secondItemIncrements = new HashMap<>();
245+
secondItemIncrements.put("bread", 100L);
246+
secondItemIncrements.put("coffee", 150L);
247+
248+
List<Long> res18 = jedis.cmsIncrBy("items_sold",
249+
secondItemIncrements
250+
);
251+
res18.sort(null);
252+
System.out.println(res18); // >>> [350, 400]
253+
254+
List<Long> res19 = jedis.cmsQuery(
255+
"items_sold",
256+
"bread", "tea", "coffee", "beer"
257+
);
258+
res19.sort(null);
259+
System.out.println(res19); // >>> [100, 200, 350, 400]
260+
```
261+
<!--< clients-example home_prob_dts cms Java-Sync >}}
262+
< /clients-example >}} -->
170263

171264
The advantage of using a CMS over keeping an exact count with a
172265
[sorted set]({{< relref "/develop/data-types/sorted-sets" >}})
@@ -197,8 +290,48 @@ maximum values, the quantile of 0.75, and the
197290
shows how to merge two or more t-digest objects to query the combined
198291
data set.
199292

200-
{{< clients-example home_prob_dts tdigest Java-Sync >}}
201-
{{< /clients-example >}}
293+
```java
294+
String res20 = jedis.tdigestCreate("male_heights");
295+
System.out.println(res20); // >>> OK
296+
297+
String res21 = jedis.tdigestAdd("male_heights",
298+
175.5, 181, 160.8, 152, 177, 196, 164);
299+
System.out.println(res21); // >>> OK
300+
301+
double res22 = jedis.tdigestMin("male_heights");
302+
System.out.println(res22); // >>> 152.0
303+
304+
double res23 = jedis.tdigestMax("male_heights");
305+
System.out.println(res23); // >>> 196.0
306+
307+
List<Double> res24 = jedis.tdigestQuantile("male_heights", 0.75);
308+
System.out.println(res24); // >>> [181.0]
309+
310+
// Note that the CDF value for 181 is not exactly 0.75.
311+
// Both values are estimates.
312+
List<Double> res25 = jedis.tdigestCDF("male_heights", 181);
313+
System.out.println(res25); // >>> [0.7857142857142857]
314+
315+
String res26 = jedis.tdigestCreate("female_heights");
316+
System.out.println(res26); // >>> OK
317+
318+
String res27 = jedis.tdigestAdd("female_heights",
319+
155.5, 161, 168.5, 170, 157.5, 163, 171);
320+
System.out.println(res27); // >>> OK
321+
322+
List<Double> res28 = jedis.tdigestQuantile("female_heights", 0.75);
323+
System.out.println(res28); // >>> [170.0]
324+
325+
String res29 = jedis.tdigestMerge(
326+
"all_heights",
327+
"male_heights", "female_heights"
328+
);
329+
System.out.println(res29); // >>> OK
330+
List<Double> res30 = jedis.tdigestQuantile("all_heights", 0.75);
331+
System.out.println(res30); // >>> [175.5]
332+
```
333+
<!--< clients-example home_prob_dts tdigest Java-Sync >}}
334+
< /clients-example >}} -->
202335

203336
A t-digest object also supports several other related commands, such
204337
as querying by rank. See the
@@ -219,5 +352,33 @@ the `topkReserve()` method). It also shows how to list the
219352
top *k* items and query whether or not a given item is in the
220353
list.
221354

222-
{{< clients-example home_prob_dts topk Java-Sync >}}
223-
{{< /clients-example >}}
355+
```java
356+
String res31 = jedis.topkReserve("top_3_songs", 3L, 2000L, 7L, 0.925D);
357+
System.out.println(res31); // >>> OK
358+
359+
Map<String, Long> songIncrements = new HashMap<>();
360+
songIncrements.put("Starfish Trooper", 3000L);
361+
songIncrements.put("Only one more time", 1850L);
362+
songIncrements.put("Rock me, Handel", 1325L);
363+
songIncrements.put("How will anyone know?", 3890L);
364+
songIncrements.put("Average lover", 4098L);
365+
songIncrements.put("Road to everywhere", 770L);
366+
367+
List<String> res32 = jedis.topkIncrBy("top_3_songs",
368+
songIncrements
369+
);
370+
System.out.println(res32);
371+
// >>> [null, null, null, null, null, Rock me, Handel]
372+
373+
List<String> res33 = jedis.topkList("top_3_songs");
374+
System.out.println(res33);
375+
// >>> [Average lover, How will anyone know?, Starfish Trooper]
376+
377+
List<Boolean> res34 = jedis.topkQuery("top_3_songs",
378+
"Starfish Trooper", "Road to everywhere"
379+
);
380+
System.out.println(res34);
381+
// >>> [true, false]
382+
```
383+
<!--< clients-example home_prob_dts topk Java-Sync >}}
384+
< /clients-example >}} -->

0 commit comments

Comments
 (0)