Redis data type, transaction and Jedis operation redis

Posted by pplexr on Mon, 22 Jun 2020 10:19:45 +0200

Five basic types of Redis

Note: Note arrangement source: B station UP master maniac said Java

Official document: Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware.
It supports many types of data structures, such as strings, hashes, lists, sets,
sorted sets and range queries, bitmaps, hyperlogs and geospatial
Index radius query. Redis has built-in replication, Lua scripting and LRU driver events
eviction, transactions, and different levels of disk persistence, through
Redis Sentinel and Cluster provide high availability. String (string)

String> flushdb
OK> set key1 v1    Set value
OK> get key1          Get value
"v1"> keys *          Get all key
1) "key1"> exists key1          Judge a key Does it exist
(integer) 1> append key1 "hello"     Append string if key No, equivalent to set key
(integer) 7> get kye1
(nil)> strlen key1    obtain key Length of
(integer) 7> append key1 ",kuangshen"
(integer) 17> strlen key1
(integer) 17> get key1
"v1hello,kuangshen"> get views
"0"> incr views  Automatic growth
(integer) 1> incr views
(integer) 2> get views
"2"> decr views  Auto decrement
(integer) 1> get views
"1"> incrby views 10  Grow in steps of 10
(integer) 11> get views
"11"> decrby views 8  Decrement in steps of 8
(integer) 3> get views
"3"> set key1 "hello,fly"
OK> get key1
"hello,fly"> getrange key1 0 4   String truncation
"hello"> getrange key1 0 -1
"hello,fly"> setex key3 30 "hello"   Set expiration time
OK> ttl key3
(integer) 26> get key3
(nil)> ttl key3
(integer) -2> setnx mykey redis   mykey No, setting succeeded
(integer) 1                    1 for success and 0 for failure> keys *
1) "key2"
2) "key1"
3) "mykey"> setnx mykey mongodb  mykey Exist, setting failed
(integer) 0> get mykey
"redis"> mset key1 v1 key2 v2 key3 v3    Set multiple values
OK> keys *
1) "key2"
2) "key1"
3) "key3"> mget key1 key2 key3   Get multiple values
1) "v1"
2) "v2"
3) "v3"
msetnx key1 v1 key4 v4
(integer) 0> getset db redis          If not, return nil
(nil)> get db
"redis"> getset db mongodb     If it exists, return the current value first, and then set it to a new value
"redis"> get db

//It is interlinked with CAS structure

set user:1 { name:zhangsan , age:3 }Set up a user:1 Object value is json string to save an object
The key here is a clever design: user: {ID}: {file} so it is completely ok in redis> mset user:1:name zhangsan user:1:age 17
OK> mget user:1:name user:1:age
1) "zhangsan"
2) "17"

String similar usage scenarios:
value can be a number as well as a string
- counter
- count multiple units{ uid:9282329 : follow 0} can be inc uid:9282329:follow

List (basic data type)

In redis, you can play redis as stack, queue, blocking queue, etc. all list commands start with l

LPUSH RPUSH> lpush list one   Insert one or more values into the list header
(integer) 1> lpush list two
(integer) 2> lpush list three
(integer) 3> lrange list 0 -1
1) "three"
2) "two"
3) "one"> lrange list 0 1
1) "three"
2) "two"> rpush list 1    Put one or more values at the end of the list
(integer) 7> rpush list 2
(integer) 8> rpush list 3
(integer) 9> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "1"
5) "2"
6) "3"

LPOP RPOP > remove an rpop list from the back
"3" > remove an lpop list from the front

Lindex > lindex list 1 get the value of index position

Llen> Llen list
(integer) 7

Remove the specified value: Lrem> lrem list 1 one   remove 1 individual "one"
(integer) 1> lrange list 0 -1
1) "two"
2) "3"
3) "2"
4) "1"
5) "1"
6) "2"

ltrim trim: list> lpush mylist hello1
(integer) 1> lpush mylist hello2
(integer) 2> lpush mylist hello3
(integer) 3> ltrim mylist 1 2
OK> lrange mylist 0 -1
1) "hello2"
2) "hello1"

Rpolpush removes the last element of the list and moves it to a new list> lrange mylist 0 -1
1) "hello2"
2) "hello1"> rpoplpush mylist myotherlist
"hello1"> lrange mylist 0 -1
1) "hello2"> lrange myotherlist 0 -1
1) "hello1"

