Redis和Hbase互补取值思路

1、思路:

用户取数据首先从Redis中取,Redis中没有数据则将Hbase中的数据插入Redis,且在配置文件中设置redis开关redisSwitch,如果redisSwitch=true则是从Redis中取数据,否则直接从Hbase中取数据,在配置文件中设置Redis的域的有效时间effectiveTime=86400,86400s为24小时,若设置为0s则直接失效。


2、实现:
@RequestMapping(value = "consumer/consumerPortrait.json", produces = "application/json; charset=UTF-8")
public void consumerPortraitJson(@RequestParam("cust_num") String cust_num, HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String resultJson ="";
String tableName = "ns_sousuo:tdm_user_label_info_ed";
String rowKey = cust_num;
if (rowKey == null || rowKey == "") {
rowKey = "";
}
Map<String,String>  redisMap = commonTools.readPropertiesFile("ps_redis");
String redisSwitch = redisMap.get("redisSwitch");
if(redisSwitch.equalsIgnoreCase("true")){//从redis获取数据
ShardedJedis jedis = null;
try {
jedis = pool.getResource();
boolean hexist = jedis.hexists(nimitzRowkey, cust_num);
log.info(String.valueOf(hexist));
if(hexist){
log.info("从redis取数据");
resultJson = jedis.hget(nimitzRowkey, cust_num);
}else{
resultJson = cusService.getResultMapFromHbase(tableName, cust_num);
int effectiveTime = Integer.parseInt(redisMap.get("effectiveTime"));
//离线存储至redis
taskExecutorUtil.setRedis(jedis, nimitzRowkey, cust_num, resultJson, effectiveTime);
log.info("从Hbase离线导入redis");
}

} catch (JedisConnectionException e){
log.error("jedis连接异常", e);
}
}else{//从hbase获取数据
resultJson = cusService.getResultMapFromHbase(tableName, rowKey);
log.info("从hbase取数据");
}
response.getWriter().write(resultJson);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值