Skip to content

Commit fdf165f

Browse files
DOC-4094 hash examples (#3940)
* DOC-4094 added command examples for HGET and HSET * Delete Bitfield_tutorial --------- Co-authored-by: M Sazzadul Hoque <[email protected]>
1 parent 81c9f32 commit fdf165f

File tree

1 file changed

+333
-0
lines changed

1 file changed

+333
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
// EXAMPLE: cmds_hash
2+
// REMOVE_START
3+
package io.redis.examples;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
// REMOVE_END
12+
// HIDE_START
13+
import redis.clients.jedis.UnifiedJedis;
14+
// HIDE_END
15+
16+
// HIDE_START
17+
public class CmdsHashExample {
18+
@Test
19+
public void run() {
20+
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
21+
22+
//REMOVE_START
23+
// Clear any keys here before using them in tests.
24+
jedis.del("myhash");
25+
//REMOVE_END
26+
// HIDE_END
27+
28+
29+
// STEP_START hdel
30+
31+
// STEP_END
32+
33+
// Tests for 'hdel' step.
34+
// REMOVE_START
35+
36+
// REMOVE_END
37+
38+
39+
// STEP_START hexists
40+
41+
// STEP_END
42+
43+
// Tests for 'hexists' step.
44+
// REMOVE_START
45+
46+
// REMOVE_END
47+
48+
49+
// STEP_START hexpire
50+
51+
// STEP_END
52+
53+
// Tests for 'hexpire' step.
54+
// REMOVE_START
55+
56+
// REMOVE_END
57+
58+
59+
// STEP_START hexpireat
60+
61+
// STEP_END
62+
63+
// Tests for 'hexpireat' step.
64+
// REMOVE_START
65+
66+
// REMOVE_END
67+
68+
69+
// STEP_START hexpiretime
70+
71+
// STEP_END
72+
73+
// Tests for 'hexpiretime' step.
74+
// REMOVE_START
75+
76+
// REMOVE_END
77+
78+
79+
// STEP_START hget
80+
Map<String, String> hGetExampleParams = new HashMap<>();
81+
hGetExampleParams.put("field1", "foo");
82+
83+
long hGetResult1 = jedis.hset("myhash", hGetExampleParams);
84+
System.out.println(hGetResult1); // >>> 1
85+
86+
String hGetResult2 = jedis.hget("myhash", "field1");
87+
System.out.println(hGetResult2); // >>> foo
88+
89+
String hGetResult3 = jedis.hget("myhash", "field2");
90+
System.out.println(hGetResult3); // >>> null
91+
// STEP_END
92+
93+
// Tests for 'hget' step.
94+
// REMOVE_START
95+
Assert.assertEquals(1, hGetResult1);
96+
Assert.assertEquals("foo", hGetResult2);
97+
Assert.assertNull(hGetResult3);
98+
jedis.del("myhash");
99+
// REMOVE_END
100+
101+
102+
// STEP_START hgetall
103+
104+
// STEP_END
105+
106+
// Tests for 'hgetall' step.
107+
// REMOVE_START
108+
109+
// REMOVE_END
110+
111+
112+
// STEP_START hincrby
113+
114+
// STEP_END
115+
116+
// Tests for 'hincrby' step.
117+
// REMOVE_START
118+
119+
// REMOVE_END
120+
121+
122+
// STEP_START hincrbyfloat
123+
124+
// STEP_END
125+
126+
// Tests for 'hincrbyfloat' step.
127+
// REMOVE_START
128+
129+
// REMOVE_END
130+
131+
132+
// STEP_START hkeys
133+
134+
// STEP_END
135+
136+
// Tests for 'hkeys' step.
137+
// REMOVE_START
138+
139+
// REMOVE_END
140+
141+
142+
// STEP_START hlen
143+
144+
// STEP_END
145+
146+
// Tests for 'hlen' step.
147+
// REMOVE_START
148+
149+
// REMOVE_END
150+
151+
152+
// STEP_START hmget
153+
154+
// STEP_END
155+
156+
// Tests for 'hmget' step.
157+
// REMOVE_START
158+
159+
// REMOVE_END
160+
161+
162+
// STEP_START hmset
163+
164+
// STEP_END
165+
166+
// Tests for 'hmset' step.
167+
// REMOVE_START
168+
169+
// REMOVE_END
170+
171+
172+
// STEP_START hpersist
173+
174+
// STEP_END
175+
176+
// Tests for 'hpersist' step.
177+
// REMOVE_START
178+
179+
// REMOVE_END
180+
181+
182+
// STEP_START hpexpire
183+
184+
// STEP_END
185+
186+
// Tests for 'hpexpire' step.
187+
// REMOVE_START
188+
189+
// REMOVE_END
190+
191+
192+
// STEP_START hpexpireat
193+
194+
// STEP_END
195+
196+
// Tests for 'hpexpireat' step.
197+
// REMOVE_START
198+
199+
// REMOVE_END
200+
201+
202+
// STEP_START hpexpiretime
203+
204+
// STEP_END
205+
206+
// Tests for 'hpexpiretime' step.
207+
// REMOVE_START
208+
209+
// REMOVE_END
210+
211+
212+
// STEP_START hpttl
213+
214+
// STEP_END
215+
216+
// Tests for 'hpttl' step.
217+
// REMOVE_START
218+
219+
// REMOVE_END
220+
221+
222+
// STEP_START hrandfield
223+
224+
// STEP_END
225+
226+
// Tests for 'hrandfield' step.
227+
// REMOVE_START
228+
229+
// REMOVE_END
230+
231+
232+
// STEP_START hscan
233+
234+
// STEP_END
235+
236+
// Tests for 'hscan' step.
237+
// REMOVE_START
238+
239+
// REMOVE_END
240+
241+
242+
// STEP_START hset
243+
Map<String, String> hSetExampleParams = new HashMap<>();
244+
hSetExampleParams.put("field1", "Hello");
245+
long hSetResult1 = jedis.hset("myhash", hSetExampleParams);
246+
System.out.println(hSetResult1); // >>> 1
247+
248+
String hSetResult2 = jedis.hget("myhash", "field1");
249+
System.out.println(hSetResult2); // >>> Hello
250+
251+
hSetExampleParams.clear();
252+
hSetExampleParams.put("field2", "Hi");
253+
hSetExampleParams.put("field3", "World");
254+
long hSetResult3 = jedis.hset("myhash",hSetExampleParams);
255+
System.out.println(hSetResult3); // >>> 2
256+
257+
String hSetResult4 = jedis.hget("myhash", "field2");
258+
System.out.println(hSetResult4); // >>> Hi
259+
260+
String hSetResult5 = jedis.hget("myhash", "field3");
261+
System.out.println(hSetResult5); // >>> World
262+
263+
Map<String, String> hSetResult6 = jedis.hgetAll("myhash");
264+
265+
for (String key: hSetResult6.keySet()) {
266+
System.out.println("Key: " + key + ", Value: " + hSetResult6.get(key));
267+
}
268+
// >>> Key: field3, Value: World
269+
// >>> Key: field2, Value: Hi
270+
// >>> Key: field1, Value: Hello
271+
// STEP_END
272+
273+
// Tests for 'hset' step.
274+
// REMOVE_START
275+
Assert.assertEquals(1, hSetResult1);
276+
Assert.assertEquals("Hello", hSetResult2);
277+
Assert.assertEquals(2, hSetResult3);
278+
Assert.assertEquals("Hi", hSetResult4);
279+
Assert.assertEquals("World", hSetResult5);
280+
Assert.assertEquals(3, hSetResult6.size());
281+
Assert.assertEquals("Hello", hSetResult6.get("field1"));
282+
Assert.assertEquals("Hi", hSetResult6.get("field2"));
283+
Assert.assertEquals("World", hSetResult6.get("field3"));
284+
jedis.del("myhash");
285+
// REMOVE_END
286+
287+
288+
// STEP_START hsetnx
289+
290+
// STEP_END
291+
292+
// Tests for 'hsetnx' step.
293+
// REMOVE_START
294+
295+
// REMOVE_END
296+
297+
298+
// STEP_START hstrlen
299+
300+
// STEP_END
301+
302+
// Tests for 'hstrlen' step.
303+
// REMOVE_START
304+
305+
// REMOVE_END
306+
307+
308+
// STEP_START httl
309+
310+
// STEP_END
311+
312+
// Tests for 'httl' step.
313+
// REMOVE_START
314+
315+
// REMOVE_END
316+
317+
318+
// STEP_START hvals
319+
320+
// STEP_END
321+
322+
// Tests for 'hvals' step.
323+
// REMOVE_START
324+
325+
// REMOVE_END
326+
327+
328+
// HIDE_START
329+
330+
}
331+
}
332+
// HIDE_END
333+

0 commit comments

Comments
 (0)