Skip to content

Commit 69b4c01

Browse files
DOC-4241 added t-digest examples (#3123)
1 parent 233f97a commit 69b4c01

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed

doctests/tdigest_tutorial_test.go

+251
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
// EXAMPLE: tdigest_tutorial
2+
// HIDE_START
3+
package example_commands_test
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/redis/go-redis/v9"
10+
)
11+
12+
// HIDE_END
13+
14+
func ExampleClient_tdigstart() {
15+
ctx := context.Background()
16+
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "", // no password docs
20+
DB: 0, // use default DB
21+
})
22+
23+
// REMOVE_START
24+
rdb.Del(ctx, "racer_ages", "bikes:sales")
25+
// REMOVE_END
26+
27+
// STEP_START tdig_start
28+
res1, err := rdb.TDigestCreate(ctx, "bikes:sales").Result()
29+
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
fmt.Println(res1) // >>> OK
35+
36+
res2, err := rdb.TDigestAdd(ctx, "bikes:sales", 21).Result()
37+
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
fmt.Println(res2) // >>> OK
43+
44+
res3, err := rdb.TDigestAdd(ctx, "bikes:sales",
45+
150, 95, 75, 34,
46+
).Result()
47+
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
fmt.Println(res3) // >>> OK
53+
54+
// STEP_END
55+
56+
// Output:
57+
// OK
58+
// OK
59+
// OK
60+
}
61+
62+
func ExampleClient_tdigcdf() {
63+
ctx := context.Background()
64+
65+
rdb := redis.NewClient(&redis.Options{
66+
Addr: "localhost:6379",
67+
Password: "", // no password docs
68+
DB: 0, // use default DB
69+
})
70+
71+
// REMOVE_START
72+
rdb.Del(ctx, "racer_ages", "bikes:sales")
73+
// REMOVE_END
74+
75+
// STEP_START tdig_cdf
76+
res4, err := rdb.TDigestCreate(ctx, "racer_ages").Result()
77+
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
fmt.Println(res4) // >>> OK
83+
84+
res5, err := rdb.TDigestAdd(ctx, "racer_ages",
85+
45.88, 44.2, 58.03, 19.76, 39.84, 69.28,
86+
50.97, 25.41, 19.27, 85.71, 42.63,
87+
).Result()
88+
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
fmt.Println(res5) // >>> OK
94+
95+
res6, err := rdb.TDigestRank(ctx, "racer_ages", 50).Result()
96+
97+
if err != nil {
98+
panic(err)
99+
}
100+
101+
fmt.Println(res6) // >>> [7]
102+
103+
res7, err := rdb.TDigestRank(ctx, "racer_ages", 50, 40).Result()
104+
105+
if err != nil {
106+
panic(err)
107+
}
108+
109+
fmt.Println(res7) // >>> [7 4]
110+
// STEP_END
111+
112+
// Output:
113+
// OK
114+
// OK
115+
// [7]
116+
// [7 4]
117+
}
118+
119+
func ExampleClient_tdigquant() {
120+
ctx := context.Background()
121+
122+
rdb := redis.NewClient(&redis.Options{
123+
Addr: "localhost:6379",
124+
Password: "", // no password docs
125+
DB: 0, // use default DB
126+
})
127+
128+
// REMOVE_START
129+
rdb.Del(ctx, "racer_ages")
130+
// REMOVE_END
131+
132+
_, err := rdb.TDigestCreate(ctx, "racer_ages").Result()
133+
134+
if err != nil {
135+
panic(err)
136+
}
137+
138+
_, err = rdb.TDigestAdd(ctx, "racer_ages",
139+
45.88, 44.2, 58.03, 19.76, 39.84, 69.28,
140+
50.97, 25.41, 19.27, 85.71, 42.63,
141+
).Result()
142+
143+
if err != nil {
144+
panic(err)
145+
}
146+
147+
// STEP_START tdig_quant
148+
res8, err := rdb.TDigestQuantile(ctx, "racer_ages", 0.5).Result()
149+
150+
if err != nil {
151+
panic(err)
152+
}
153+
154+
fmt.Println(res8) // >>> [44.2]
155+
156+
res9, err := rdb.TDigestByRank(ctx, "racer_ages", 4).Result()
157+
158+
if err != nil {
159+
panic(err)
160+
}
161+
162+
fmt.Println(res9) // >>> [42.63]
163+
// STEP_END
164+
165+
// Output:
166+
// [44.2]
167+
// [42.63]
168+
}
169+
170+
func ExampleClient_tdigmin() {
171+
ctx := context.Background()
172+
173+
rdb := redis.NewClient(&redis.Options{
174+
Addr: "localhost:6379",
175+
Password: "", // no password docs
176+
DB: 0, // use default DB
177+
})
178+
179+
// REMOVE_START
180+
rdb.Del(ctx, "racer_ages")
181+
// REMOVE_END
182+
183+
_, err := rdb.TDigestCreate(ctx, "racer_ages").Result()
184+
185+
if err != nil {
186+
panic(err)
187+
}
188+
189+
_, err = rdb.TDigestAdd(ctx, "racer_ages",
190+
45.88, 44.2, 58.03, 19.76, 39.84, 69.28,
191+
50.97, 25.41, 19.27, 85.71, 42.63,
192+
).Result()
193+
194+
if err != nil {
195+
panic(err)
196+
}
197+
198+
// STEP_START tdig_min
199+
res10, err := rdb.TDigestMin(ctx, "racer_ages").Result()
200+
201+
if err != nil {
202+
panic(err)
203+
}
204+
205+
fmt.Println(res10) // >>> 19.27
206+
207+
res11, err := rdb.TDigestMax(ctx, "racer_ages").Result()
208+
209+
if err != nil {
210+
panic(err)
211+
}
212+
213+
fmt.Println(res11) // >>> 85.71
214+
// STEP_END
215+
216+
// Output:
217+
// 19.27
218+
// 85.71
219+
}
220+
221+
func ExampleClient_tdigreset() {
222+
ctx := context.Background()
223+
224+
rdb := redis.NewClient(&redis.Options{
225+
Addr: "localhost:6379",
226+
Password: "", // no password docs
227+
DB: 0, // use default DB
228+
})
229+
230+
// REMOVE_START
231+
rdb.Del(ctx, "racer_ages")
232+
// REMOVE_END
233+
_, err := rdb.TDigestCreate(ctx, "racer_ages").Result()
234+
235+
if err != nil {
236+
panic(err)
237+
}
238+
239+
// STEP_START tdig_reset
240+
res12, err := rdb.TDigestReset(ctx, "racer_ages").Result()
241+
242+
if err != nil {
243+
panic(err)
244+
}
245+
246+
fmt.Println(res12) // >>> OK
247+
// STEP_END
248+
249+
// Output:
250+
// OK
251+
}

0 commit comments

Comments
 (0)