Redis入门之增删改查(命令行)

1、到github上下载需要的版本,我用的是官方的windows版本。
redis下载地址

2、然后启动Redis

Set REDIS_HOME=C:\Database\Redis\Redis-x64-2.8.2103
Set PATH=%REDIS_HOME%;%PATH%

redis-server.exe D:\Database\Redis2.8\db\redis.windows.conf

配置文件redis.windows.conf中,我只修改了这两项:

logfile "D:/Database/Redis2.8/db/logs/redis_log.txt"
dir "D:/Database/Redis2.8/db/data/"

如果要停止redis

#停止Redis
redis-cli -h localhost -p 6379 shutdown

如果要查看redis信息

#查看信息
redis-cli -h localhost -p 6379 info

3、启动redis-cli.exe,连接到redis

#连接Redis
redis-cli -h localhost -p 6379

4、String类型数据操作

localhost:6379> dbsize
(integer) 0
localhost:6379> set key01 a
OK
localhost:6379> set key02 b
OK
localhost:6379> set key03 c
OK
localhost:6379> set key04 d
OK
localhost:6379> set key05 e
OK
localhost:6379> set key06 f
OK
localhost:6379> dbsize
(integer) 6
localhost:6379> set name hansen
OK
localhost:6379> keys key*
1) "key03"
2) "key04"
3) "key01"
4) "key05"
5) "key02"
6) "key06"
localhost:6379> exists key01
(integer) 1
localhost:6379> exists key07
(integer) 0
localhost:6379> get key06
"f"
localhost:6379> get name
"hansen"
localhost:6379> rename name myname
OK
localhost:6379> get myname
"hansen"
localhost:6379> get name
(nil)
localhost:6379> dbsize
(integer) 7
localhost:6379> move key06 1
(integer) 1
localhost:6379> dbsize
(integer) 6
localhost:6379> exists key06
(integer) 0
localhost:6379> select 1
OK
localhost:6379[1]> keys *
1) "key06"
localhost:6379[1]> select 0
OK
localhost:6379> exists key05
(integer) 1
localhost:6379> del key05
(integer) 1
localhost:6379> exists key05
(integer) 0

5、Map类型数据操作

localhost:6379> hset hset01 username "hansen"
(integer) 1
localhost:6379> hset hset02 username "neohope"
(integer) 1
localhost:6379> dbsize
(integer) 2
localhost:6379> hset hset01 sex "male"
(integer) 0
localhost:6379> hset hset02 sex "male"
(integer) 1
localhost:6379> dbsize
localhost:6379> type hset01
hash
(integer) 2
localhost:6379> hlen hset01
(integer) 2
localhost:6379> hkeys hset01
1) "username"
2) "sex"
localhost:6379> hget hset01 username
"hansen"

6、List类型数据操作

localhost:6379> lpush listkey01 a
(integer) 1
localhost:6379> lpush listkey01 b
(integer) 2
localhost:6379> lpush listkey01 c
(integer) 3
localhost:6379> lpush listkey01 d
(integer) 4
localhost:6379> lpush listkey01 e
(integer) 5
localhost:6379>  lrange listkey01 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
localhost:6379> lindex listkey01 3
"b"
localhost:6379> lrem listkey01 0 c
(integer) 1
localhost:6379> lrange listkey01 0 -1
1) "e"
2) "d"
3) "b"
4) "a"
localhost:6379> lpop listkey01
"e"
localhost:6379> lpop listkey01
"d"
localhost:6379> lrange listkey01 0 -1
1) "b"
2) "a"
localhost:6379> lpush listkey02 1
(integer) 1
localhost:6379> lpush listkey02 2
(integer) 2
localhost:6379> lpush listkey02 3
(integer) 3
localhost:6379> lpush listkey02 4
(integer) 4
localhost:6379> lpush listkey02 5
(integer) 5
localhost:6379> lrange listkey02 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
localhost:6379> sort listkey02
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
localhost:6379> lrange listkey02 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
localhost:6379>

7、Set类型数据操作

localhost:6379> sadd keyset01 1 2 3
(integer) 3
localhost:6379> sadd keyset02 4 5 6
(integer) 3
localhost:6379> dbsize
(integer) 2
localhost:6379> type keyset01
set
localhost:6379> smembers keyset01
1) "1"
2) "2"
3) "3"

8、Redis常用命令(在命令行使用时,请把函数的括号及逗号都替换为空格)