LSET specifies the subscript update value> lset mylist 0 "hello3"

linsert inserts a value before or after the specified location> linsert mylist before(after) hello3 "other"
(integer) 2> lrange mylist 0 -1
1) "other"
2) "hello3"

List is actually a linked list. Before node after, left and right can insert values
If the key does not exist, create a new linked list
key exists, new content
If the key is removed and it is an empty linked list, it does not exist
Insert on both sides, or change the value, the most efficient! Low efficiency of operating intermediate elements
Message queuing! Message queue (Lpush Rpop) stack (Lpush lpop)

Set (set)

Value in set cannot be duplicate

Sadd ,Smember , SisMember> sadd myset "hello"   set Collection add element
(integer) 1> sadd myset "fly"
(integer) 1> sadd myset "love fly"
(integer) 1> SMEMBERS myset     View assignments set All values of
1) "love fly"
2) "hello"
3) "fly"> SISMEMBER myset hi     see set Include this element or not,With return 1 ,0 was not returned
(integer) 0> SISMEMBER myset hello
(integer) 1

Scard> scard myset    obtain set Number of in set
(integer) 3

Srem> srem myset hello  remove set Elements of
(integer) 1

Set disorder, do not repeat set, random > srandmember myset randomly extracts an element
"love fly"> SRANDMEMBER myset
"love fly"> SRANDMEMBER myset
"love fly"> SRANDMEMBER myset
"love fly"> SRANDMEMBER myset
"fly" > srandmember myset 2 randomly extracts the specified number of elements
1) "love fly"
2) "fly"

Spop delete the specified key, and then delete the key! > spop myset randomly delete some elements in the set set

Smove moves a specified value to another collection> smove myset newset fly   If there is no target set, the elements of the first set will be deleted and the target set will not be created
(integer) 0> SMEMBERS newset
(empty array)> SMEMBERS myset
1) "love fly"

Set set set scene:
Microblog, station B, common concern (intersection)
Weibo: A user puts all the people concerned in A set set! Put fans in the collection
Common concerns, hobbies and second Friends of users A and B (recommended friends)
Digital set class:
- difference set> sadd key a
(integer) 1> sadd key b
(integer) 1> sadd key c
(integer) 1> sadd key1 d
(integer) 1> sadd key1 c
(integer) 1> sadd key1 e
(integer) 1> SDIFF key key1   Difference set
1) "b"
2) "a"
    -intersection> SINTER key key1  Find intersection
1) "c"
    -Union> SUNION key key1
1) "b"
2) "c"
3) "d"
4) "a"
5) "e"

Hash (hash, Map set)

The storage is key - map! value is map set
Hash is not much different from String in nature. It's still a key value. Only the value value is map (key value)!> hset myhash field fly   set up key-map value
(integer) 1> hget myhash field   obtain key-map value
"fly"> hmset myhash field fly field hello Set multiple key-map Value, repeated setting will overwrite
OK> hget myhash field 
"hello"> hmget myhash field field1 Get multiple key-map value
1) "fly"
2) "hello"> hgetall myhash  Get all values
1) "field"     field-fly  Belong to one key-value 
2) "fly"
3) "field1"
4) "hello"> hdel myhash field1   Delete the specified one value value
(integer) 1> hgetall myhash
1) "field"
2) "fly"> hmset myhash field1 1 field2 2
OK> hlen myhash      Get length
(integer) 3> hexists myhash field    Judge whether the field is saved
(integer) 1> hkeys myhash  Get only key value
1) "field"
2) "field1"
3) "field2"> hvals myhash  Get only value value
1) "fly"
2) "1"
3) "2"> hincrby myhash field1 10 Grow in specified steps 
(integer) 11> hincrby myhash field1 -1 Reduce in specified steps(A negative value is decrement) 
(integer) 10> hgetall myhash
1) "field"
2) "fly"
3) "field1"
4) "11"
5) "field2"
6) "2"> hsetnx myhash field f  If it exists, the setting fails. If it does not exist, it is created(Distributed lock:success version+1)
(integer) 0

hash stores changed data:
user name age, especially the storage of user information and frequently changed information
hash is more suitable for object storage and String is more suitable for String storage

Zset (ordered set: Sort Set)

A value is added based on set, set K1 V1 Zset K1 Score1 v1

