Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java example code snippets for T-Digest and TopK #3660

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/test/java/io/redis/examples/TDigestExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//EXAMPLE: tdigest_tutorial
//HIDE_START
package io.redis.examples;
//HIDE_END

//REMOVE_START
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import java.util.List;
//REMOVE_END

public class TDigestExample {
@Test
public void run(){
//HIDE_START
UnifiedJedis unifiedJedis = new UnifiedJedis("redis://127.0.0.1:6379");
//HIDE_END

//REMOVE_START
unifiedJedis.del("racer_ages");
unifiedJedis.del("bikes:sales");
//REMOVE_END

//STEP_START tdig_start
String res1 = unifiedJedis.tdigestCreate("bikes:sales", 100);
System.out.println(res1); // >>> True

String res2 = unifiedJedis.tdigestAdd("bikes:sales", 21);
System.out.println(res2); // >>> OK

String res3 = unifiedJedis.tdigestAdd("bikes:sales", 150, 95, 75, 34);
System.out.println(res3); // >>> OK
//STEP_END

//REMOVE_START
Assert.assertEquals("OK","OK");
//REMOVE_END

//STEP_START tdig_cdf
String res4 = unifiedJedis.tdigestCreate("racer_ages");
System.out.println(res4); // >>> True

String res5 = unifiedJedis.tdigestAdd("racer_ages", 45.88,
44.2,
58.03,
19.76,
39.84,
69.28,
50.97,
25.41,
19.27,
85.71,
42.63);
System.out.println(res5); // >>> OK

List<Long> res6 = unifiedJedis.tdigestRank("racer_ages", 50);
System.out.println(res6); // >>> [7]

List<Long> res7 = unifiedJedis.tdigestRank("racer_ages", 50, 40);
System.out.println(res7); // >>> [7, 4]
//STEP_END

//STEP_START tdig_quant
List<Double> res8 = unifiedJedis.tdigestQuantile("racer_ages", 0.5);
System.out.println(res8); // >>> [44.2]

List<Double> res9 = unifiedJedis.tdigestByRank("racer_ages", 4);
System.out.println(res9); // >>> [42.63]
//STEP_END

//STEP_START tdig_min
double res10 = unifiedJedis.tdigestMin("racer_ages");
System.out.println(res10); // >>> 19.27

double res11 = unifiedJedis.tdigestMax("racer_ages");
System.out.println(res11); // >>> 85.71
//STEP_END

//STEP_START tdig_reset
String res12 = unifiedJedis.tdigestReset("racer_ages");
System.out.println(res12); // >>> OK
//STEP_END
}
}
47 changes: 47 additions & 0 deletions src/test/java/io/redis/examples/TopKExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//EXAMPLE: topk_tutorial
//HIDE_START
package io.redis.examples;
//HIDE_END

//REMOVE_START
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;

import java.util.List;
//REMOVE_END

public class TopKExample {
@Test
public void run(){
//HIDE_START
UnifiedJedis unifiedJedis = new UnifiedJedis("redis://127.0.0.1:6379");
//HIDE_END

//REMOVE_START
unifiedJedis.del("bikes:keywords");
//REMOVE_END

//STEP_START topk
String res1 = unifiedJedis.topkReserve("bikes:keywords", 5L, 2000L, 7L, 0.925D);
System.out.println(res1); // >>> True

List<String> res2 = unifiedJedis.topkAdd("bikes:keywords",
"store",
"seat",
"handlebars",
"handles",
"pedals",
"tires",
"store",
"seat");

System.out.println(res2); // >>> [None, None, None, None, None, 'handlebars', None, None]

List<String> res3 = unifiedJedis.topkList("bikes:keywords");
System.out.println(res3); // >>> ['store', 'seat', 'pedals', 'tires', 'handles']

List<Boolean> res4 = unifiedJedis.topkQuery("bikes:keywords", "store", "handlebars");
System.out.println(res4); // >>> [1, 0]
//STEP_END
}
}
Loading