In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to talk to you about what the 8 big data type of Redis is. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Redis-key
127.0.0.1 > keys * (empty list or set) 127.0.0.1 > set name xxx OK 127.0.0.1 > keys * 1) "name" 127.0.0.1 > set age 1 OK 127.0.0.1 > keys * 1) "age" 2) "name" 127.0.0.1 > exists name # to determine whether key exists (integer) 1 127.0. 0.1move name1 6379 > exists name1 (integer) 0 127.0.0.1 move name1 > move name1 (integer) 1 127.0.0.1 move name1 > keys * 1) "age" 127.0.0.1 > set name yyy OK 127.0.0.1 > expire name 10 # sets the expiration time of the key The remaining expiration time of the current key (integer) 7 127.0.1 key 6379 > ttl name (integer)-2 127.0.1 key 6379 > type age # View the current key type string 127.0.0.16379 >
Redis has the following five basic data types
1. String (string)
127.0.0.1 set key1 v1 # setting value OK 127.0.0.1 get key1 "v1" 127.0.0.1 append key1 "hello" # added value, if it does not exist, it is equivalent to set key (integer) 7 127.0.1 get key1 6379 > get key1 "v1hello" 127.0.0.1 get key1 6379 > strlen key1 # get string length (integer) 7 127.0.1
Self-increasing and self-decreasing
127.0.0.1 set views 0 OK 127.0.0.1 get views "0" 127.0.0.1 get views 6379 > incr views # self-increasing 1 (integer) 1 127.0.0.1 get views 6379 > get views "1" 127.0.0.1 get views 6379 > decr views # self-subtracting 1 (integer) 0 127.0.0.1 lug 6379 > decr views (integer)-1 127.0.1 0.1 incr views > get views "- 1" 127.0.0.1 incrby views 6379 > set step size, Increase 10 (integer) 9 127.0.1 integer 6379 > decrby views 5 # set step size, minus 5 (integer) 4
String range
127.0.1 6379 > set key1 "hello,world!" OK 127.0.0.1 hell 6379 > get key1 "hello,world!" 127.0.0.1 hello,world! "127.0.0.1 getrange key1 0 3 # intercept the string [0,3]" hell "127.0.0.1 hello,world 6379 > get all strings, just like get key" hello,world! "127.0.0.1 Suzhou 6379 >
Replace:
127.0.0.1 set key2 abcdefg OK 6379 > get key2 "abcdefg" 127.0.0.1 abcdefg 6379 > setrange key2 1 xx (integer) 7 127.0.0.1 xx 6379 > get key2 "axxdefg" 127.0.0.16379 >
Setex (set with expire): set expiration time
And setnx (set if not exist): there are no more settings (often used in distributed locks)
127.0.0.1OK 6379 > setex key3 30 "hello" # expires after 30 seconds setting OK 127.0.0.1 OK 6379 > ttl key3 # remaining expiration time (integer) 25 127.0.0.1 OK 6379 > setnx mykey "redis" # successfully set (integer) 1 127.0.0.1 OK 6379 > keys * 1) "key2" 2) "key1" 3) "views" 4) Setting failed when "mykey" 127.0.0.1 mykey 6379 > setnx mykey "mongoDB" # mykey exists (integer) 0 127.0.1 mykey 6379 > get mykey # mykey value unchanged "redis" 127.0.0.1 mykey 6379 >
Mset and mget
Keys * 1) "K1" 2) "K3" 3) "K1" 2) "K3" 3) "K2" 127.0.1 keys K2 K3 # get multiple values at the same time 1) "v1" 2) "v2" 3) "v3" 127.0.1) msetnx K1 v1 k4 v4 # msetnx is an atomic operation Either succeed or fail together (integer) 0 127.0.0.1 nil 6379 > get K4 (nil) 127.0.0.1
Object
Set user:1 {name:zhangsan, age:3} # sets the value of a user:1 object to the json character to save an object 127.0.0.1 json 6379 > mset user:1:name zhangsan user:1:age 2 OK 127.0.0.1 age:3 6379 > mget user:1:name user:1:age 1) "zhangsan" 2) "2" 127.0.0.1 age:3 6379 >
Getset: first get and then set
127.0.0.1 getset db redis # if no value exists, return nil (nil) 127.0.0.1 nil 6379 > get db "redis" 127.0.0.1 nil 6379 > getset db mongodb # if there is a value, get the original value and set the new value "redis" 127.0.0.1 nil 6379 > get db "mongodb" 127.0.0.1
String usage scenario: value can also be a number in addition to a string
Counter
Count the number of multiple units
Number of fans
Object cache storage
2. List (list)
Basic data types, lists.
List can be used as a stack, queue, or blocking queue in Redis.
Most list commands start with l.
127.0.0.1 6379 > lpush list one # will have one or more values Insert into the head of the list (left) (integer) 1 127.0.0.1 integer 6379 > lpush list two (integer) 2 127.0.0.1 integer > lpush list three (integer) 3 127.0.0.1 integer 6379 > lrange list 0-1 # View all elements 1) "three" 2) "two" 3) "one" 127.0.0.1 integer > lrange list 0 1 # get values by interval 1) "three" 2) "two" 127.0.0.1 rpush list right # will have one or more values Insert at the end of the list (right) (integer) 4 127.0.0.1 two 6379 > lrange list 0-11) "three" 2) "two" 3) "one" 4) "right" 127.0.0.1
Eject pop
127.0.0.1world 6379 > lrange list 0-11) "!" 2) "world" 3) "world" 4) "hello" 127.0.0.1 world 6379 > lpop list # remove the first element of list "!" 127.0.0.1 lpop list 0-11) "world" 2) "world" 3) "hello" 127.0.0.1 > rpop list # move Except for the first element of list, "hello" 127.0.0.1 lrange list 6379 > lrange list 0-11) "world" 2) "world" 127.0.0.1 lrange list 6379 >
Index Lindex
127.0.0.1world 6379 > lrange list 0-11) "hjk" 2) "world" 3) "world" 127.0.0.1 lindex list 1 # get a value "world" 127.0.0.16379 > lindex list 0 "hjk" 127.0.0.16379 > in list by subscript.
Llen length:
127.0.0.1 6379 > llen list (integer) 3 127.0.0.1
Remove the specified value:
127.0.0.1 lrange list 6379 > world 0-1 1) "hjk" 2) "world" 3) "world" 127.0.1 world 6379 > lrem list 1 world # removes the specified number of value in the lrem list collection Exact match (integer) 1 127.0.0.1) lrange list 0-11) "hjk" 2) "world" 127.0.0.1 > lpush list hjk (integer) 3 127.0.0.1 > lrange list 0-11) "hjk" 2) "hjk" 3) "world" 127.0.1 1 hjk 6379 > lrem list 2 hjk (integer) 2 127.0.1 1 1) "world" 127.0.0.1 purl 6379 >
Trim truncation
127.0.0.1 lrange mylist 0-11) "hello1" 2) "hello2" 3) "hello3" 4) "hello4" 127.0.0.1 hello2 6379 > ltrim mylist 12 # the specified length is truncated by subscript. This list has been destroyed, and only the truncated element OK 127.0.1 1hello4 0-11) "hello2" 2) "hello3" 127.0.0.1 hello3 6379 > is left.
Rpoplpush: remove the last element of the list and move it to the new list.
127.0.0.1 lrange mylist 0-11) "hello1" 2) "hello2" 3) "hello3" 127.0.1 hello2 6379 > rpoplpush mylist myotherlist # removes the last element of the list and moves it to the new list. "hello3" 127.0.0.1 hello1 6379 > lrange mylist 0-1 # View the original list 1) "hello1" 2) "hello2" 127.0.0.1 hello1 6379 > lrange myotherlist 0-1 # View target list, this value does exist 1) "hello3" 127.0.0.1 hello1 6379 >
Lset: replace the value of the specified subscript in the list with another value, update operation
127.0.0.1item 6379 > exists list # determine whether the list exists (integer) 0 127.0.1 lset list 0 item # if it does not exist, update will report an error (error) ERR no such key 127.0.1 item # > lpush list value1 (integer) 1 127.0.0.1 > item 01) "value1" 127.0.0.1 lset list 0 item # if it exists Update the current subscript value OK 127.0.0.1 other 6379 > lset list 1 other # if it does not exist, the update will report an error (error) ERR index out of range 127.0.0.1 other 6379 >
Linsert: inserts a specific value before or after an element in the list
127.0.0.1) lrange mylist 0-11) "hello1" 2) "hello2" 127.0.0.1 > linsert mylist before "hello2" hello (integer) 3 127.0.0.1 (integer) 3 127.0.0.1) lrange mylist 0-11) "hello1" 2) "hello" 3) "hello2" 127.0.1 > linsert mylist after "hello2" hello (integer) 4 127.0.1 > lrange mylist 0-1 1 ) "hello1" 2) "hello" 3) "hello2" 4) "hello" 127.0.0.1
Summary:
List is actually a linked list, which can be inserted before and after.
If key does not exist, create a new linked list
If all values are removed, the empty linked list also means it doesn't exist.
Inserting or changing values on both sides is the most efficient.
3. Set (collection)
127.0.0.1 set 6379 > sadd myset "hello" # add elements in the collection
(integer) 1 127.0.0.1 world 6379 > sadd myset "world" (integer) 1 127.0.0.1 world 6379 > smembers myset # View all values of the specified Set 1) "world" 2) "hello" 127.0.0.1 world 6379 > sismember myset hello # determine whether a value is in set (integer) 1 127.0.1 integer > sismember myset hello1 (integer) 0 127.0.0.1 : 6379 > 127.0.0.1 sadd myset 6379 > scard myset # get the number in the set (integer) 2127.0.0.1 integer > smembers myset 1) "world" 2) "hello2" 3) "hello" 127.0.0.1 > srem myset hello # remove elements (integer) 1 127.0.1 > smembers myset 1) "world "2)" hello2 "127.0.0.1 world 6379 > smembers myset 1)" kkk "2)" world "3)" hjk "4)" hello2 "127.0.0.1 kkk 6379 > srandmember myset # randomly select an element" hjk "127.0.0.1 kkk 6379 > srandmember myset" hello2 "127.0.0.1 kkk 6379 > srandmember myset 2 Number of elements 1) "world" 2) "hello2" 127.0.0.1 srandmember myset 6379 > srandmember myset 21) "hello2" 2) "hjk" 127.0.0.1 srandmember myset 6379 > smembers myset 1) "kkk" 2) "world" 3) "hjk" 4) "hello2" 127.0.0.1lug 6379 > spop myset # randomly delete the element "hjk" 127. 0.0.1 smembers myset 1) "kkk" 2) "world" 3) "hello2" 127.0.0.1 hello2 > spop myset "hello2" 127.0.0.1 > smembers myset 1) "kkk" 2) "world" 127.0.0.1 > smembers myset 1) "kkk" 2) "world" 127.0.1 > sadd myset2 set2 (integer) 1 127.0.0.1 6379 > smove myset myset2 "kkk" # set a specific value Move to another set collection (integer) 1 127.0.0.1 smembers myset 6379) "world" 127.0.0.1 smembers myset 1) "kkk" 2) "set2" 127.0.0.1 smembers myset 6379 > smembers key1 1) "b" 2) "a" 3) "c" 127.0.0.1 smembers myset 6379 > smembers key2 1) "e" 2) " D "3)" c "127.0.0.1 sdiff key1 key2 # difference 1)" b "2)" a "127.0.0.1 sunion key1 key2 # intersection 1)" c "127.0.1 sunion key1 key2 # Union 1)" e "2)" a "3)" c "4)" d "5)" b "
4. Hash (hash)
It is also in the form of key-value, but value is a map.
127.0.0.1 key-value 6379 > hset myhash field xxx # set one key-value (integer) 1 127.0.0.1 key-value 6379 > hget myhash field # get a field value "xxx" 127.0.0.1 key-value 6379 > hmset myhash field1 hello field2 world # set multiple key-value OK 127.0.0.1key-value 6379 > hmget myhash field field1 field2 # get multiple field values 1) "xxx" 2) "hello" 3) "world" "127.0.0.1 xxx 6379 > hgetall myhash # get all data 1)" field "2)" xxx "3)" field1 "4)" hello "5)" field2 "6)" world "127.0.0.1 xxx 6379 > hdel myhash field1 # Delete the specified key There is no corresponding value (integer) 1 127.0.0.1 xxx 6379 > hgetall myhash 1) "field" 2) "xxx" 3) "field2" 4) "world" 127.0.0.1 xxx 6379 > 127.0.0.1 xxx 6379 > hlen myhash # get length (integer) 2 127.0.0.1xxx 6379 > hexists myhash field1 # to determine whether a specified key exists (integer) 0 127.0.0.1 : 6379 > hexists myhash field2 (integer) 1 127.0.0.1 hkeys myhash # get all key 1) "field" 2) "field2" 127.0.0.1 hkeys myhash 6379 > hvals myhash # get all value 1) "xxx" 2) "world" 127.0.0.1 hkeys myhash 6379 > hset myhash field3 5 (integer) 1 127.0.0.1 key 6379 > hincrby myhash field3 1 # Specify increment (integer) 6 127.0.0.1 hsetnx myhash field4 hello 6379 > hincrby myhash field3-1 (integer) 5 127.0.0.1 purl 6379 > hsetnx myhash field4 hello # if it does not exist, you can set (integer) 1 127.0.1 purl 6379 > hsetnx myhash field4 world # if it exists, it cannot be set (integer) 0 127.0.1
Hash is suitable for storing frequently changing object information, while String is more suitable for storing strings.
5. Zset (ordered set)
127.0.0.1 zadd myset 1 one # add a value (integer) 1 127.0.1 integer > zadd myset 2 two 3 three # add multiple values (integer) 2 127.0.1 1 two 6379 > zrange myset 0-11) "one" 2) "two" 3) "three" 127.0.0.1
Implement sorting:
127.0.0.1 xiaohong (integer) 1 127.0.0.1 integer > zadd salary 5000 xiaoming (integer) 1 127.0.0.1 integer > zadd salary 5000 xaiozhang (integer) 1 127.0.1 (integer) 1 127.0.1 xaiozhang 2) "xiaohong" 3) "xiaoming" 127.0.0.1 xaiozhang 6379 > zrangebyscore salary-inf + inf # shows all users from small to large 1) "xaiozhang" 2) "xiaohong" 3) "xiaoming" 127.0.0.1 zrevrange salary 6379 > zrevrange salary 0-1 # sort from large to small 1) "xiaoming" 2) "xiaohong" 3) "xaiozhang" 127.0.0.1 xiaoming 6379 > zrangebyscore salary-inf + inf withscores # display all users 1) "xaiozhang" 2) "500" 3) "xiaohong" 4) "2500" "5)" xiaoming "6)" 5000 "127.0.0.1 inf 2500 withscores # shows users whose wages are less than 2500 1)" xaiozhang "2)" 500 "3)" xiaohong "4)" 2500 "127.0.0.1 xaiozhang 6379 > zrange salary 0-11)" xaiozhang "2)" xiaohong "3)" xiaoming "127.0.0.1 inf 6379 > zrem salary xiaohong # Remove the specific element (integer) 1 127.0.0.1 xaiozhang 6379 > zrange salary 0-11) "xaiozhang" 2) "xiaoming" 127.0.0.1 xaiozhang 6379 > zcard salary # to get the number of ordered sets (integer) 2 127.0.0.1 xaiozhang 6379 > 127.0.0.1 world 6379 > zadd myset 1 hello (integer) 1 127.0.0.1 xaiozhang 6379 > zadd myset 2 world 3! (integer) 2 127.0.0.1 zcount myset 6379 > zcount myset 1 3 # get the number of personnel in the specified range (integer) 3 127.0.1 integer > zcount myset 12 (integer) 2
Three special data types of Redis
1 、 geospatial
Redis introduced the Geo type in 3.2, which can calculate the geographical location information and the distance between the two places.
Documentation: https://www.redis.net.cn/order/3687.html
Simulate some data with the help of the website: http://www.jsons.cn/lngcode/
Geoadd add geolocation
Rules: poles cannot be added directly. City data will generally be downloaded and imported directly through the Java program.
Valid longitude ranges from-180 to 180 degrees. Valid latitudes range from-85.05112878 to 85.05112878 degrees. When the coordinate location is outside the specified range, the command returns an error.
(error) ERR invalid longitude latitude pair xxx yyy
Add some simulation data:
127.0.0.1 beijing (integer) 1 127.0.0.1 beijing (integer) 1 127.0.0.1 shanghai (integer) 1 127.0.0.1 > geoadd china:city 106.50 29.53 chongqing 114.05 22.52 shengzhen (integer) 2 127.0.1 hangzhou 108.96 34.26 xian (integer) 2 127.0.1 xian
Geopos obtains the current positioning coordinate values
1) "116.39999896287918091" 2) "39.900009167092543" 127.0.0.1 geopos china:city shanghai 6379 > 1) "121.47000163793563843" 2) "31.22999903975783553" 127.0.1 6379 >
Geodist gets the distance between two locations
Unit:
M represents the unit in meters.
Km means the unit is kilometer.
Mi stands for miles.
Ft means the unit is feet.
If the user does not explicitly specify the unit parameter, GEODIST defaults to meters.
127.0.0.1 6379 > geodist china:city beijing shanghai km # View the direct straight line distance between Beijing and Shanghai: "1067.3788" 127.0.1 6379 > geodist china:city beijing chongqing km "1464.0708" 127.0.0.1
Georedius centers on a given latitude and longitude to find out the elements within a certain radius
127.0.1 georadius china:city 6379 > 11030 1000 km # centered on 110,30 1) "chongqing" 2) "xian" 3) "shengzhen" 4) "hangzhou" 127.0.0.1 km > georadius china:city 11030500 km 1) "chongqing" 2) "xian" 127.0.0.1xian 6379 > georadius china:city 11030500 km withcoord # display other people's location information 1) 1) "chongqing" 2) 1) "106.49999767541885376" 2) "29.52999957900659211" 2) 1) "xian" 2) 1) "108.96000176668167114" 2) "34.25999964418929977" 127.0.1xian 6379 > 127.0.0.1 km withdist # display distance to the central point 1) 1) "chongqing" 2) "341.9374" 2) 1) "xian" 2) "483. 8340 "127.0.0.1 km withdist withcoord count 6379 > chongqing 11030500 km withdist withcoord count 1 # specified quantity 1) 1)" chongqing "2)" 341.9374 "3) 1)" 106.49999767541885376 "2)" 29.52999957900659211 "127.0.0.1km withdist withcoord count 6379 > georadius china:city 11030500 km withdist withcoord count 21) 1)" chongqing "2)" 341.9374 "3) 1) "106.49999767541885376" 2) "29.52999957900659211" 2) "xian" 2) "483.8340" 3) 1) "108.96000176668167114" 2) "34.25999964418929977" 127.0.0.16379 >
GEORADIUSBYMEMBER finds other elements that lie around the specified element
127.0.0.1 georadiusbymember china:city shanghai 1000 km 1) "hangzhou" 2) "shanghai" 127.0.0.1 hangzhou 6379 >
The underlying implementation principle of geo is actually zset. You can use the zset command to operate geo.
127.0.0.1 zrange china:city 0-11) "chongqing" 2) "xian" 3) "shengzhen" 4) "hangzhou" 5) "shanghai" 6) "beijing" 127.0.0.1 hangzhou 6379 > zrem china:city beijing # Delete an element (integer) 1 127.0.0.16379 > zrange china:city 0-11) "chongqing" 2) "xian" 3) " Shengzhen "4)" hangzhou "5)" shanghai "127.0.0.1 shanghai 6379 >
2 、 hyperloglog
Cardinal number: the number of elements in a mathematical set that cannot be repeated.
UV (Unique visitor): refers to the natural person who accesses and browses this web page through the Internet. A computer client visited is a visitor, and the same visitor is counted only once in a day.
Redis version 2.8.9 updates the hyperloglog data structure, which is based on cardinality statistics.
The advantage of hyperloglog is that it takes up less memory and is fixed. Storing the cardinality of 2 ^ 64 different elements requires only 12 KB of space. But there may also be an error rate of 0.81%.
This data structure is often used to count the UV of a website. The traditional way is to use set to save the user's ID, and then count the number of elements in the set as a criterion. But this way to save a large number of users ID,ID is generally long, take up space, but also very troublesome. Our goal is to count, not to save data, so this has drawbacks. But it would be more appropriate to use hyperloglog.
127.0.0.1 mykey 6379 > pfadd mykey a b c d e f g h i j # create the first set of elements (integer) 1 127.0.0.1 PFCOUNT mykey # Statistics mykey cardinality (integer) 10 127.0.1 PFCOUNT mykey # create the second set of elements (integer) 1 127.0.1 PFCOUNT mykey # Statistics mykey2 cardinality (integer) 9 127.0.0.1 mykey3 OK 6379 > PFMERGE mykey3 mykey mykey2 # merge two groups mykey mykey2 = > PFCOUNT mykey3 (integer) 15 127.0.0.1 mykey3 OK 6379 >
3. Bitmap bitmap
Bitmap is to set 0 or 1 through the smallest unit of bit, indicating the corresponding value or state of an element. The value of a bit is either 0 or 1; that is, the most information a bit can store is 2.
Bitmap is often used to count user information such as active and inactive fans, login and non-login, whether or not to sign in, etc.
Here is an one-week sign-in case to illustrate its usage:
127.0.0.1 setbit sign 0 1 # Monday (integer) 0127.0.0.1 setbit sign 1 # Tuesday integer 0127.0.0.1 integer > setbit sign 20 # Wednesday unsigned (integer) 0127.0.0.1 purl 6379 > setbit sign 3 1 (integer) 0127.0.1 1R 6379 > setbit sign 4 1 (integer) 0127.0.0. 1setbit sign 6379 > setbit sign 5 1 (integer) 0 127.0.0.1 setbit sign 6 0 (integer) 0 127.0.1
Check to see if you sign in on a certain day:
127.0.0.1GETBIT sign 6379 > GETBIT sign 3 (integer) 1 127.0.0.1 > GETBIT sign 6 (integer) 0 127.0.0.1 >
Statistics: count the number of days of signing in
127.0.0.1 BITCOUNT sign 6379 > Redis (integer) 4 127.0.1 Redis 6379 > after reading the above, do you have any further understanding of what the 8 Redis type is? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.