连接操作相关的命令
quit 关闭连接(connection)
auth 简单密码认证
对value操作的命令
exists(key) 确认一个key是否存在
del(key) 删除一个key
type(key) 返回值的类型
keys(pattern) 返回满足给定pattern的所有key
randomkey 随机返回key空间的一个key
rename(oldname, newname) 将key由oldname重命名为newname,若newname存在则删除newname表示的key
dbsize 返回当前数据库中key的数目
expire 设定一个key的活动时间(s)
ttl 获得一个key的活动时间
select(index) 按索引查询
move(key, dbindex) 将当前数据库中的key转移到有dbindex索引的数据库
flushdb 删除当前选择数据库中的所有key
flushall 删除所有数据库中的所有key
对String操作的命令
set(key, value) 给数据库中名称为key的string赋予值value
get(key) 返回数据库中名称为key的string的value
getset(key, value) 给名称为key的string赋予上一次的value
mget(key1, key2,…, key N) 返回库中多个string(它们的名称为key1,key2…)的value
setnx(key, value) 如果不存在名称为key的string,则向库中添加string,名称为key,值为value
setex(key, time, value) 向库中添加string(名称为key,值为value)同时,设定过期时间time
mset(key1, value1, key2, value2,…key N, value N) 同时给多个string赋值,名称为key i的string赋值value i
msetnx(key1, value1, key2, value2,…key N, value N) 如果所有名称为key i的string都不存在,则向库中添加string,名称key i赋值为value i
incr(key) 名称为key的string增1操作
incrby(key, integer) 名称为key的string增加integer
decr(key) 名称为key的string减1操作
decrby(key, integer) 名称为key的string减少integer
append(key, value) 名称为key的string的值附加value
substr(key, start, end) 返回名称为key的string的value的子串
对List操作的命令
rpush(key, value) 在名称为key的list尾添加一个值为value的元素
lpush(key, value) 在名称为key的list头添加一个值为value的 元素
llen(key) 返回名称为key的list的长度
lrange(key, start, end) 返回名称为key的list中start至end之间的元素(下标从0开始,下同)
ltrim(key, start, end) 截取名称为key的list,保留start至end之间的元素
lindex(key, index) 返回名称为key的list中index位置的元素
lset(key, index, value) 给名称为key的list中index位置的元素赋值为value
lrem(key, count, value) 删除count个名称为key的list中值为value的元素。count为0,删除所有值为value的元素,count>0从头至尾删除count个值为value的元素,count<0从尾到头删除|count|个值为value的元素。
lpop(key) 返回并删除名称为key的list中的首元素 rpop(key),返回并删除名称为key的list中的尾元素
blpop(key1, key2,… key N, timeout) lpop命令的block版本。即当timeout为0时,若遇到名称为key i的list不存在或该list为空,则命令结束。如果timeout>0,则遇到上述情况时,等待timeout秒,如果问题没有解决,则对keyi+1开始的list执行pop操作。
brpop(key1, key2,… key N, timeout) rpop的block版本。参考上一命令。
rpoplpush(srckey, dstkey) 返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
对Set操作的命令
sadd(key, member) 向名称为key的set中添加元素member
srem(key, member) 删除名称为key的set中的元素member
spop(key) 随机返回并删除名称为key的set中一个元素
smove(srckey, dstkey, member) 将member元素从名称为srckey的集合移到名称为dstkey的集合
scard(key) 返回名称为key的set的基数
sismember(key, member) 测试member是否是名称为key的set的元素
sinter(key1, key2,…key N) 求交集
sinterstore(dstkey, key1, key2,…key N) 求交集并将交集保存到dstkey的集合
sunion(key1, key2,…key N) 求并集
sunionstore(dstkey, key1, key2,…key N) 求并集并将并集保存到dstkey的集合
sdiff(key1, key2,…key N) 求差集
sdiffstore(dstkey, key1, key2,…key N) 求差集并将差集保存到dstkey的集合
smembers(key) 返回名称为key的set的所有元素
srandmember(key) 随机返回名称为key的set的一个元素
对zset(sorted set)操作的命令
zadd(key, score, member) 向名称为key的zset中添加元素member,score用于排序。如果该元素已经存在,则根据score更新该元素的顺序。
zrem(key, member) 删除名称为key的zset中的元素member
zincrby(key, increment, member) 如果在名称为key的zset中已经存在元素member,则该元素的score增加increment;否则向集合中添加该元素,其score的值为increment
zrank(key, member) 返回名称为key的zset(元素已按score从小到大排序)中member元素的rank(即index,从0开始),若没有member元素,返回“nil”
zrevrank(key, member) 返回名称为key的zset(元素已按score从大到小排序)中member元素的rank(即index,从0开始),若没有member元素,返回“nil”
zrange(key, start, end) 返回名称为key的zset(元素已按score从小到大排序)中的index从start到end的所有元素
zrevrange(key, start, end) 返回名称为key的zset(元素已按score从大到小排序)中的index从start到end的所有元素
zrangebyscore(key, min, max) 返回名称为key的zset中score >= min且score <= max的所有元素
zcard(key):返回名称为key的zset的基数 zscore(key, element) 返回名称为key的zset中元素element的
score zremrangebyrank(key, min, max) 删除名称为key的zset中rank >= min且rank <= max的所有元素
zremrangebyscore(key, min, max) 删除名称为key的zset中score >= min且score <= max的所有元素
zunionstore / zinterstore(dstkeyN, key1,…,keyN, WEIGHTS w1,…wN, AGGREGATE SUM|MIN|MAX) 对N个zset求并集和交集,并将最后的集合保存在dstkeyN中。对于集合中每一个元素的score,在进行AGGREGATE运算前,都要乘以对于的WEIGHT参数。如果没有提供WEIGHT,默认为1。默认的AGGREGATE是SUM,即结果集合中元素的score是所有集合对应元素进行SUM运算的值,而MIN和MAX是指,结果集合中元素的score是所有集合对应元素中最小值和最大值。
对Hash操作的命令
hset(key, field, value) 向名称为key的hash中添加元素field<—>value
hget(key, field) 返回名称为key的hash中field对应的value
hmget(key, field1, …,field N) 返回名称为key的hash中field i对应的value
hmset(key, field1, value1,…,field N, value N) 向名称为key的hash中添加元素field i<—>value i
hincrby(key, field, integer) 将名称为key的hash中field的value增加integer
hexists(key, field) 名称为key的hash中是否存在键为field的域
hdel(key, field) 删除名称为key的hash中键为field的域
hlen(key) 返回名称为key的hash中元素个数
hkeys(key) 返回名称为key的hash中所有键
hvals(key) 返回名称为key的hash中所有键对应的value
hgetall(key) 返回名称为key的hash中所有的键(field)及其对应的value
持久化
save 将数据同步保存到磁盘
bgsave 将数据异步保存到磁盘
lastsave 返回上次成功将数据保存到磁盘的Unix时戳
shundown 将数据同步保存到磁盘,然后关闭服务
远程服务控制
info 提供服务器的信息和统计
monitor 实时转储收到的请求
slaveof 改变复制策略设置
config 在运行时配置Redis服务器