Add value zadd key score (sort by score) value> zadd myset 1 one
(integer) 1> zadd myset 2 two 3 three
(integer) 2> zrange myset 0 -1
1) "one"
2) "two"
3) "three"

Sort zrangebyscore key min max with places > zrangebycore salary - inf + inf WithCores
1) "gou"
2) "2000"
3) "fly"
4) "2500"
5) "lei"
6) "2600"

Zrevrange key start stop> zrevrange salary 0 -1
1) "lei"
2) "gou"

Remove element Zrem> zrem salary fly
(integer) 1
 View the number of elements Zcard> zcard salary
(integer) 2

Query the number of score in the specified interval zcount key min max> zcount salary 0 2700
(integer) 2

Application scenario:

If you need to work, you can query the official documents!
set sorting can store class grade table and salary table sorting,
General message (1), important message (2)
Leaderboard application, TOP N test

Three special data types

geospatial (map, geographic location)

Positioning of friends, people nearby, and Realization of taxi distance
redis's Geo can calculate the location information (distance between two places, people in a few miles around)
You can query some test data
There are six commands:

Geoadd key longitude latitude member
Rule: there is no way to add two levels of the earth (North and south poles). Generally, the city data will be downloaded, and the file will be read and imported through the java program
Parameter: key (longitude, latitude, name)
Effective longitude - 85.05 to 85.05
Effective latitude - 180 to 180> geoadd china:city 116.40 39.30 beijing
(integer) 1> geoadd china:city 121.47 31.32 shanghai
(integer) 1> geoadd china:city 106.50 29.53 chongqing
(integer) 1> geoadd china:city 114.08 22.54 shenzhen
(integer) 1> geoadd china:city 120.15 30.28 hangzhou
(integer) 1> geoadd china:city 108.94 34.26 xian
(integer) 1

Geodist (GEO distinct: get the distance between given two positions)
-m unit meter
-Km unit km
-mi unit mile
-ft in thousands> geodist  china:city beijing  Shanghai straight line distance from Beijing to Shanghai m
"1008342.6601"> geodist  china:city beijing  Shanghai km the linear distance between Beijing and Shanghai km
"1008.3427"> geodist  china:city beijing  Chongqing km distance from Beijing to Chongqing km

GeoHash (returns GeoHash representation of one or more locations)> GEOHASH china:city beijing
 1) "wwfz8drghe0" represents the 11 bit hash of the current city longitude and latitude string. If the two strings are closer, the closer the distance is
    Geopos (get longitude and latitude of the specified geographic location)> geopos china:city xian
1) 1) "108.93999785184860229"
2) "34.25999964418929977"> geopos china:city beijing
1) 1) "116.39999896287918091"
2) "39.30000117660147652"> geopos china:city chongqing
1) 1) "106.49999767541885376"
2) "29.52999957900659211"> geopos china:city shenzhen
1) 1) "114.08000081777572632"
2) "22.53999903789756587"> geopos china:city hangzhou
1) 1) "120.15000075101852417"
2) "30.2800007575645509"> 

Geordius (find out the elements within a certain radius with the given longitude and latitude as the center)
Wechat to find people near your friends? (get the address of nearby people, locate) query by radius!> GEORADIUS  china:city 110  Within a radius of 1000 km and with 110 30 as the center
1) "chongqing"
2) "shenzhen"
3) "hangzhou"
Get the designated number of people 200> GEORADIUS  china:city 110  Get 1 person from 30 1000 km withcoord withdist count 1
1) 1) "chongqing"
2) "341.9374"
3) 1) "106.49999767541885376"
2) "29.52999957900659211"

All data should be entered china:city In order to make the result more accurate

Location between cities (navigation)> GEORADIUSBYMEMBER china:city beijing 1000 km
1) "beijing"
2) "xian"> GEORADIUSBYMEMBER china:city shanghai 400 km
1) "hangzhou"
2) "shanghai"

The implementation principle of GEO bottom layer is Zset!> type china:city
You can use the Zset command to operate Geo> zrange china:city 0 -1   View all elements of the map
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"
6) "beijing"> zrem china:city shenzhen  Delete specified map elements
(integer) 1> zrange china:city 0 -1
1) "chongqing"
2) "xian"
3) "hangzhou"
4) "shanghai"
5) "beijing"



