redis definition:
What 1.redis that?
redis is a key-value high-performance database, the stored value type comprising support string, list, set, zset and the hash, and support the push / pop add / remove and intersected union and difference operations.
2.redis features:
- Data is cached in memory, will periodically update the data written to disk or to modify the operating log file additional written.
- Deployed in a cluster, the master server for external services, support master-slave synchronization, data can be from any number of servers to sync from the master server supports cascading architecture.
- Achieve a publish / subscribe mechanism, sub-channel data is stored (in a different channel), the premise consumption data to a subscription channel.
- edis worth is not limited to the type of string, but the string of further support:
3.redis architecture:
- No interaction between the center-based architecture, the node
- The position of the master node hash algorithm key values and their mappings stored, each node maintains the mapping between the key and the server
- client can initiate a request to any node, it does not matter from the master, if the server is client nodes are looking for, it will respond, if not, it will only redirect client
- If between the client request and redirects the request, topology changes, the next request will be redirected to redirect again, until you find the right server
- But fusionInsight HD in redis clusters only support a master-slave mode
4. Scenario:
Get the latest news
leaderboard application
counter application
in accordance with: high-performance low-latency access to rich data structures to support persistence
5. Implement product recommendation system:
program:
the introduction of redis, reduce the number of read data from the database and file system ;
cache the results to redis, realization and read and take; relative to the HBase faster because HBase although the index still going to access the underlying file system
structure design:
because the stored data is unstructured, there is no time to read table class information
using hash access structure:
<Key, value> ->
<the userid, name, Xiaoming>
<the userid, Age, 18 is>
MapReduce task timing to the user information imported from redis HBase; and
get data in the start redis obtain, if not then go get HBase, and sync to redis
redis cluster operations
note:
- Introducing an amount of data is limited to a memory configuration and redis cluster instance, the total capacity of the number of instances redis * = the memory size of a single instance;
- And should be split data to a plurality of the key, so that the data load balancing.
API the Java:
1. cluster initialization
using jedis
public RedisTest() {
//实例化一个集群的集合
Set<HostAndPort> hosts = new HashSet<HostAndPort>();
//通过ip地址和端口号获取到集群
hosts.add(new HostAndPort(”node01“, 6379));
int timeout = 5000;
client = new JedisCluster(hosts, timeout);
}
String type stores:
public void testString() {
String key = "sid-user01";
// 设置指定key值,过期时间,sessionid
client.setex(key, 5, "A0BC9869FBC92933255A37A1D21167B2");
//获取指定的key值
String sessionId = client.get(key);
LOGGER.info("User " + key + ", session id: " + sessionId);
try {
Thread.sleep(10000);//进程休息
} catch (InterruptedException e) {
LOGGER.warn("InterruptedException");
}
sessionId = client.get(key);
LOGGER.info("User " + key + ", session id: " + sessionId);
key = "message";
//追加key,value
client.set(key, "hello");
String value = client.get(key);
LOGGER.info("Value: " + value);
client.append(key, " world");
value = client.get(key);
LOGGER.info("After append, value: " + value);
client.del(key);
}
hash storage type:
public void testHash() {
String key = "userinfo-001";
// 以key,field,value的形式存入数据
client.hset(key, "id", "J001");
client.hset(key, "name", "John");
client.hset(key, "gender", "male");
client.hset(key, "age", "35");
client.hset(key, "salary", "1000000");
// get方式获取 一个字段的值
String id = client.hget(key, "id");
String name = client.hget(key, "name");
LOGGER.info("User " + id + "'s name is " + name);
Map<String, String> user = client.hgetAll(key);
LOGGER.info(user);
client.del(key);
key = "userinfo-002";
Map<String, String> user2 = new HashMap<String, String>();
user2.put("id", "L002");
user2.put("name", "Lucy");
user2.put("gender", "female");
user2.put("age", "25");
user2.put("salary", "200000");
client.hmset(key, user2);
//追加
client.hincrBy(key, "salary", 50000);
id = client.hget(key, "id");
String salary = client.hget(key, "salary");
LOGGER.info("User " + id + "'s salary is " + salary);
// like Map.keySet()
Set<String> keys = client.hkeys(key);
LOGGER.info("all fields: " + keys);
// like Map.values()
List<String> values = client.hvals(key);
LOGGER.info("all values: " + values);
// 获取多个字段的值
values = client.hmget(key, "id", "name");
LOGGER.info("partial field values: " + values);
// 判断key值是否已经存在
boolean exist = client.hexists(key, "gender");
LOGGER.info("Exist field gender? " + exist);
// 删除key
client.hdel(key, "age");
keys = client.hkeys(key);
LOGGER.info("after del field age, rest fields: " + keys);
client.del(key);
}