sparksql到redis到mysql_spark统计完后将数据存入redis数据库的工具类JedisUtils代码

importcom.google.common.collect.Lists;importcom.google.common.collect.Maps;importcom.google.common.collect.Sets;importorg.apache.commons.lang3.StringUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;importredis.clients.jedis.exceptions.JedisException;import java.io.*;importjava.lang.reflect.Method;importjava.util.List;importjava.util.Map;importjava.util.Set;/*** Jedis Cache 工具类

*

*@authorhpc

*@version2017-8-29*/

public classJedisUtils {private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);/*** 链接url*/

//如果是本机,则用默认的的127地址,远程连接用对方ip//private static String ADDR = "127.0.0.1";

private static String ADDR = "10.101.148.11";private static int PORT = 6379;/*** 密码*/

private static String AUTH = null;//private static int MAX_ACTIVE = 300;//private static int MAX_IDLE = 200;//private static int MAX_WAIT = 10000;

/*** 超时时间*/

private static int TIMEOUT = 10000;private static boolean TEST_ON_BORROW = true;private static JedisPool jedisPool = null;/*** 初始化Redis连接池*/

static{try{

init();

}catch(Exception e) {

logger.error("初始化Redis出错," +e);

}

}/*** 初始化连接池

*

*@see[类、类#方法、类#成员]*/

private synchronized static voidinit() {

JedisPoolConfig config= newJedisPoolConfig();//config.setMaxIdle(MAX_IDLE);//config.setMaxWaitMillis(MAX_WAIT);

config.setTestOnBorrow(TEST_ON_BORROW);//config.setMaxTotal(MAX_ACTIVE);

jedisPool = newJedisPool(config, ADDR, PORT, TIMEOUT, AUTH);

}/*** 获取缓存

*@paramkey 键

*@return值*/

public staticString get(String key) {

String value= null;

Jedis jedis= null;try{

jedis=getResource();if(jedis.exists(key)) {

value=jedis.get(key);

value= StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;

logger.debug("get {} = {}", key, value);

}

}catch(Exception e) {

logger.error("get {} = {} error = {}", key, value);

}finally{

returnResource(jedis);

}returnvalue;

}/*** 获取缓存

*@paramkey 键

*@return值*/

public staticObject getObject(String key) {

Object value= null;

Jedis jedis= null;try{

jedis=getResource();if(jedis.exists(getBytesKey(key))) {

value=toObject(jedis.get(getBytesKey(key)));

logger.debug("getObject {} = {}", key, value);

}

}catch(Exception e) {

logger.warn("getObject{} "+ key+"=" +value);

}finally{

returnResource(jedis);

}returnvalue;

}/*** 设置缓存

*@paramkey 键

*@paramvalue 值

*@paramcacheSeconds 超时时间,0为不超时 以秒为单位

*@return

*/

public static String set(String key, String value, intcacheSeconds) {

String result= null;

Jedis jedis= null;try{

jedis=getResource();

result=jedis.set(key, value);if (cacheSeconds != 0) {

jedis.expire(key, cacheSeconds);

}

logger.debug("set {} = {}", key, value);

}catch(Exception e) {//logger.warn("set {} = {}", key, value, e);

} finally{

returnResource(jedis);

}returnresult;

}/*** 设置缓存

*@paramkey 键

*@paramvalue 值

*@paramcacheSeconds 超时时间,0为不超时

*@return

*/

public static String setObject(String key, Object value, intcacheSeconds) {

String result= null;

Jedis jedis= null;try{

jedis=getResource();

result=jedis.set(getBytesKey(key), toBytes(value));if (cacheSeconds != 0) {

jedis.expire(key, cacheSeconds);

}

logger.debug("setObject {} = {}", key, value);

}catch(Exception e) {

logger.warn("getObject{} "+ key+"=" +value);

}finally{

returnResource(jedis);

}returnresult;

}/*** 获取List缓存

*@paramkey 键

*@return值*/

public static ListgetList(String key) {

List value = null;

Jedis jedis= null;try{

jedis=getResource();if(jedis.exists(key)) {

value= jedis.lrange(key, 0, -1);

logger.debug("getList {} = {}", key, value);

}

}catch(Exception e) {//logger.warn("getList {} = {}", key, value, e);

logger.warn("getList{} "+ key+"=" +value);

}finally{

returnResource(jedis);

}returnvalue;

}/*** 获取List缓存

*@paramkey 键

*@return值*/

public static ListgetObjectList(String key) {

List value = null;

Jedis jedis= null;try{

jedis=getResource();if(jedis.exists(getBytesKey(key))) {

List list = jedis.lrange(getBytesKey(key), 0, -1);

value=Lists.newArrayList();for (byte[] bs :

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值