|
1. NoSql是以key-value形式存储,和传统的关系型数据库不一样,不一定遵循传统数据库的一些基本要求,比如说遵循SQL标准,ACID属性,表结构等等,这类数据库主要有一下特点:非关系型的,分布式的,开源的,水平可扩展的。
2. NoSql的特点:
a) 处理超大量的数据。
b) 运行在便宜的pc服务器集群上
c) 击碎了性能瓶颈。
3. NoSql适用场景
a) 对数据高并发读写。(咱们在对mysql进行上万次的写操作,那么咱们的硬盘io就可能无法承受了)
b) 对海量数据的高效率存储和访问。
c) 对数据的高可扩展和高可用性。(数据库可以增加一个服务器节点来完成数据迁移和数据库维护)
4. Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为(数据结构服务器),因为值(value)可以是 字符串(String), 哈希(Map), 链表(list), 集合(sets) 和 有序集合(sorted sets)等类型.
5. Redis是一个key-value存储系统。它支持存储的value类型很多,包括string,list,set,zset(有序集合).这些数据类型都支持push/pop,add/remove及取交集和并集及更丰富的操作,Redis支持各种不同方式的排序(因为redis中有zset数据结构,这是通过一个键来存储它的顺序)。为了保证效率,数据都是缓存在内存中,它可以周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。(把修改操作写入追加的记录文件这个类似于mysql中的bin-log,存储的都是更新数据,插入数据和删除数据的相关操作)
6. Redis提供的API语言:http://www.redis.io/clients
7. Redis适用场合
a) 应用程序直接访问Redis数据库
注意:这样直接访问Redis数据库可能存在某些问题,读写都是直接操作数据库,这样做比较简单,但是如果某天我的某台Redis数据库down掉了,那我的数据就会永久的丢失。
b) 应用程序直接访问Redis,只有Redis访问失败时才访问Mysql
分析:首先我们的应用程序直接先访问我们的Redis server,向redis里面写。此时redis会跟后面的mysql集群进行同步。当redis服务down掉以后,应用服务器会去找后面的mysql。
Redis更加实用的场景:
取最新N个数据的操作
排行榜应用,取TOP N操作
需要精确设定过期时间的应用(可以对键设置过期时间,这个mysql就无法做到)
计数器应用
Uniq操作,获取某段时间所有数据排重值
实时系统,反垃圾系统。
Pub/Sub构建实时消息系统。(发布与订阅是redis独有的系统)
构建队列系统。
缓存
8. mysql与redis、mongodb的比较
Redis
| Mysql
| mongodb
| 有库的概念
| 有库的概念
| 有库的概念
| 无表的概念
| 有表的概念
| 集合
| 无字段的概念
| 有字段的概念(行,列)
| 无字段的概念
|
1.下载地址http://www.redis.io/download
2.可以在linux下运行如下命令进行安装
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make
安装完成后 /opt/redis-2.8.17/src目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli
3.下面启动redis服务
[iyunv@gitclient redis-2.8.17]# src/redis-server
[8491] 02 Nov 22:45:39.989 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
[8491] 02 Nov 22:45:39.991 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[8491] 02 Nov 22:45:39.992 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8491
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[8491] 02 Nov 22:45:39.995 # Server started, Redis version 2.8.17
[8491] 02 Nov 22:45:39.997 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[8491] 02 Nov 22:45:39.997 * The server is now ready to accept connections on port 6379
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动.
[iyunv@gitclient redis-2.8.17]# src/redis-server redis.conf
redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。
1.输入以下命令
[iyunv@gitclient redis-2.8.17]# src/redis-cli
127.0.0.1:6379> set yml bling
OK
127.0.0.1:6379> get yml
"bling"
127.0.0.1:6379>
这里演示了get和set命令操作简单类型value的例子。foo是key ,bar是个string类型的value
没linux的可以通过这个在线的来练习,当然在线版的很多管理相关的命令是不支持的。
http://try.redis-db.com/
1.客户端jar下载地址:http://www.java2s.com/Code/Jar/j/DownloadJRedisjar.htm
MAVEN地址:http://mvnrepository.com/artifact/redis.clients/jedis/2.6.0
2.在eclipse中新建一个maven项目,然后添加jredis包依赖。下面是个简单的实例
1 package com.bling.redis;
2
3 import redis.clients.jedis.Jedis;
4
5
6 /**
7 * Hello world!
8 *
9 */
10 public class App
11 {
12 public static void main( String[] args )
13 {
14 Jedis j = new Jedis("192.168.168.128",6379);
15 String key = "yml";
16 j.set(key, "hello redis jun !");
17 String value = new String(j.get(key));
18
19 String key2 = "count";
20 j.incr(key2);
21 j.incr(key2);
22 String value2 = new String(j.get(key2));
23 System.out.println( "value = "+value );
24 System.out.println( "value2 = "+value2 );
25 }
26 }
4.0.0
com.bling.redis
com.bling.redis
0.0.1-SNAPSHOT
jar
com.bling.redis
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
redis.clients
jedis
2.6.0
运行结果如下:
value = hello redis jun !
value2 = 2 |
|