NOSQL数据库分类

NOSQL:Not Only SQL

1、列存储(Wide Column Store / Column Families),如
Hadoop / HBase
Cassandra
Hypertable

2、文档存储(JSSON),如
MongoDB
CouchDB

3、文档存储(XML)
EMC Documentum xDB
Berkeley DB XML

4、图形存储(Graph)
Neo4J
TITAN

5、对象存储(Object Databases)
Versant
ObjectDB

6、键值存储(Key Value / Tuple Store)
DynamoDB
Redis
Berkeley DB

7、多值数据库
U2
TigerLogic PICK

8、多模型(Multimodel Databases)
ArangoDB
OrientDB

9、多维(Multidimensional Databases)
Intersystems Cache
MiniM DB

10、网格/云(Grid & Cloud Database Solutions)
Oracle Coherence
Hazelcast

11、事件源(Event Sourcing)
Event Store

12、网络模型(Network Model)
Vyhodb

MongoDB数据引用(Shell)

1、被引用数据

db.address.insert({"city":"shanghai","street":"huaihai road","no":"101"})
db.address.insert({"city":"beijing","street":"taipingqiao road","no":"102"})

2、引用数据

db.persons.insert({"name":"joe","address":{"$ref":"address","$id": ObjectId("55f522e96811e30fd403e83d"),"$db": "test"},"age":20,"sex":"male"})
db.persons.insert({"name":"leo","address":{"$ref":"address","$id": ObjectId("55f522f46811e30fd403e83e"),"$db": "test"},"age":21,"sex":"male"})

3、查询被引用数据

var user = db.persons.findOne({"name":"joe"})
var addressRef = user.address
db[addressRef.$ref].findOne({"_id":(addressRef.$id)})

MongoDB的ObjectId(Shell)

MongoDB中存储的文档必须有一个”_id”键。这个键的值可以是任何类型的,默认是个ObjectId对象。该_id用来确保集合里面每个文档都能被唯一标识,并用来在多个服务器上同步数据。

