...

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

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

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

View as plain text