Skip to content

Commit da8e25c

Browse files
Harsh4902sazzad16
andauthored
Create Java code snippets for count min sketch (#3644)
* added code snippet for CMS in Java * Update CMSExample.java * format spaces * use cmsIncrBy simple variant * more format spaces --------- Co-authored-by: M Sazzadul Hoque <[email protected]>
1 parent cea57ff commit da8e25c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Diff for: src/test/java/io/redis/examples/CMSExample.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//EXAMPLE: cms_tutorial
2+
//HIDE_START
3+
package io.redis.examples;
4+
//HIDE_END
5+
6+
//REMOVE_START
7+
import redis.clients.jedis.UnifiedJedis;
8+
import org.junit.Test;
9+
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
//REMOVE_END
14+
15+
public class CMSExample {
16+
17+
@Test
18+
public void run() {
19+
20+
//HIDE_START
21+
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
22+
//HIDE_END
23+
24+
//REMOVE_START
25+
jedis.del("bikes:profit");
26+
//REMOVE_END
27+
28+
//STEP_START cms
29+
String res1 = jedis.cmsInitByProb("bikes:profit", 0.001d, 0.002d);
30+
System.out.println(res1); // >>> OK
31+
32+
long res2 = jedis.cmsIncrBy("bikes:profit", "Smoky Mountain Striker", 100L);
33+
System.out.println(res2); // >>> 100
34+
35+
List<Long> res3 = jedis.cmsIncrBy("bikes:profit", new HashMap<String, Long>() {{
36+
put("Rocky Mountain Racer", 200L);
37+
put("Cloudy City Cruiser", 150L);
38+
}});
39+
System.out.println(res3); // >>> [200, 150]
40+
41+
List<Long> res4 = jedis.cmsQuery("bikes:profit", "Smoky Mountain Striker");
42+
System.out.println(res4); // >>> [100]
43+
44+
Map<String, Object> res5 = jedis.cmsInfo("bikes:profit");
45+
System.out.println(res5.get("width") + " " + res5.get("depth") + " " + res5.get("count")); // >>> 2000 9 450
46+
//STEP_END
47+
}
48+
49+
}

0 commit comments

Comments
 (0)