What is the cardinality?
Cardinality (non repeating elements): introduction to redis 2.8.9 updates the Hyperloglog data structure! Redis Hyperloglog cardinality statistics algorithm!
Advantages: the memory occupied is fixed, 2 ^ 64 different elements are non-technical, only 12KB memory is needed. From the perspective of memory, hyperlog is the preferred UV(Uniqe) for web pages
Visitor: the number of independent visitors who visit a website many times is counted as one person)

The traditional way: set saves the user's id, and then counts the number of elements in the set as the standard judgment
If this method saves a large number of user IDs and takes up too much memory, it will be very troublesome! The purpose is to count, not to save the id
0.81% error rate! The task of UV statistics is negligible

Test use> pfadd mykey a b c d e f g h i j  Create the first set of elements
(integer) 1> pfcount mykey  Count the cardinality number of the first group of elements
(integer) 10> pfadd mykey2 i j z x c v b n m
(integer) 1> pfcount mykey2
(integer) 9> PFMERGE mykey3 mykey2 mykey Merge two groups mykey + mykey2 ->mykey3 
OK> pfcount mykey3
(integer) 15

Application scenario

Make page statistics
If fault tolerance is allowed, Hyperloglog must be used
Fault tolerance is not allowed. Just use set or your own data type!

Bitmap (bitmap)

Bit storage: all binary bits are used for recording. If it is not 0, the number of infected persons will be counted. If it is not 0, the number of infected persons will be counted. If it is 1, the user information will be counted,
Number of active or inactive people
365 days = 365bit 1 byte = 8bit 46 bytes can be stored (save memory)
As long as there are two states, you can use bitmap

Use bitmaps to record the clock in setbit key offer value from Monday to Sunday
Monday: 1 Tuesday: 1 Wednesday: 0> setbit sign 0 1
(integer) 0> setbit sign 1 1
(integer) 0> setbit sign 2 0
(integer) 0> setbit sign 3 0
(integer) 0> setbit sign 4 1
(integer) 0> setbit sign 5 1
(integer) 0> setbit sign 6 1
(integer) 0

Check whether there is a punch in getbit key offer one day> getbit sign 1
(integer) 1> getbit sign 3
(integer) 0
//Count the number of days to punch bitcount key [start end]> bitcount sign 0 6
(integer) 5

Redis transaction operation

What is a transaction: a collection of commands executed together
All commands in a transaction will be serialized and executed in order when the transaction is executed! One off, sequential, exclusive
---------------Queue set set set execution----------------

Either succeed at the same time, or fail at the same time, atomicity
redis transaction does not guarantee atomicity!!!
redis single command is atomic!!!
redis transaction has no isolation level concept! (no unreal reading, dirty reading, non repeatable reading...)
All commands are not executed directly in the transaction! Only when the execution command is initiated can it be executed!

redis transaction:
- open transaction (multi)
- order to join ()
- execute transaction (exec)

Normal transaction execution:> multi  Open transaction
OK> set key1 v1
QUEUED> set key2 v2
QUEUED> set key3 v3
QUEUED> get key2
QUEUED> get key1
QUEUED> exec   Execute transaction
1) OK
2) OK
3) OK
4) "v2"
5) "v1"> multi  Open transaction
OK> set key1 v1
QUEUED> set key2 v2
QUEUED> set key4 v4
QUEUED> DISCARD  Cancel transaction
OK> get key4 Commands in the transaction queue will not be executed

Compile exception (redis command error): all commands in the transaction will not be executed> multi
OK> set k1 v1
QUEUED> set k2 v2
QUEUED> getset k3 v3
QUEUED> getset k4
(error) ERR wrong number of arguments for 'getset' command> set k5 v5
QUEUED> exec
(error) EXECABORT Transaction discarded because of previous errors.> get v5   All commands will not be executed

Runtime exception (syntax error): there is a syntax problem in the transaction. Other commands can be executed normally when executing,
Bad command throw exception (so no atomicity)> multi
OK> set k2 "v2"
QUEUED> incr k2
QUEUED> exec
1) OK
2) (error) ERR value is not an integer or out of range

Lock: Redis can realize optimistic lock and watch monitoring!
Pessimistic lock: very pessimistic, think that whenever there will be problems, no matter what you do will be locked! Affect performance
Optimistic lock: very optimistic. I don't think there will be any problem, so I won't lock it! When updating data, judge whether someone has modified the data during this period
1. get version
2. Compare version when updating