ObjectId是一个12字节BSON类型数据,格式如下:
前4个字节表示时间戳
接下来的3个字节是机器标识码
接的两个字节由进程id组成(PID)
最后三个字节是随机数

myObjectId = ObjectId()
myObjectId.getTimestamp()

ACID、BASE与CAP

一、关系型数据库遵循的ACID原则

1、A (Atomicity) 原子性
原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整个事务就失败,需要回滚。

比如银行转账,从A账户转100元至B账户,分为两个步骤:1)从A账户取100元;2)存入100元至B账户。这两步要么一起完成,要么一起不完成,如果只完成第一步,第二步失败,钱会莫名其妙少了100元。

2、C (Consistency) 一致性
一致性也比较容易理解,也就是说数据库要一直处于一致的状态,事务的运行不会改变数据库原本的一致性约束。

例如现有完整性约束a+b=10,如果一个事务改变了a,那么必须得改变b,使得事务结束后依然满足a+b=10,否则事务失败。

3、I (Isolation) 独立性
所谓的独立性是指并发的事务之间不会互相影响,如果一个事务要访问的数据正在被另外一个事务修改,只要另外一个事务未提交,它所访问的数据就不受未提交事务的影响。
比如现有有个交易是从A账户转100元至B账户,在这个交易还未完成的情况下,如果此时B查询自己的账户,是看不到新增加的100元的。

4、D (Durability) 持久性
持久性是指一旦事务提交后,它所做的修改将会永久的保存在数据库上,即使出现宕机也不会丢失。

二、非关系型数据库遵循的BASE原则
BASE:Basically Available, Soft-state, Eventually Consistent。 由 Eric Brewer 定义。
1、Basically Availble –基本可用
2、Soft-state –软状态/柔性事务。 “Soft state” 可以理解为”无连接”的, 而 “Hard state” 是”面向连接”的
3、Eventual Consistency –最终一致性 最终一致性, 也是是 ACID 的最终目的。

三、CAP定理(CAP theorem)

在计算机科学中, CAP定理(CAP theorem), 又被称作 布鲁尔定理(Brewer’s theorem), 它指出对于一个分布式计算系统来说,不可能同时满足以下三点:
1、一致性(Consistency) (所有节点在同一时间具有相同的数据)
2、可用性(Availability) (保证每个请求不管成功或者失败都有响应)
3、分隔容忍(Partition tolerance) (系统中任意信息的丢失或失败不会影响系统的继续运作)

CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性这三个需求,最多只能同时较好的满足两个。
因此,根据 CAP 原理将 NoSQL 数据库分成了满足 CA 原则、满足 CP 原则和满足 AP 原则三 大类:
CA – 单点集群,满足一致性,可用性的系统,通常在可扩展性上不太强大。
CP – 满足一致性,分区容忍必的系统,通常性能不是特别高。
AP – 满足可用性,分区容忍性的系统,通常可能对一致性要求低一些。

CAP理论

举个例子来说,HBase与Cassandra,HBase在CAP中,更偏重于CP,客户端读到的数据是一致的,但性能会差;而Cassandra更偏重于AP,性能好一些,但有时客户端会读到不同的数据。

MongoDB聚合操作(Shell)

0、数据准备

for(var i=0;i<10000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age,address:{"city":"shanghai","street":"huaihai road"}});
}

1、count

db.patient.count({"age":12})

2、distinct

db.patient.distinct("age")

3、min, max, sum,avg

db.patient.aggregate([{$group:{_id:"$item",maxAge:{$max:"$age"}}}])
db.patient.aggregate([{$group:{_id:"$item",minAge:{$min:"$age"}}}])
db.patient.aggregate([{$group:{_id:"$item",sumAge:{$sum:"$age"}}}])
db.patient.aggregate([{$group:{_id:"$item",avgAge:{$avg:"$age"}}}])

4、group

db.patient.group({
"key":{"age":true},
"initial":{"patids":[]},
"reduce":function(item,out){out.patids.push(item.patid);},
"finalize":function(out){out.count=out.patids.length;},
"condition":{"age":{$lte:18}}
})

5、map reduce

map=function(){emit(this.age,1);}
reduce=function(key,values){return values.length;}
mropt={"out":"mrresult"}
db.patient.mapReduce(map,reduce,mropt).find()

MongoDB更新操作(Shell)

0、数据准备

for(var i=0;i<10000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age,address:{"city":"shanghai","street":"huaihai road"}});
}

1、默认为全局更新

