1. NoSQL数据库
redis是基于key-value模型的非关系数据库(NoSQL),常见的非关系数据库如下:
NoSQL数据库的四种分类表
分类 | Examples举例 | 典型应用场景 | 数据模型 | 优点 | 缺点 |
---|---|---|---|---|---|
键值(key-value) | Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB | 内容缓存,主要用于处理大量数据的高访问负载,也用于一些日志系统等等。 | Key 指向 Value 的键值对,通常用hash table来实现 | 查找速度快 | 数据无结构化,通常只被当作字符串或者二进制数据 |
列存储数据库 | Cassandra, HBase, Riak | 分布式的文件系统 | 以列簇式存储,将同一列数据存在一起 | 查找速度快,可扩展性强,更容易进行分布式扩展 | 功能相对局限 |
文档型数据库 | CouchDB, MongoDb | Web应用(与Key-Value类似,Value是结构化的,不同的是数据库能够了解Value的内容) | Key-Value对应的键值对,Value为结构化数据 | 数据结构要求不严格,表结构可变,不需要像关系型数据库一样需要预先定义表结构 | 查询性能不高,而且缺乏统一的查询语法。 |
图形(Graph)数据库 | Neo4J, InfoGrid, Infinite Graph | 社交网络,推荐系统等。专注于构建关系图谱 | 图结构 | 利用图结构相关算法。比如最短路径寻址,N度关系查找等 | 很多时候需要对整个图做计算才能得出需要的信息,而且这种结构不太好做分布式的集群方案。 |
redis支持的数据类型包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型),这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。另外,与memcached一样,为了保证效率,redis的数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。 (Memcached与redis的区别?)
2,centos系统安装redis
下载redis安装包:sudo wget http://download.redis.io/releases/redis-4.0.7.tar.gz
解压并安装:
tar -xzvf redis-4.0.7.tar.gz
make
make install
启动 redis-server
查看状态:redis-cli
(将打开一个 Redis 提示符:redis 127.0.0.1:6379>, 127.0.0.1 是本机的IP地址,6379是 Redis 服务器运行的端口 )
redis 127.0.0.1:6379> ping PONG
2,redis 命令行的使用 (也可以安装redis桌面管理器,通过用户界面来管理Redis 键和数据。下载地址https://redisdesktop.com/download)
redis启动后,输入redis-cli且连接成功就可以使用redis了
string操作:
redis中的String在在内存中按照一个name对应一个value来存储。如图:
help @string 命令能查看关于字符窜的所有操作命令,如下所示:
APPEND key value summary: Append a value to a key key不存在时创建key,赋值value,存在key时,将value追加到原来的值上 since: 2.0.0 BITCOUNT key [start end] summary: Count set bits in a string 对应的值中二进制表示中的1的个数 since: 2.6.0 BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL] summary: Perform arbitrary bitfield integer operations on strings since: 3.2.0 BITOP operation destkey key [key ...] summary: Perform bitwise operations between strings since: 2.6.0 BITPOS key bit [start] [end] summary: Find first bit set or clear in a string since: 2.8.7 DECR key summary: Decrement the integer value of a key by one since: 1.0.0 DECRBY key decrement summary: Decrement the integer value of a key by the given number since: 1.0.0 GET key summary: Get the value of a key 获取key所对应的值 since: 1.0.0 GETBIT key offset summary: Returns the bit value at offset in the string value stored at key 获取key所对应的值的二进制表示的某位的值(0或1) since: 2.2.0 GETRANGE key start end summary: Get a substring of the string stored at a key 获取子序列(类似切片,但根据字节获取,非字符,对于汉字和数字注意) since: 2.4.0 GETSET key value summary: Set the string value of a key and return its old value 设置新值并获取原来的值 since: 1.0.0 INCR key summary: Increment the integer value of a key by one 自增1,必须为整数。 since: 1.0.0 INCRBY key increment summary: Increment the integer value of a key by the given amount 自增对应的值,必须为整数 since: 1.0.0 INCRBYFLOAT key increment summary: Increment the float value of a key by the given amount 自增对应的值,浮点数 since: 2.6.0 MGET key [key ...] summary: Get the values of all the given keys 批量获取值 mget k1 k2 since: 1.0.0 MSET key value [key value ...] summary: Set multiple keys to multiple values 批量设置值 mset k1 v1 k2 v2 since: 1.0.1 MSETNX key value [key value ...] summary: Set multiple keys to multiple values, only if none of the keys exist since: 1.0.1 PSETEX key milliseconds value summary: Set the value and expiration in milliseconds of a key 设置值,过期时间(毫秒) since: 2.6.0 SET key value [EX seconds] [PX milliseconds] [NX|XX] summary: Set the string value of a key 创建键值对,存在则修改 since: 1.0.0 SETBIT key offset value summary: Sets or clears the bit at offset in the string value stored at key 对应值的二进制表示的某位进行操作,设置为0或1 since: 2.2.0 SETEX key seconds value summary: Set the value and expiration of a key key已存在,设置值 since: 2.0.0 SETNX key value summary: Set the value of a key, only if the key does not exist 只有key不存在时,设置键值对 since: 1.0.0 SETRANGE key offset value summary: Overwrite part of a string at key starting at the specified offset 修改字符串内容,从指定字符串索引开始向后替换(新值太长时,则向后添加) since: 2.2.0 STRLEN key summary: Get the length of the value stored in a key 返回name对应值的字节长度(一个汉字3个字节) since: 2.2.0
help command 能查看command的参数和使用,如下:
hash 操作
hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强的数据 , redis中Hash在内存中的存储格式如下图:
关于hash的所有操作如下:
hash操作 HDEL key field [field ...] summary: Delete one or more hash fields 删除key对应的hash中一个或多个field since: 2.0.0 HEXISTS key field summary: Determine if a hash field exists 检测key对应的hash中是否存在field since: 2.0.0 HGET key field summary: Get the value of a hash field 获取key所对应的hash中field的值 since: 2.0.0 HGETALL key summary: Get all the fields and values in a hash 获取key所对应hash的所有键值对 since: 2.0.0 HINCRBY key field increment summary: Increment the integer value of a hash field by the given number since: 2.0.0 HINCRBYFLOAT key field increment summary: Increment the float value of a hash field by the given amount since: 2.6.0 HKEYS key summary: Get all the fields in a hash 获取key对应的hash中所有的field since: 2.0.0 HLEN key summary: Get the number of fields in a hash 获取key对应的hash中键值对的个数 since: 2.0.0 HMGET key field [field ...] summary: Get the values of all the given hash fields 批量获取key所对应的hash中field的值 since: 2.0.0 HMSET key field value [field value ...] summary: Set multiple hash fields to multiple values key对应的hash中批量设置键值对 since: 2.0.0 HSCAN key cursor [MATCH pattern] [COUNT count] 参数:cursor,游标(基于游标分批取获取数据);match,匹配指定key,默认None 表示所有的key;count,每次分片最少获取个数,默认None表示采用Redis的默认分片个数 summary: Incrementally iterate hash fields and associated values 增量式迭代获取,对于数据大的数据非常有用,hscan可以实现分片的获取数据,并非一次性将数据全部获取完,从而防止内存被撑爆 since: 2.8.0 HSCAN myhash 0 MATCH order_* COUNT 1000 HSET key field value summary: Set the string value of a hash field key对应的hash中设置一个键值对field/value, 不存在,则创建;否则,修改 since: 2.0.0 HSETNX key field value summary: Set the value of a hash field, only if the field does not exist field不存在时,key对应的hash中设置键值对 since: 2.0.0 HSTRLEN key field summary: Get the length of the value of a hash field 获得value的长度 since: 3.2.0 HVALS key summary: Get all the values in a hash 获取key对应的hash中所有的value since: 2.0.0
list操作
List操作,redis中的List在在内存中按照一个name对应一个List来存储。如图:
关于list的所有操作:
list操作 BLPOP key [key ...] timeout summary: Remove and get the first element in a list, or block until one is available 将多个列表排列,按照从左到右去pop对应列表的元素 since: 2.0.0 BRPOP key [key ...] timeout summary: Remove and get the last element in a list, or block until one is available 将多个列表排列,按照从右到左去pop对应列表的元素 since: 2.0.0 BRPOPLPUSH source destination timeout summary: Pop a value from a list, push it to another list and return it; or block until one is available 从一个列表的右侧移除一个元素并将其添加到另一个列表的左侧 since: 2.2.0 LINDEX key index summary: Get an element from a list by its index key对应的列表中根据索引获取列表元素 since: 1.0.0 LINSERT key BEFORE|AFTER pivot value summary: Insert an element before or after another element in a list 在key对应的列表的某一个值前或后插入一个新值 since: 2.2.0 LLEN key summary: Get the length of a list key对应的list元素的个数 since: 1.0.0 LPOP key summary: Remove and get the first element in a list key对应的列表的左侧获取第一个元素并在列表中移除,返回值则是第一个元素 since: 1.0.0 LPUSH key value [value ...] summary: Prepend one or multiple values to a list 在key对应的list中添加一个或多个元素,每个新的元素都添加到列表的最左边,RPUSH添加到右边 since: 1.0.0 LPUSHX key value summary: Prepend a value to a list, only if the list exists 在key对应的list中添加元素,只有name已经存在时,值添加到列表的最左边, RPUSHX添加到右边 since: 2.2.0 LRANGE key start stop summary: Get a range of elements from a list 在key对应的列表分片获取数据 since: 1.0.0 LREM key count value summary: Remove elements from a list 在key对应的list中删除指定的值, count=0,删除列表中所有的指定值;count=2,从前到后,删除2个;count=-2,从后向前,删除2个;(同一个value值有多个) since: 1.0.0 LSET key index value summary: Set the value of an element in a list by its index key对应的list中的某一个索引位置重新赋值 since: 1.0.0 LTRIM key start stop summary: Trim a list to the specified range key对应的列表中移除没有在start-end索引之间的值 since: 1.0.0 RPOP key summary: Remove and get the last element in a list since: 1.0.0 RPOPLPUSH source destination summary: Remove the last element in a list, prepend it to another list and return it 从一个列表source取出最右边的元素,同时将其添加至另一个列表destination的最左边 since: 1.2.0 RPUSH key value [value ...] summary: Append one or multiple values to a list since: 1.0.0 RPUSHX key value summary: Append a value to a list, only if the list exists since: 2.2.0
set集合操作
类似于list的存储形式,但没有重复元素。关于set的所有操作:
set集合操作 SADD key member [member ...] summary: Add one or more members to a set key对应的set中添加元素 since: 1.0.0 SCARD key summary: Get the number of members in a set 获取key对应的集合中元素个数 since: 1.0.0 SDIFF key [key ...] summary: Subtract multiple sets 第一个key对应的集合中存在,且不存在于其他key对应的集合中的元素 (即集合相减) since: 1.0.0 SDIFFSTORE destination key [key ...] summary: Subtract multiple sets and store the resulting set in a key 将集合相减的结果放到destinat对应的集合中 since: 1.0.0 SINTER key [key ...] summary: Intersect multiple sets 获取多个key对应集合的交集 since: 1.0.0 SINTERSTORE destination key [key ...] summary: Intersect multiple sets and store the resulting set in a key 将集合的交集放到destinat对应的集合中 since: 1.0.0 SISMEMBER key member summary: Determine if a given value is a member of a set 检查member是否是key对应的集合的成员 since: 1.0.0 SMEMBERS key summary: Get all the members in a set 获取key对应的集合的所有成员 since: 1.0.0 SMOVE source destination member summary: Move a member from one set to another 将某个成员从一个集合中移动到另外一个集合 since: 1.0.0 SPOP key [count] summary: Remove and return one or multiple random members from a set 随机获取并删除一个member since: 1.0.0 SRANDMEMBER key [count] summary: Get one or multiple random members from a set 随机获取一个member since: 1.0.0 SREM key member [member ...] summary: Remove one or more members from a set 集合中删除某些值 since: 1.0.0 SSCAN key cursor [MATCH pattern] [COUNT count] 同字符串的操作,用于增量迭代分批获取元素,避免内存消耗太大 summary: Incrementally iterate Set elements since: 2.8.0 SUNION key [key ...] summary: Add multiple sets 获取多个集合的并集 since: 1.0.0 SUNIONSTORE destination key [key ...] summary: Add multiple sets and store the resulting set in a key 获取多个集合的并集,放到destinat对应的集合中 since: 1.0.0
sorted_set有序集合操作
有序集合,在集合的基础上,为每元素排序;元素的排序需要根据另外一个值来进行比较,所以,对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序。
有序集合的所有操作如下:
sorted_set有序集合操作 ZADD key [NX|XX] [CH] [INCR] score member [score member ...] summary: Add one or more members to a sorted set, or update its score if it already exists 有序集合中添加元素,zadd 'zz' 'n1' 1 'n2' 2 since: 1.2.0 ZCARD key summary: Get the number of members in a sorted set key应的有序集合元素的数量 since: 1.2.0 ZCOUNT key min max summary: Count the members in a sorted set with scores within the given values 获取key应的有序集合中分数 在 [min,max] 之间的个数 since: 2.0.0 ZINCRBY key increment member summary: Increment the score of a member in a sorted set since: 1.2.0 ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] summary: Intersect multiple sorted sets and store the resulting sorted set in a new key 获取两个有序集合的交集到destination,如果遇到相同值不同分数,则按照aggregate进行操作(aggregate的值为: SUM MIN MAX) since: 2.0.0 ZLEXCOUNT key min max summary: Count the number of members in a sorted set between a given lexicographical range since: 2.8.9 ZRANGE key start stop [WITHSCORES] summary: Return a range of members in a sorted set, by index 按照索引范围获取key应的有序集合的元素 since: 1.2.0 ZRANGEBYLEX key min max [LIMIT offset count] summary: Return a range of members in a sorted set, by lexicographical range since: 2.8.9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] summary: Return a range of members in a sorted set, by score since: 1.0.5 ZRANK key member summary: Determine the index of a member in a sorted set 获取某个值在key对应的有序集合中的排行 since: 2.0.0 ZREM key member [member ...] summary: Remove one or more members from a sorted set 删除有序集合中值 since: 1.2.0 ZREMRANGEBYLEX key min max summary: Remove all members in a sorted set between the given lexicographical range since: 2.8.9 ZREMRANGEBYRANK key start stop summary: Remove all members in a sorted set within the given indexes 根据排行范围删除 since: 2.0.0 ZREMRANGEBYSCORE key min max summary: Remove all members in a sorted set within the given scores 根据分数范围删除 since: 1.2.0 ZREVRANGE key start stop [WITHSCORES] summary: Return a range of members in a sorted set, by index, with scores ordered from high to low since: 1.2.0 ZREVRANGEBYLEX key max min [LIMIT offset count] summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings. since: 2.8.9 ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] summary: Return a range of members in a sorted set, by score, with scores ordered from high to low since: 2.2.0 ZREVRANK key member summary: Determine the index of a member in a sorted set, with scores ordered from high to low since: 2.0.0 ZSCAN key cursor [MATCH pattern] [COUNT count] summary: Incrementally iterate sorted sets elements and associated scores 同字符串相似,相较于字符串新增score_cast_func,用来对分数进行操作 since: 2.8.0 ZSCORE key member summary: Get the score associated with the given member in a sorted set 获取key应有序集合中 member对应的分数 since: 1.2.0 ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] summary: Add multiple sorted sets and store the resulting sorted set in a new key since: 2.0.0
通用命令操作 (help @generic)
keys
# KEYS * 匹配数据库中所有 key 。
# KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
# KEYS h*llo 匹配 hllo 和 heeeeello 等。
# KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo
rename key dst 重命名key'
type key 获取key对应值的类型
所有通用命令如下:
generic 通用命令 DEL key [key ...] summary: Delete a key 删除redis中的任意数据类型 since: 1.0.0 DUMP key summary: Return a serialized version of the value stored at the specified key. since: 2.6.0 EXISTS key [key ...] summary: Determine if a key exists 检测redis的key是否存在 since: 1.0.0 EXPIRE key seconds summary: Set a key's time to live in seconds 为redis的某个key设置超时时间 since: 1.0.0 EXPIREAT key timestamp summary: Set the expiration for a key as a UNIX timestamp since: 1.2.0 KEYS pattern summary: Find all keys matching the given pattern 根据模型获取redis的key; #KEYS * 匹配数据库中所有key;KEYS h?llo 匹配hello,hallo和hxllo等;KEYS h[ae]llo 匹配hello和hallo,但不匹配hillo since: 1.0.0 MIGRATE host port key| destination-db timeout [COPY] [REPLACE] [KEYS key] summary: Atomically transfer a key from a Redis instance to another one. since: 2.6.0 MOVE key db summary: Move a key to another database 将redis的某个值移动到指定的db下 since: 1.0.0 OBJECT subcommand [arguments [arguments ...]] summary: Inspect the internals of Redis objects since: 2.2.3 PERSIST key summary: Remove the expiration from a key since: 2.2.0 PEXPIRE key milliseconds summary: Set a key's time to live in milliseconds since: 2.6.0 PEXPIREAT key milliseconds-timestamp summary: Set the expiration for a key as a UNIX timestamp specified in milliseconds since: 2.6.0 PTTL key summary: Get the time to live for a key in milliseconds since: 2.6.0 RANDOMKEY - summary: Return a random key from the keyspace 随机获取一个redis的key(不删除) since: 1.0.0 RENAME key newkey summary: Rename a key 对redis的key重命名 since: 1.0.0 RENAMENX key newkey summary: Rename a key, only if the new key does not exist since: 1.0.0 RESTORE key ttl serialized-value [REPLACE] summary: Create a key using the provided serialized value, previously obtained using DUMP. since: 2.6.0 SCAN cursor [MATCH pattern] [COUNT count] summary: Incrementally iterate the keys space 同字符串操作,用于增量迭代获取key since: 2.8.0 SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination] summary: Sort the elements in a list, set or sorted set since: 1.0.0 TTL key summary: Get the time to live for a key since: 1.0.0 TYPE key summary: Determine the type stored at key 获取key对应值的类型 since: 1.0.0 WAIT numslaves timeout summary: Wait for the synchronous replication of all the write commands sent in the context of the current connection since: 3.0.0 RESTORE-ASKING key arg arg arg ...options... summary: Help not available since: not known LATENCY arg arg ...options... summary: Help not available since: not known POST arg ...options... summary: Help not available since: not known ASKING arg summary: Help not available since: not known SUBSTR key arg arg arg summary: Help not available since: not known GEORADIUSBYMEMBER_RO key arg arg arg arg ...options... summary: Help not available since: not known TOUCH key arg ...options... summary: Help not available since: not known REPLCONF arg ...options... summary: Help not available since: not known PFDEBUG arg arg arg ...options... summary: Help not available since: not known MEMORY arg arg ...options... summary: Help not available since: not known PSYNC arg arg arg summary: Help not available since: not known PFSELFTEST arg summary: Help not available since: not known MODULE arg arg ...options... summary: Help not available since: not known GEORADIUS_RO key arg arg arg arg arg ...options... summary: Help not available since: not known SWAPDB arg arg arg summary: Help not available since: not known HOST: arg ...options... summary: Help not available since: not known UNLINK key arg ...options... summary: Help not available since: not known
3, python操作redis
安装redis模块 : sudo pipi install redis
3.1 连接redis:
redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。
import redis r = redis.Redis(host='10.211.55.4', port=6379) #redis-sever安装的ip地址 (本机为127.0.0.1)r.set('foo', 'Bar')print(r.get('foo'))
连接远程redis 服务器时,注意修改redis-sever的配置信息:
1.注释掉redis.conf文件中绑定的主机地址,使所有ip可访问,修改完后可以通过命令 ps -ef|grep redis 查看,*表示所有ip均可访问,即修改成功。
#bind 127.0.0.1
2, 修改redis保护模式为no
修改redis.conf中 protected-mode no 或者通过redis-cli客户端:127.0.0.1:6379> config set protected-mode "no"
3,仍无法连接时,注意防火墙设置,网络连接等。参考检查方案:
1.本地防火墙是否关闭2.服务器的防火墙是否关闭 检查方法: service iptables status 查看防火墙状态 service iptables stop 关闭防火墙 chkconfig iptables off 永久关闭防火墙3.本地是否能ping通服务器redis4.修改redis服务器的配置文件 vi redis.conf 注释绑定的主机地址 #bind 127.0.0.1 修改redis的守护进程为no daemonize no 或者通过redis-cli客户端:127.0.0.1:6379> config set daemonize "no" 修改redis的保护模式为no protected-mode no 或者通过redis-cli客户端:127.0.0.1:6379> config set protected-mode "no"
连接池:redis-py使用connection pool来管理对一个redis server的所有连接,避免每次建立、释放连接的开销。默认,每个Redis实例都会维护一个自己的连接池。可以直接建立一个连接池,然后作为参数Redis,这样就可以实现多个Redis实例共享一个连接池。
3.2 数据操作
string,hash,list,set,sorted-set操作,将上面的命令转换为相应的函数。
参考网址:
https://github.com/andymccurdy/redis-py
https://www.cnblogs.com/alex3714/articles/6217453.html
3.3 管道
redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作
#/usr/bin/python#coding:utf-8import redis pool = redis.ConnectionPool(host='127.0.0.1', port=6379) r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False)pipe = r.pipeline(transaction=True) pipe.lpush('ball', 'basketball')pipe.rpush('ball', 'volleyball')pipe.execute()
3.4 发布和订阅
redis可以实现消息的发布和订阅:
import redisclass Redishelper def __init__(self): self.__conn = redis.Redis(host='127.0.0.1', port=6379) self.chan_sub='100' self.chan_pub='100' def publish(self,msg) self.__conn.publish(self.chan_pub,msg) return True def subscribe(self): pub = self.__conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub
发布者
#发布者from redishelper import Redishelperobj = Redishelper()obj.publish('hello')
订阅者
from redishelper import Redishelper obj = Redishelper()sub_obj = obj.subscribe()while True: msg = sub_obj.parse_response() print(msg)
先执行订阅者,在执行发布者就能收到消息: