1
2
3 package example_commands_test
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/redis/go-redis/v9"
10 )
11
12
13
14 func ExampleClient_probabilistic_datatypes() {
15 ctx := context.Background()
16
17 rdb := redis.NewClient(&redis.Options{
18 Addr: "localhost:6379",
19 Password: "",
20 DB: 0,
21 })
22
23
24 rdb.FlushDB(ctx)
25 rdb.Del(ctx,
26 "recorded_users", "other_users",
27 "group:1", "group:2", "both_groups",
28 "items_sold",
29 "male_heights", "female_heights", "all_heights",
30 "top_3_songs")
31
32
33
34 res1, err := rdb.BFMAdd(
35 ctx,
36 "recorded_users",
37 "andy", "cameron", "david", "michelle",
38 ).Result()
39
40 if err != nil {
41 panic(err)
42 }
43
44 fmt.Println(res1)
45
46 res2, err := rdb.BFExists(ctx,
47 "recorded_users", "cameron",
48 ).Result()
49
50 if err != nil {
51 panic(err)
52 }
53
54 fmt.Println(res2)
55
56 res3, err := rdb.BFExists(ctx, "recorded_users", "kaitlyn").Result()
57
58 if err != nil {
59 panic(err)
60 }
61
62 fmt.Println(res3)
63
64
65
66 res4, err := rdb.CFAdd(ctx, "other_users", "paolo").Result()
67
68 if err != nil {
69 panic(err)
70 }
71
72 fmt.Println(res4)
73
74 res5, err := rdb.CFAdd(ctx, "other_users", "kaitlyn").Result()
75
76 if err != nil {
77 panic(err)
78 }
79
80 fmt.Println(res5)
81
82 res6, err := rdb.CFAdd(ctx, "other_users", "rachel").Result()
83
84 if err != nil {
85 panic(err)
86 }
87
88 fmt.Println(res6)
89
90 res7, err := rdb.CFMExists(ctx,
91 "other_users", "paolo", "rachel", "andy",
92 ).Result()
93
94 if err != nil {
95 panic(err)
96 }
97
98 fmt.Println(res7)
99
100 res8, err := rdb.CFDel(ctx, "other_users", "paolo").Result()
101
102 if err != nil {
103 panic(err)
104 }
105
106 fmt.Println(res8)
107
108 res9, err := rdb.CFExists(ctx, "other_users", "paolo").Result()
109
110 if err != nil {
111 panic(err)
112 }
113
114 fmt.Println(res9)
115
116
117
118 res10, err := rdb.PFAdd(
119 ctx,
120 "group:1",
121 "andy", "cameron", "david",
122 ).Result()
123
124 if err != nil {
125 panic(err)
126 }
127
128 fmt.Println(res10)
129
130 res11, err := rdb.PFCount(ctx, "group:1").Result()
131
132 if err != nil {
133 panic(err)
134 }
135
136 fmt.Println(res11)
137
138 res12, err := rdb.PFAdd(ctx,
139 "group:2",
140 "kaitlyn", "michelle", "paolo", "rachel",
141 ).Result()
142
143 if err != nil {
144 panic(err)
145 }
146
147 fmt.Println(res12)
148
149 res13, err := rdb.PFCount(ctx, "group:2").Result()
150
151 if err != nil {
152 panic(err)
153 }
154
155 fmt.Println(res13)
156
157 res14, err := rdb.PFMerge(
158 ctx,
159 "both_groups",
160 "group:1", "group:2",
161 ).Result()
162
163 if err != nil {
164 panic(err)
165 }
166
167 fmt.Println(res14)
168
169 res15, err := rdb.PFCount(ctx, "both_groups").Result()
170
171 if err != nil {
172 panic(err)
173 }
174
175 fmt.Println(res15)
176
177
178
179
180
181
182 res16, err := rdb.CMSInitByProb(ctx, "items_sold", 0.01, 0.005).Result()
183
184 if err != nil {
185 panic(err)
186 }
187
188 fmt.Println(res16)
189
190
191
192
193 res17, err := rdb.CMSIncrBy(ctx, "items_sold",
194 "bread", 300,
195 "tea", 200,
196 "coffee", 200,
197 "beer", 100,
198 ).Result()
199
200 if err != nil {
201 panic(err)
202 }
203
204 fmt.Println(res17)
205
206 res18, err := rdb.CMSIncrBy(ctx, "items_sold",
207 "bread", 100,
208 "coffee", 150,
209 ).Result()
210
211 if err != nil {
212 panic(err)
213 }
214
215 fmt.Println(res18)
216
217 res19, err := rdb.CMSQuery(ctx,
218 "items_sold",
219 "bread", "tea", "coffee", "beer",
220 ).Result()
221
222 if err != nil {
223 panic(err)
224 }
225
226 fmt.Println(res19)
227
228
229
230 res20, err := rdb.TDigestCreate(ctx, "male_heights").Result()
231
232 if err != nil {
233 panic(err)
234 }
235
236 fmt.Println(res20)
237
238 res21, err := rdb.TDigestAdd(ctx, "male_heights",
239 175.5, 181, 160.8, 152, 177, 196, 164,
240 ).Result()
241
242 if err != nil {
243 panic(err)
244 }
245
246 fmt.Println(res21)
247
248 res22, err := rdb.TDigestMin(ctx, "male_heights").Result()
249 if err != nil {
250 panic(err)
251 }
252 fmt.Println(res22)
253
254 res23, err := rdb.TDigestMax(ctx, "male_heights").Result()
255
256 if err != nil {
257 panic(err)
258 }
259
260 fmt.Println(res23)
261
262 res24, err := rdb.TDigestQuantile(ctx, "male_heights", 0.75).Result()
263
264 if err != nil {
265 panic(err)
266 }
267
268 fmt.Println(res24)
269
270
271
272 res25, err := rdb.TDigestCDF(ctx, "male_heights", 181).Result()
273
274 if err != nil {
275 panic(err)
276 }
277
278 fmt.Printf("%.4f\n", res25[0])
279
280 res26, err := rdb.TDigestCreate(ctx, "female_heights").Result()
281
282 if err != nil {
283 panic(err)
284 }
285
286 fmt.Println(res26)
287
288 res27, err := rdb.TDigestAdd(ctx, "female_heights",
289 155.5, 161, 168.5, 170, 157.5, 163, 171,
290 ).Result()
291
292 if err != nil {
293 panic(err)
294 }
295
296 fmt.Println(res27)
297
298 res28, err := rdb.TDigestQuantile(ctx, "female_heights", 0.75).Result()
299
300 if err != nil {
301 panic(err)
302 }
303
304 fmt.Println(res28)
305
306 res29, err := rdb.TDigestMerge(ctx, "all_heights",
307 nil,
308 "male_heights", "female_heights",
309 ).Result()
310
311 if err != nil {
312 panic(err)
313 }
314
315 fmt.Println(res29)
316
317 res30, err := rdb.TDigestQuantile(ctx, "all_heights", 0.75).Result()
318
319 if err != nil {
320 panic(err)
321 }
322
323 fmt.Println(res30)
324
325
326
327
328 res31, err := rdb.TopKReserve(ctx, "top_3_songs", 3).Result()
329
330 if err != nil {
331 panic(err)
332 }
333
334 fmt.Println(res31)
335
336
337 res32, err := rdb.TopKIncrBy(ctx,
338 "top_3_songs",
339 "Starfish Trooper", 3000,
340 "Only one more time", 1850,
341 "Rock me, Handel", 1325,
342 "How will anyone know?", 3890,
343 "Average lover", 4098,
344 "Road to everywhere", 770,
345 ).Result()
346
347 if err != nil {
348 panic(err)
349 }
350
351 fmt.Println(res32)
352
353
354 res33, err := rdb.TopKList(ctx, "top_3_songs").Result()
355
356 if err != nil {
357 panic(err)
358 }
359
360 fmt.Println(res33)
361
362
363
364 res34, err := rdb.TopKQuery(
365 ctx,
366 "top_3_songs",
367 "Starfish Trooper", "Road to everywhere",
368 ).Result()
369
370 if err != nil {
371 panic(err)
372 }
373
374 fmt.Println(res34)
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 }
413
View as plain text