...

Source file src/github.com/redis/go-redis/v9/doctests/home_prob_dts_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     1  // EXAMPLE: home_prob_dts
     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_probabilistic_datatypes() {
    15  	ctx := context.Background()
    16  
    17  	rdb := redis.NewClient(&redis.Options{
    18  		Addr:     "localhost:6379",
    19  		Password: "", // no password set
    20  		DB:       0,  // use default DB
    21  	})
    22  
    23  	// REMOVE_START
    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  	// REMOVE_END
    32  
    33  	// STEP_START bloom
    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) // >>> [true true true true]
    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) // >>> true
    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) // >>> false
    63  	// STEP_END
    64  
    65  	// STEP_START cuckoo
    66  	res4, err := rdb.CFAdd(ctx, "other_users", "paolo").Result()
    67  
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  
    72  	fmt.Println(res4) // >>> true
    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) // >>> true
    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) // >>> true
    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) // >>> [true true false]
    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) // >>> true
   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) // >>> false
   115  	// STEP_END
   116  
   117  	// STEP_START hyperloglog
   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) // >>> 1
   129  
   130  	res11, err := rdb.PFCount(ctx, "group:1").Result()
   131  
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  
   136  	fmt.Println(res11) // >>> 3
   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) // >>> 1
   148  
   149  	res13, err := rdb.PFCount(ctx, "group:2").Result()
   150  
   151  	if err != nil {
   152  		panic(err)
   153  	}
   154  
   155  	fmt.Println(res13) // >>> 4
   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) // >>> OK
   168  
   169  	res15, err := rdb.PFCount(ctx, "both_groups").Result()
   170  
   171  	if err != nil {
   172  		panic(err)
   173  	}
   174  
   175  	fmt.Println(res15) // >>> 7
   176  	// STEP_END
   177  
   178  	// STEP_START cms
   179  	// Specify that you want to keep the counts within 0.01
   180  	// (0.1%) of the true value with a 0.005 (0.05%) chance
   181  	// of going outside this limit.
   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) // >>> OK
   189  
   190  	// The parameters for `CMSIncrBy()` are two lists. The count
   191  	// for each item in the first list is incremented by the
   192  	// value at the same index in the second list.
   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) // >>> [300 200 200 100]
   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) // >>> [400 350]
   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) // >>> [400 200 350 100]
   227  	// STEP_END
   228  
   229  	// STEP_START tdigest
   230  	res20, err := rdb.TDigestCreate(ctx, "male_heights").Result()
   231  
   232  	if err != nil {
   233  		panic(err)
   234  	}
   235  
   236  	fmt.Println(res20) // >>> OK
   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) // >>> OK
   247  
   248  	res22, err := rdb.TDigestMin(ctx, "male_heights").Result()
   249  	if err != nil {
   250  		panic(err)
   251  	}
   252  	fmt.Println(res22) // >>> 152
   253  
   254  	res23, err := rdb.TDigestMax(ctx, "male_heights").Result()
   255  
   256  	if err != nil {
   257  		panic(err)
   258  	}
   259  
   260  	fmt.Println(res23) // >>> 196
   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) // >>> [181]
   269  
   270  	// Note that the CDF value for 181 is not exactly
   271  	// 0.75. Both values are estimates.
   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]) // >>> 0.7857
   279  
   280  	res26, err := rdb.TDigestCreate(ctx, "female_heights").Result()
   281  
   282  	if err != nil {
   283  		panic(err)
   284  	}
   285  
   286  	fmt.Println(res26) // >>> OK
   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) // >>> OK
   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) // >>> [170]
   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) // >>> OK
   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) // >>> [175.5]
   324  	// STEP_END
   325  
   326  	// STEP_START topk
   327  	// Create a TopK filter that keeps track of the top 3 items
   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) // >>> OK
   335  
   336  	// Add some items to the filter
   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  	// >>> [   Rock me, Handel Only one more time ]
   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  	// >>> [Average lover How will anyone know? Starfish Trooper]
   362  
   363  	// Query the count for specific items
   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) // >>> [true false]
   375  	// STEP_END
   376  
   377  	// Output:
   378  	// [true true true true]
   379  	// true
   380  	// false
   381  	// true
   382  	// true
   383  	// true
   384  	// [true true false]
   385  	// true
   386  	// false
   387  	// 1
   388  	// 3
   389  	// 1
   390  	// 4
   391  	// OK
   392  	// 7
   393  	// OK
   394  	// [300 200 200 100]
   395  	// [400 350]
   396  	// [400 200 350 100]
   397  	// OK
   398  	// OK
   399  	// 152
   400  	// 196
   401  	// [181]
   402  	// 0.7857
   403  	// OK
   404  	// OK
   405  	// [170]
   406  	// OK
   407  	// [175.5]
   408  	// OK
   409  	// [   Rock me, Handel Only one more time ]
   410  	// [Average lover How will anyone know? Starfish Trooper]
   411  	// [true false]
   412  }
   413  

View as plain text