Once the monitoring test of Redis succeeds, the monitoring will be cancelled
Normal execution successful: single thread> set money 100
OK> set out 0
OK> watch money  monitor money object
OK> multi  The transaction ends normally, and there is no change in the data period
OK> decrby money 20
QUEUED> incrby out 20
QUEUED> exec
1) (integer) 80
2) (integer) 20> mget money out
1) "80"
2) "20"

Multithreading: multiple client operations> watch money
OK> multi
OK> decrby money 10
QUEUED> incrby out 10
//Thread 2 came in and modified money> get money
"80"> set money 1000
OK> exec  Commit transaction discovery meney If it is modified, it will fail

//If the transaction fails to execute, you need to unlock it first, and then get the latest value> unwatch  1.Discover transaction execution failure, unlock first
OK> watch money 2.Get the latest value, monitor again, select version
OK> multi
OK> decrby money 80
QUEUED> incrby out 80
QUEUED> exec   3.Before each execution, check whether the monitored value has changed
1) (integer) 920
2) (integer) 100

watch can be used as optimistic lock operation of redis!

Jedis operation redis

Using java to operate redis
What is jedis?  
Is the official recommendation of Java connection development tools! Using java to operate redis

Step 1:

<!-- Import jedis 's bag-->>

    <!--Import fastjson -->

2. Coding test:
- connect to database
- operation command
- disconnect

