Five kinds of data structure types bring you into Redis

Preface:

Redis on the basis of finishing, five commonly used type of data structure, String, List, Set, Hash, ZSet, below their corresponding profiles, command and application case, the following key, value refers to are the key parameters for the when referring to values ​​can pass multiple values

maven introduced

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0-yp</version>
    </dependency>

Write a test case:

Jedis jedis = new Jedis("192.168.1.214",6379);
String ping = jedis.ping();
System.out.println(ping);

This result => PONG
described can be successfully connected

1.redis strings

command:

command Features Java code corresponding to Return Type return value
set Insert key and value jedis.set(key,value) String “OK”
get According to acquire key value jedis.get(key) String value
of the According delete key jedis.del(key) Long

Case:

redis 127.0.0.1:6379> set hello world
OK
redis 127.0.0.1:6379> get hello
"world"
redis 127.0.0.1:6379> del hello
(integer) 1
redis 127.0.0.1:6379> get hello
(nil)
redis 127.0.0.1:6379>

The list 2.redis

command:

command Features Java code corresponding to Return Type return value
LPUSH Customization will push the left end of the list jedis.lpush(key,values) Long
RPUSH Customization will push the right end of the list jedis.rpush(key,values) Long
LPOP Pop a value from the left of the list, and returns the value of the popup jedis.lpop(key) String value
RPOP Pop a value from the right of the list, and returns the value of the popup jedis.rpop(key) String value
lrange Get a list of all values ​​in a given range jedis.lrange(key, ,start,end); List<String> values
LINDEX Get a list of individual elements in a given position on the jedis.lindex(key,index) String value

Case:

127.0.0.1:6379> rpush list-key item
(integer) 1
127.0.0.1:6379> rpush list-key item2
(integer) 2
127.0.0.1:6379> rpush list-key item
(integer) 3
127.0.0.1:6379> lrange list-key 0 -1
1) "item"
2) "item2"
3) "item"
127.0.0.1:6379> lindex list-key 1
"item2"
127.0.0.1:6379> lpop list-key
"item"
127.0.0.1:6379> lrange list-key 0 -1
1) "item2"
2) "item"
127.0.0.1:6379> 

Note:
0 is the starting index
ending index position -1

3.Redis collection

Summary:

from one end of the set can not push or pop
can with SADD add elements into the collection
SREM remove elements
SISMEMBER check element exists
SMEMBERS Gets a collection that contains all the elements a lot will be very slow

command:

command Features Java code corresponding to Return Type return value
SADD The elements added to the collection jedis.sadd(key,values) Long
SREM Remove elements jedis.srem(key,values) Long
SISMEMBER Command to determine whether a member is a member of the set of elements jedis.sismember(key,value) Boolean
SMEMBERS Gets a collection that contains all the elements a lot will be very slow jedis.smembers(key) Set<String> values

Case:

127.0.0.1:6379> sadd set-key item
(integer) 1
127.0.0.1:6379> sadd set-key item2
(integer) 1
127.0.0.1:6379> sadd set-key item3
(integer) 1
127.0.0.1:6379> sadd set-key item
(integer) 0
127.0.0.1:6379> smembers set-key
1) "item2"
2) "item3"
3) "item"
127.0.0.1:6379> sismember set-key item
(integer) 1
127.0.0.1:6379> sismember set-key item4
(integer) 0
127.0.0.1:6379> srem set-key item2
(integer) 1
127.0.0.1:6379> srem set-key item2
(integer) 0
127.0.0.1:6379> smembers set-key
1) "item3"
2) "item"
127.0.0.1:6379> 

4.Redis hash hash

Introduction:
hash may store a mapping between the plurality of key-value pairs, as string, string or numeric values may be stored value
and the user may perform its digital value increment decrement operation

command:

command Features Java code corresponding to Return Type return value
HSET Since the hash associated with the given key-value pairs inside jedis.hset(key,field,value) Long
hget Gets Top hash value 18 lines jedis.hget(key,field) String
HGETALL Get all values jedis.hgetAll(key) Map<String, String> {field:value}
HDEL Deletes the specified value jedis.hdel(key,field) Long

Case:

127.0.0.1:6379> hset hash-key sub-key1 value1
(integer) 1
127.0.0.1:6379> hset hash-key sub-key1 value2
(integer) 0
127.0.0.1:6379> hset hash-key sub-key1 value1
(integer) 0
127.0.0.1:6379> hset hash-key sub-key2 value1
(integer) 1
127.0.0.1:6379> hgetall hash-key
1) "sub-key1"
2) "value1"
3) "sub-key2"
4) "value1"
127.0.0.1:6379> hdel hash-key sub-key2
(integer) 1
127.0.0.1:6379> hdel hash-key sub-key2
(integer) 0
127.0.0.1:6379> hget hash-key sub-key1
"value1"
127.0.0.1:6379> hgetall hash-key
1) "sub-key1"
2) "value1"
127.0.0.1:6379> 

Ordered set of 5.Redis

Description:
ordered set and hash, as are used to store key-value pairs, ordered set can be sorted according to the incoming score (score)

command:

command Features Java code corresponding to Return Type return value
ZADD A member of the set with a score added to the ordered collection jedis.zadd(key,score,member) Long
ZRANGE The index order, and displays the value within the index range jedis.zrange(key,start,end) Set<String>
ZRANGEBYSCORE The fraction sorted and displayed value within the range of scores jedis.zrangeByScore(key,min,max) Set<String>
ZREM If the member exists, you can remove the member jedis.zre(key,values) Long
ZINTERSTORE Intersection operator generates a new ordered set based on two ordered set, adding Score jedis.zinterstore(newkey,key1,keys) Long
ZINTERSTORE The two ordered set union operation that generates a new ordered set, adding Score jedis.zunionstore(newkey,key1,keys) Long

Case:

127.0.0.1:6379> zadd zset-key 728 member1
(integer) 1
127.0.0.1:6379> zadd zset-key 982 member0
(integer) 1
127.0.0.1:6379> zadd zset-key 982 member0
(integer) 0
127.0.0.1:6379> zrange zset-key 0 -1 withscores
1) "member1"
2) "728"
3) "member0"
4) "982"
127.0.0.1:6379> zrangebyscore zset-key 0 800 withscores
1) "member1"
2) "728"
127.0.0.1:6379> zrem zset-key member1
(integer) 1
127.0.0.1:6379> zrem zset-key member1
(integer) 0
127.0.0.1:6379> zrange zset-key 0 -1 withscores
1) "member0"
2) "982"
127.0.0.1:6379> zrange zset-key 0 -1 
1) "member0"
127.0.0.1:6379> 

Note
on java withscores, with a separate method jedis.zrangeWithScores / zrangeByScoreWithScores, the return value Set <Tuple>

Released five original articles · won praise 3 · views 96

Guess you like

Origin blog.csdn.net/weixin_42887211/article/details/103895587