Skip to content

Commit c6d91ea

Browse files
DOC-5227 added inline examples
1 parent 2bae25d commit c6d91ea

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+
<!--< clients-example home_prob_dts cuckoo Java-Sync >}}
122+
< /clients-example >}}-->
123+
```java
124+
boolean res4 = jedis.cfAdd("other_users", "paolo");
125+
System.out.println(res4); // >>> true
126+
127+
boolean res5 = jedis.cfAdd("other_users", "kaitlyn");
128+
System.out.println(res5); // >>> true
129+
130+
boolean res6 = jedis.cfAdd("other_users", "rachel");
131+
System.out.println(res6); // >>> true
132+
133+
boolean[] res7 = jedis.cfMExists(
134+
"other_users",
135+
"paolo", "rachel", "andy"
136+
);
137+
System.out.println(res7); // >>> [true, true, false]
138+
139+
boolean res8 = jedis.cfDel("other_users", "paolo");
140+
System.out.println(res8); // >>> true
141+
142+
boolean res9 = jedis.cfExists("other_users", "paolo");
143+
System.out.println(res9); // >>> false
144+
```
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+
<!--< clients-example home_prob_dts hyperloglog Java-Sync >}}
165+
< /clients-example >}}-->
166+
```java
167+
long res10 = jedis.pfadd("group:1", "andy", "cameron", "david");
168+
System.out.println(res10); // >>> 1
169+
170+
long res11 = jedis.pfcount("group:1");
171+
System.out.println(res11); // >>> 3
172+
173+
long res12 = jedis.pfadd(
174+
"group:2",
175+
"kaitlyn", "michelle", "paolo", "rachel"
176+
);
177+
System.out.println(res12); // >>> 1
178+
179+
long res13 = jedis.pfcount("group:2");
180+
System.out.println(res13); // >>> 4
181+
182+
String res14 = jedis.pfmerge("both_groups", "group:1", "group:2");
183+
System.out.println(res14); // >>> OK
184+
185+
long res15 = jedis.pfcount("both_groups");
186+
System.out.println(res15); // >>> 7
187+
```
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+
<!--< clients-example home_prob_dts cms Java-Sync >}}
226+
< /clients-example >}}-->
227+
```java
228+
// Specify that you want to keep the counts within 0.01
229+
// (0.1%) of the true value with a 0.005 (0.05%) chance
230+
// of going outside this limit.
231+
String res16 = jedis.cmsInitByProb("items_sold", 0.01, 0.005);
232+
System.out.println(res16); // >>> OK
233+
234+
Map<String, Long> firstItemIncrements = new HashMap<>();
235+
firstItemIncrements.put("bread", 300L);
236+
firstItemIncrements.put("tea", 200L);
237+
firstItemIncrements.put("coffee", 200L);
238+
firstItemIncrements.put("beer", 100L);
239+
240+
List<Long> res17 = jedis.cmsIncrBy("items_sold",
241+
firstItemIncrements
242+
);
243+
res17.sort(null);
244+
System.out.println(); // >>> [100, 200, 200, 300]
245+
246+
Map<String, Long> secondItemIncrements = new HashMap<>();
247+
secondItemIncrements.put("bread", 100L);
248+
secondItemIncrements.put("coffee", 150L);
249+
250+
List<Long> res18 = jedis.cmsIncrBy("items_sold",
251+
secondItemIncrements
252+
);
253+
res18.sort(null);
254+
System.out.println(res18); // >>> [350, 400]
255+
256+
List<Long> res19 = jedis.cmsQuery(
257+
"items_sold",
258+
"bread", "tea", "coffee", "beer"
259+
);
260+
res19.sort(null);
261+
System.out.println(res19); // >>> [100, 200, 350, 400]
262+
```
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+
<!--< clients-example home_prob_dts tdigest Java-Sync >}}
294+
< /clients-example >}}-->
295+
```java
296+
String res20 = jedis.tdigestCreate("male_heights");
297+
System.out.println(res20); // >>> OK
298+
299+
String res21 = jedis.tdigestAdd("male_heights",
300+
175.5, 181, 160.8, 152, 177, 196, 164);
301+
System.out.println(res21); // >>> OK
302+
303+
double res22 = jedis.tdigestMin("male_heights");
304+
System.out.println(res22); // >>> 152.0
305+
306+
double res23 = jedis.tdigestMax("male_heights");
307+
System.out.println(res23); // >>> 196.0
308+
309+
List<Double> res24 = jedis.tdigestQuantile("male_heights", 0.75);
310+
System.out.println(res24); // >>> [181.0]
311+
312+
// Note that the CDF value for 181 is not exactly 0.75.
313+
// Both values are estimates.
314+
List<Double> res25 = jedis.tdigestCDF("male_heights", 181);
315+
System.out.println(res25); // >>> [0.7857142857142857]
316+
317+
String res26 = jedis.tdigestCreate("female_heights");
318+
System.out.println(res26); // >>> OK
319+
320+
String res27 = jedis.tdigestAdd("female_heights",
321+
155.5, 161, 168.5, 170, 157.5, 163, 171);
322+
System.out.println(res27); // >>> OK
323+
324+
List<Double> res28 = jedis.tdigestQuantile("female_heights", 0.75);
325+
System.out.println(res28); // >>> [170.0]
326+
327+
String res29 = jedis.tdigestMerge(
328+
"all_heights",
329+
"male_heights", "female_heights"
330+
);
331+
System.out.println(res29); // >>> OK
332+
List<Double> res30 = jedis.tdigestQuantile("all_heights", 0.75);
333+
System.out.println(res30); // >>> [175.5]
334+
```
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+
<!--< clients-example home_prob_dts topk Java-Sync >}}
356+
< /clients-example >}}-->
357+
```java
358+
String res31 = jedis.topkReserve("top_3_songs", 3L, 2000L, 7L, 0.925D);
359+
System.out.println(res31); // >>> OK
360+
361+
Map<String, Long> songIncrements = new HashMap<>();
362+
songIncrements.put("Starfish Trooper", 3000L);
363+
songIncrements.put("Only one more time", 1850L);
364+
songIncrements.put("Rock me, Handel", 1325L);
365+
songIncrements.put("How will anyone know?", 3890L);
366+
songIncrements.put("Average lover", 4098L);
367+
songIncrements.put("Road to everywhere", 770L);
368+
369+
List<String> res32 = jedis.topkIncrBy("top_3_songs",
370+
songIncrements
371+
);
372+
System.out.println(res32);
373+
// >>> [null, null, null, null, null, Rock me, Handel]
374+
375+
List<String> res33 = jedis.topkList("top_3_songs");
376+
System.out.println(res33);
377+
// >>> [Average lover, How will anyone know?, Starfish Trooper]
378+
379+
List<Boolean> res34 = jedis.topkQuery("top_3_songs",
380+
"Starfish Trooper", "Road to everywhere"
381+
);
382+
System.out.println(res34);
383+
// >>> [true, false]
384+
```

0 commit comments

Comments
 (0)