db.patient.find({"patid":"pat100"})
db.patient.update({"patid":"pat100"},{"patid":"pat100","sex":"F"})
db.patient.find({"patid":"pat100"})

2、局部更新$set

db.patient.find({"patid":"pat101"})
db.patient.update({"patid":"pat101"},{$set:{"sex":"F"}})
db.patient.find({"patid":"pat101"})

3、局部更新$inc

db.patient.find({"patid":"pat102"})
db.patient.update({"patid":"pat102"},{$inc:{"age":-100}})
db.patient.find({"patid":"pat102"})

4、批量更新

db.patient.find({"age":10})
db.patient.update({"age":10},{$set:{"age":11}})
db.patient.find({"age":10})
db.patient.update({"age":10},{$set:{"age":11}},false,true)
db.patient.find({"age":10})

5、更新时,没有匹配则插入

db.patient.find({"patid":"pidx001"})
db.patient.update({"patid":"pidx001"},{"patid":"pidx001","sex":"F"},true)
db.patient.find({"patid":"pidx001"})

MongoDB实现条件查询(Shell)

0、数据准备

for(var i=0;i<10000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age,address:{"city":"shanghai","street":"huaihai road"}});
}

1、比较运算符

运算符 操作符
> $gt
>= $gte
< $lt
<= $lte
!= $ne
= $eq 或 空
db.patient.find({"age":20})
db.patient.find({"age":{$eq:20}})
db.patient.find({"age":{$ne:20}})
db.patient.find({"age":{$gt:20}})
db.patient.find({"age":{$gte:20}})
db.patient.find({"age":{$lt:20}})
db.patient.find({"age":{$lte:20}})

2、逻辑运算符

运算符 操作符
And $and
Or $or
db.patient.find({$and:[{"age":10},{"age":11}]})
db.patient.find({$or:[{"age":10},{"age":11}]})

3、IN 与 Not IN

运算符 操作符
In $in
not in nin
db.patient.find({"age":{$in:[10,11]}})
db.patient.find({"age":{$nin:[10,11]}})

4、where

db.patient.find({$where:function(){return this.patid=='pat1000'}})
db.patient.find({$where:function(){return this.patid=='pat1000' || this.age==1}})

5、正则表达式

#patid以0做结尾
db.patient.find({"patid":/0$/})
#patid以pat开头
db.patient.find({"patid":{$regex:"^pat"}})
#patid以pat开头,切不区分pat大小写
db.patient.find({"patid":{$regex:"^pat",$options:"$i"}})
#patid以pat1做开头,age为10
db.patient.find({"patid":/^pat1/,age:10})

6、分页与排序

db.patient.find({"patid":/0$/}).count()
db.patient.find({"patid":/0$/}).limit(10)
db.patient.find({"patid":/0$/}).skip(10).limit(10)
db.patient.find({"patid":/0$/}).sort({"age":1})
db.patient.find({"patid":/0$/}).sort({"age":-1})

7、between是由min max来实现的

#需要age字段的索引哦
db.patient.find({"patid":/0$/}).min({"age":10}).max({"age":20})

8、全文检索

#建立text索引后,mongo会帮你分词,一个collection只能建立一个text索引
#在patname字段建立全文索引
db.patient.ensureIndex({"address.street":"text"})
#在全部字段建立全文索引
db.patient.ensureIndex({"$**": "text"})
#进行简单查询
db.patient.find({$text:{$search:"huaihai"}})

MongoDB实现分片存储(Shell)

MongoDB分片存储结构如下图所示:
Mongo分片存储

#启动配置服务
mongod --dbpath=D:\Database\MongoDB3\slice\config --port 27018
#启动mongos
mongos --port 27017 --configdb=localhost:27018
#启动mongo分片服务
mongod --dbpath=D:\Database\MongoDB3\slice\slice1 --port 27019
mongod --dbpath=D:\Database\MongoDB3\slice\slice2 --port 27020
mongo localhost:27017
#增加节点
sh.addShard("localhost:27019")
sh.addShard("localhost:27020")
#开启分片
sh.enableSharding("test")
#配置collection
sh.shardCollection("test.patient",{"patid":1},true)
#查看状态
sh.status()
#批量插入数据
#单独连一个mongo查一下试试:)
for(var i=0;i<10000;i++){
var patid="pat"+i;
var patname="name"+i;
var sex="M";
var age=parseInt(100*Math.random(i));
db.patient.insert({"patid":patid,"patname":patname,"sex":sex,"age":age,address:{"city":"shanghai","street":"huaihai road"}});
}