04_NoSQL数据库之Redis数据库:set类型和zset类型
sets類型及操作
???Set是集合,它是string類型的無序集合。set是通過hash table實現的,添加,刪除和查找復雜度都是0(1)。對集合我們可以取并集、交集、差集。通過這些操作我們可以實現sns中的好友推薦和blog的tag功能。
?
sadd:向集合中添加一個元素,通名稱為key的set中添加元素。
| [toto@localhost bin]$ ./redis-cli 127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset1 two???? 再次添加的時候添加不成功了。 (integer) 0 127.0.0.1:6379> |
通過smembers查看集合中有哪些元素
| 127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> |
?
srem:刪除名稱為key的set中的元素,刪除成功返回1,刪除失敗返回0
| 127.0.0.1:6379> smembers myset2 1) "two" 2) "one" 3) "three" 127.0.0.1:6379> srem myset2 two (integer) 1 127.0.0.1:6379> srem myset2 two (integer) 0 127.0.0.1:6379> smembers myset2 1) "one" 2) "three" 127.0.0.1:6379> |
?
spop:無法指定彈出元素,是隨機返回并刪除名稱為key的set中一個元素。
| 127.0.0.1:6379> sadd myset3 one (integer) 1 127.0.0.1:6379> sadd myset3 two (integer) 1 127.0.0.1:6379> sadd myset3 three (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one" 4) "two" 5) "five" 127.0.0.1:6379> spop myset3???? //隨機彈出的是three "three" 127.0.0.1:6379> smembers myset3 1) "one" 2) "four" 3) "five" 4) "two" 127.0.0.1:6379> spop myset3?? //隨機彈出的是four "four" 127.0.0.1:6379> smembers myset3 1) "one" 2) "five" 3) "two" 127.0.0.1:6379> |
?
sdiff:兩個集合的差集。
返回所有給定key與第一個key的差集。
| 127.0.0.1:6379> smembers myset3 1) "one" 2) "four" 3) "five" 4) "two" 127.0.0.1:6379> spop myset3 "four" 127.0.0.1:6379> smembers myset3 1) "one" 2) "five" 3) "two" 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sadd myset2 one (integer) 1 127.0.0.1:6379> smembers myset2 1) "one" 2) "four" 3) "three" 127.0.0.1:6379> sdiff myset2 myset3??? //去除myset2中含有與myset3元素的值。 1) "three" 2) "four" 127.0.0.1:6379> |
?
sdiffstore:返回所有給定key與第一個key的差集,并將結果存為另一個key.
sdiffstore myset4 myset2 myset3表示的意思是將myset3和myset2的差集存儲到myset4里面。
| 127.0.0.1:6379> sadd myset2 one (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset3 one (integer) 1 127.0.0.1:6379> sadd myset3 two (integer) 1 127.0.0.1:6379> sadd myset3 three (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> sadd myset3 six (integer) 1 127.0.0.1:6379> sdiff myset2 myset3 (empty list or set) 127.0.0.1:6379> sdiff myset3 myset2 ?//顯示myset3中含有myset2的元素 1) "six" 2) "five" 3) "four" 127.0.0.1:6379> sdiffstore myset4 myset3 myset2將myset3和myset2的差集存入myset4中 (integer) 3 127.0.0.1:6379> smembers myset4 1) "six" 2) "five" 3) "four" 127.0.0.1:6379> |
?
sinter:
返回所有給定key的交集
sinterstore將取出的差集存入到另外的一個集合中。
| 127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sadd myset2 five (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> sadd myset3 six (integer) 1 127.0.0.1:6379> sadd myset3 seven (integer) 1 127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "four" 2) "three" 3) "five" 4) "two" 127.0.0.1:6379> smembers myset3 1) "seven" 2) "four" 3) "six" 4) "five" 127.0.0.1:6379> sinter myset3 myset2 1) "four" 2) "five" 127.0.0.1:6379> sinterstore myset6 myset3 myset2 (integer) 2 127.0.0.1:6379> smembers myset6 1) "four" 2) "five" 127.0.0.1:6379> |
?
sunion:表示插入并集。
返回所有給定key的并集。
| 127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sunion myset1 myset2??? 查看并集 1) "three" 2) "two" 3) "four" 4) "one" 127.0.0.1:6379> sunionstore myset7 myset1 myset2?? 將并集的結果存入myset7中 (integer) 4?????????????????? //返回值是4表示成功將4個元素插入myset7中 127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> smembers myset7 1) "three" 2) "two" 3) "four" 4) "one" 127.0.0.1:6379> |
?
smove:從第一個key對應的set中移除member并添加到第二個對應的set中。(將第一個集合中的元素剪切下來放入第二個元素中)
將smove myset2 myset7 three表示:myset2中的three元素扔到myset7中
| 127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> sadd myset1 five (integer) 1 127.0.0.1:6379> smembers myset8 (empty list or set) 127.0.0.1:6379> smove myset1 myset8 two ?將myset1中的two元素移動到myset8中 (integer) 1 127.0.0.1:6379> smembers myset8 1) "two" 127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> |
?
scard返回名稱為key的set的元素個數。(看集合中的元素個數)
| 127.0.0.1:6379> smembers myset1??? 看myset1集合中的元素有哪些 1) "five" 2) "one" 127.0.0.1:6379> smembers myset2?? 看myset2集合中的元素有哪些 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> smembers myset8?? 看myset8集合中的元素有哪些 1) "two" 127.0.0.1:6379> scard myset1??? 查看myset1中的元素個數 (integer) 2 127.0.0.1:6379> scard myset2??? 查看myset2中的元素個數 (integer) 3 127.0.0.1:6379> scard myset8 (integer) 1 127.0.0.1:6379> |
?
sismember:測試member是否是名稱為key的set的元素。(判斷某個元素是否是某個集合中的元素,是返回1,不是返回0)
| 127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> sismember myset1 one??? 判斷one是否是myset1中的元素 (integer) 1 127.0.0.1:6379> sismember myset1 two?? 判斷two是否是myset1中的元素 (integer) 0 127.0.0.1:6379> |
?
srandmember:隨機返回名稱為key的set的一個元素,但不刪除元素。
| 127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> srandmember myset7 "three" 127.0.0.1:6379> srandmember myset7 "two" 127.0.0.1:6379> srandmember myset7 "four" 127.0.0.1:6379> srandmember myset7 "three" |
?
Sorted sets類型及操作
??? Sortedset是set的一個升級版本,它在set的基礎上增加了一個順序屬性,這一屬性在添加修改元素的時候可以指定,每次指定后,zset會自動重新按新的值調整順序。可以理解為由兩列的mysql表,一列存value,一列存順序。操作中key理解為zset的名字。
?
zadd:向名稱為key的zset中添加元素member,score用于排序。如果該元素存在,則更新器順序。語法:zadd set名稱 順序 值
| 127.0.0.1:6379> zadd myzset 1 "one" (integer) 1 127.0.0.1:6379> zadd myzset 2 "two" (integer) 1 127.0.0.1:6379> zadd myzset 3 "two" (integer) 0 ? 這里的0 -1表示順序號?? withscores表示的意思是帶上順序號。 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> ? 再如: 127.0.0.1:6379> zadd sset1 1 two (integer) 1 127.0.0.1:6379> zadd sset1 2 two (integer) 0 127.0.0.1:6379> zadd sset1 3 two (integer) 0 127.0.0.1:6379> zadd sset1 1 one (integer) 1 127.0.0.1:6379> zrange sset1 0 -1 1) "one" 2) "two" 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> |
?
zrem:刪除名稱為key的zset中的元素member
| 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> zrem sset1 two (integer) 1 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> |
?
zincrby:如果在名稱為key的zset中已經存在元素member,則該元素的score增加increment,否則向該集合中添加該元素,其score的值為increment。
| 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> zincrby sset1 2 one??? //將one的順序號增加3,同樣可以減2 "3"?????????????????????????????? //表示one的順序號變成了3 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "3"???????????????????????????? //one的序號變成了3 127.0.0.1:6379> |
?
zrank:返回名稱為key的zset中的member元素的排名。(按score從小到大排序)即下標。(返回的是索引值,和序號值是有差別的)
| 127.0.0.1:6379> zadd sset2 1 one (integer) 1 127.0.0.1:6379> zadd sset2 2 two (integer) 1 127.0.0.1:6379> zadd sset2 3 three (integer) 1 127.0.0.1:6379> zadd sset2 4 four (integer) 1 127.0.0.1:6379> zadd sset2 5 five (integer) 1 127.0.0.1:6379> zrange sset2 0 -1 withscores ?1) "one" ?2) "1" ?3) "two" ?4) "2" ?5) "three" ?6) "3" ?7) "four" ?8) "4" ?9) "five" 10) "5" 127.0.0.1:6379> zrank sset2 four (integer) 3??????? //這里的3表示的是索引值而不是序號值。 127.0.0.1:6379> |
?
zrevrank:返回名稱為key的zset中member元素的排名。(按score從大到小排序)即下標。
| 127.0.0.1:6379> zrange sset2 0 -1 withscores ?1) "one" ?2) "1" ?3) "two" ?4) "2" ?5) "three" ?6) "3" ?7) "four" ?8) "4" ?9) "five" 10) "5" 127.0.0.1:6379> zrevrank sset2 four?? //獲得four的逆向索引 (integer) 1 127.0.0.1:6379> zrevrank sset2 two?? //獲得two的逆向索引 (integer) 3 |
?
zrevrange:返回名稱為key的zset(按score從大到小順序)中的index從start到end的所有元素。(也就是說是反向排序)
| 127.0.0.1:6379> zrevrange sset2 0 -1 withscores ?1) "five" ?2) "5" ?3) "four" ?4) "4" ?5) "three" ?6) "3" ?7) "two" ?8) "2" ?9) "one" 10) "1" 127.0.0.1:6379> |
?
zrangebyscore:找到序號在指定范圍內的元素
| 127.0.0.1:6379> zrevrange sset2 0 -1 withscores ?1) "five" ?2) "5" ?3) "four" ?4) "4" ?5) "three" ?6) "3" ?7) "two" ?8) "2" ?9) "one" 10) "1" 127.0.0.1:6379> zrangebyscore sset2 2 4 withscores 1) "two" 2) "2" 3) "three" 4) "3" 5) "four" 6) "4" 127.0.0.1:6379> |
?
zcount:返回集合中score在給定區間的數量。
| 127.0.0.1:6379> zrevrange sset2 0 -1 withscores ?1) "five" ?2) "5" ?3) "four" ?4) "4" ?5) "three" ?6) "3" ?7) "two" ?8) "2" ?9) "one" 10) "1" 127.0.0.1:6379> zcount sset2 2 4??? 表示包括區間值 (integer) 3 127.0.0.1:6379> zcount sset2 4 5??? 表示包括區間值 (integer) 2 127.0.0.1:6379> |
?
zcard:返回集合中的所有元素的個數
| 127.0.0.1:6379> zrevrange sset2 0 -1 withscores ?1) "five" ?2) "5" ?3) "four" ?4) "4" ?5) "three" ?6) "3" ?7) "two" ?8) "2" ?9) "one" 10) "1" 127.0.0.1:6379> zcard sset2 (integer) 5 127.0.0.1:6379> |
?
zremrangebyrank:刪除集合中排名在給定區間的元素(按照索引來刪除)。
| 127.0.0.1:6379> zrange sset2 0 -1 withscores ?1) "one" ?2) "1" ?3) "two" ?4) "2" ?5) "three" ?6) "3" ?7) "four" ?8) "4" ?9) "five" 10) "5" 127.0.0.1:6379> zremrangebyrank sset2 2 3??? //注意這里的2 3表示的是索引 (integer) 2 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> |
?
zremrangebyscore:刪除集合中score在給定區間的元素(表示的是按照順序刪除)。
| 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> zremrangebyscore sset2 3 4?? //沒有刪除成功的時候返回0 (integer) 0 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> zremrangebyscore sset2 2 5?? //刪除序號為2到5之間的元素 (integer) 2 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> |
?
?
?
?
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的04_NoSQL数据库之Redis数据库:set类型和zset类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10锁屏壁纸怎么设置方法 Win1
- 下一篇: win10网页显示模糊怎么办 Windo