package com.domo.redisOfficial
import java.io.{BufferedReader, File, FileReader}
import java.util
import com.domo.redisOfficial.RedisManager.{oneIndex, secondIndex}
import com.domo.redisTest.JedisUtil
import redis.clients.jedis.JedisCluster
import scala.collection.JavaConverters._
object ExactDel {
def main(args: Array[String]): Unit = {
if (args.length < 1){
println("#####无参传入#####")
return
}
cidDel(args(0))
}
/**
* 精准删除redis中的一、二级索引中的相关信息
*
* @param pathName 路径
*/
def cidDel(pathName: String) = {
val jdCluster: JedisCluster = JedisUtil.getRedisTemplate
val file = new File(pathName)
val reader = new BufferedReader(new FileReader(file))
val list: util.ArrayList[String] = new util.ArrayList[String]()
try {
var cid = ""
do {
cid = reader.readLine()
if (cid != null) {
list.add(cid)
}
} while (cid != null)
//对集合中的数据进行分批处理
val result: List[String] = list.asScala.toList
val length = result.length //11
// 计算可以分成多少组
val groupSize = 5
val count = (length / groupSize).floor.toInt //总批次 - 1
var index = 0 //计数器
if (count > 1) {
for (key <- 1 to count) {
for (num <- index to (key * groupSize - 1)) {
index = index + 1
val resultOne = jdCluster.zrem(oneIndex, result(num))
val resultSecond = jdCluster.del(secondIndex + s"{${result(num)}}")
println(s"#####精确删除cid为:${result(num)},一级二级索引:${resultOne} ,${resultSecond}, 在文件中第${index}行#####")
}
Thread.sleep(2000) //睡一秒
}
}
//最后一批删除数据
if (index < length) {
for (num <- index to (length - 1)) {
index = index + 1
val resultOne = jdCluster.zrem(oneIndex, result(num))
val resultSecond = jdCluster.del(secondIndex + s"{${result(num)}}")
println(s"#####精确删除cid为:${result(num)},一级二级索引:${resultOne} ,${resultSecond}, 在文件中第${index}行#####")
}
}
} catch {
case e: RuntimeException =>
e.printStackTrace()
} finally {
if (reader != null) reader.close()
}
}
}
精确删除redis数据
最新推荐文章于 2023-02-23 14:42:40 发布