public class TestPing {
    public static void main(String[] args) {
        //1. Create a Jedis object
        Jedis jedis = new Jedis("",6379);
        //All the commands of Jedis are those of redis
        //Operation String hash set ZSet list geo hyperloglog bitmap


java common API for five data types

Operate redis keys

Jedis jedis = new Jedis("", 6379);

System.out.println("wipe data :"+jedis.flushDB());
System.out.println("Determine whether a value exists:"+jedis.exists("username"));
System.out.println("newly added<username , fly>Key value pair of:"+jedis.set("username","fly"));
System.out.println("newly added<password , 123>Key value pair of:"+jedis.set("password","123"));
System.out.print("All keys of the system are as follows:");
Set<String> keys = jedis.keys("*");
System.out.println("All keys:"+keys);
System.out.println("Delete key password:"+jedis.del("password"));
System.out.println("Judgment key password Does it exist:"+jedis.exists("password"));
System.out.println("see username Stored value type:"+jedis.type("username"));
System.out.println("Random return key A value of space:"+jedis.randomKey());
System.out.println("rename key:"+jedis.rename("username","name"));
System.out.println("Extract value:"+jedis.get("name"));
System.out.println("Query by index:";
System.out.println("Delete current database all key value:"+jedis.flushDB());
System.out.println("Return to current database key Number of values:"+jedis.dbSize());
System.out.println("Delete the key value:"+jedis.flushAll());


Jedis jedis = new Jedis("", 6379);

System.out.println("----Add data----");
System.out.println("delete key2:"+jedis.del("key2"));
System.out.println("obtain key2:"+jedis.get("key2"));
System.out.println("modify key1 Value of:"+jedis.set("key1","afterV1"));
System.out.println("obtain key1 Value of:"+jedis.get("key1"));
System.out.println("stay key3 Add value after:"+jedis.append("key3",",hello"));
System.out.println("key3 Value of:"+jedis.get("key3"));
System.out.println("Add multiple key-value value:"+jedis.mset("k1","v1","k2","v2","k3","v3"));
System.out.println("Get multiple key-value value:"+jedis.mget("k1","key1","k2","k3"));
System.out.println("Delete multiple values:"+jedis.del("k1","k2"));
System.out.println("Get multiple values:"+jedis.mget("username","name","k3"));

System.out.println("----New value added prevention coverage----");

System.out.println("---Add value and set effective time---");
System.out.println(jedis.setex("key3",10,"count down"));
try {
} catch (InterruptedException e) {

System.out.println("---Get the original value and update it to the new value---");
System.out.println(jedis.getSet("key1", "newkey1"));

System.out.println("obtain key2 String of:"+jedis.getrange("key2",2,4));
System.out.println("Delete the key value:"+jedis.flushAll());


Jedis jedis = new Jedis("", 6379);
System.out.println("add to list element:"+jedis.lpush("Collection","List","Map","Vector"));
System.out.println("add to list element:"+jedis.lpush("Collection","HashMap"));
System.out.println("add to list element:"+jedis.lpush("Collection","TreeSet"));
System.out.println("Collection Content of(whole):"+jedis.lrange("Collection",0,-1));
System.out.println("Collection Content of[0,3]:"+jedis.lrange("Collection",0,3));
System.out.println("-----Delete the value specified in the list-----");
System.out.println("Delete the specified number of elements:"+jedis.lrem("Collection",2,"HashMap"));
System.out.println("Delete the following table[0,3]Value in interval:"+jedis.ltrim("Collection",0,3));
System.out.println("Stack from the left side of the list:"+jedis.lpop("Collection"));
System.out.println("Add value from right side of list:"+jedis.rpush("Collection","iterator"));
System.out.println("Stack from the right end of the list:"+jedis.rpop("Collection"));
System.out.println("Get the content of the specified subscript:"+jedis.lindex("Collection",2));
System.out.println("modify Collection Value with subscript 1"+jedis.lset("Collection",1,"ArrayList"));
System.out.println("Collection Length of:"+jedis.llen("Collection"));
System.out.println("obtain Collection Value with subscript 2:"+jedis.lindex("Collection",2));
System.out.println("sortList sort(from small to large)Before sorting:"+jedis.lrange("sortList",0,-1));
System.out.println("sortList sort(from small to large)After sorting:"+jedis.sort("sortList"));


Jedis jedis = new Jedis("", 6379);

System.out.println("Add elements to the collection------");
System.out.println(jedis.sadd("eleSet", "e1", "e2", "e3", "e4", "e5", "e6"));
System.out.println("Set All elements in are:"+jedis.smembers("eleSet"));
System.out.println("Delete an element e1:"+jedis.srem("eleSet","e1"));
System.out.println("Set All elements in are:"+jedis.smembers("eleSet"));
System.out.println("Delete 2 elements e3,e4:"+jedis.srem("eleSet","e3","e4"));
System.out.println("Set All elements in are:"+jedis.smembers("eleSet"));
System.out.println("Randomly remove an element from a collection:"+jedis.spop("eleSet"));
System.out.println("Number of elements in the collection:"+jedis.scard("eleSet"));
System.out.println("e1 Is there a collection:"+jedis.sismember("eleSet","e1"));
System.out.println("e2 Is there a collection:"+jedis.sismember("eleSet","e2"));
System.out.println(jedis.sadd("eleSet1","e1","e2", "e3", "e4", "e5", "e6"));
System.out.println(jedis.sadd("eleSet2","e1","e0", "e9", "e6"));
System.out.println("delete eleSet1 Of e2,And move to eleSet2 in:"+jedis.smove("eleSet1","eleSet2","e2"));
System.out.println("eleSet1 Elements in:"+jedis.smembers("eleSet1"));
System.out.println("eleSet2 Elements in:"+jedis.smembers("eleSet2"));
System.out.println("----------Set operation--------");
System.out.println("eleSet1 and eleSet2 Frontal intersection:"+jedis.sinter("eleSet1","eleSet2"));
System.out.println("eleSet1 and eleSet2 Frontal Union:"+jedis.sunion("eleSet1","eleSet2"));
System.out.println("eleSet1 and eleSet2 Frontal difference set:"+jedis.sdiff("eleSet1","eleSet2"));
System.out.println("eleSet3 Value of:"+jedis.smembers("eleSet3"));


Jedis jedis = new Jedis("", 6379);
Map<String,String> map = new HashMap<String,String>();

//Add a hash element with the name (key) hash

System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash"));
System.out.println("hash All keys for are:"+jedis.hkeys("hash"));
System.out.println("hash All values of are:"+jedis.hvals("hash"));
System.out.println("take key66 Save the value plus an integer. If not, create key6:"+jedis.hincrBy("hash","key6",1));
System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash"));
System.out.println("Delete one or more key value pairs:"+jedis.hdel("hash","key1","key6"));
System.out.println("hash All key value pairs are:"+jedis.hgetAll("hash"));
System.out.println("hash The number of all key value pairs is:"+jedis.hlen("hash"));
System.out.println("judge hash Does it exist key2:"+jedis.hexists("hash","key2"));
System.out.println("judge hash Does it exist key3:"+jedis.hexists("hash","key3"));
System.out.println("obtain hash Value in:"+jedis.hmget("hash","key3"));
System.out.println("obtain hash Value in:"+jedis.hmget("hash","key3","key4"));

Topics: Jedis Redis Java Database