精确删除redis数据

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()
    }
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值