RedisManager
package com.domo.es
import java.util
import org.slf4j.{Logger, LoggerFactory}
import redis.clients.jedis.{Jedis, JedisCluster, JedisPool, ScanParams, ScanResult}
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
import scala.io.Source
object RedisManager {
private val logger: Logger = LoggerFactory.getLogger(this.getClass.getSimpleName.stripSuffix("$"))
val oneIndex = "CXSYZZ:IDX:CID"
val secondIndex = "CXSZYY:IDX:SECOND:"
def main(args: Array[String]): Unit = {
if (args.length < 1){
logger.info("#####传入的参数不能为null#####")
return
}
val keyResult = args(0)
if (keyResult == "1"){
cidDel("")
}else{
matchDel(args(0))
}
}
/**
* 精准删除redis中的一、二级索引中的相关信息
*
* @param cids cid的数组
*/
def cidDel(pathName: String) = {
val input: List[String] = Source.fromFile("E:\\doc\\cids.txt").getLines().toList
val jdCluster: JedisCluster = JedisUtil.getRedisTemplate //对于数据量少的时候,已经把节点给你封装好了
//删除redis中的一级索引
for (elem <- input) {
jdCluster.zrem(oneIndex, elem)
}
//删除redis中的二级索引
for (elem <- input) {
jdCluster.del(secondIndex + s"{${elem}}")
}
}
/**
* 根据key模糊匹配删除相关内容
*
* @param pattern
*/
def matchDel(pattern: String) = {
val pn = JedisUtil.getPartitionNumber
for (i <- 0 to pn - 1) {
val jedis: Jedis = JedisUtil.getJedisByPartitionCode(i)
var scanRet = "0" //从第一条开始拿取
try {
do {
val params = new ScanParams()
params.`match`(s"${pattern}") //以什么匹配拿取,默认匹配拿取个数是10
val ret = jedis.scan(scanRet, params)
scanRet = ret.getStringCursor
val result = ret.getResult.asScala.toList
for (elem <- result) {
jedis.del(elem)
logger.info(s"#####模糊匹配key为:${elem},删除成功#####" )
}
} while (!"0".equals(scanRet)) //游标值为 "0" 时遍历自动结束
} catch {
case e: RuntimeException =>
e.printStackTrace()
} finally {
jedis.close()
}
}
}
}