package com.aliyun.odps.spark.examples.makedata

import com.aliyun.odps.TableSchema
import com.aliyun.odps.data.Record
import examples.dataloader.RecommRedisFeatureConstructor
import org.apache.spark.aliyun.odps.OdpsOps
import org.apache.spark.sql.SparkSession
import com.aliyun.odps.spark.examples.myUtils.{ParamUtils, env}

import java.util
import scala.collection.JavaConversions._


object makedata_03_deleteredis {
  def main(args: Array[String]) {
    val spark = SparkSession
      .builder()
      .appName(this.getClass.getName)
      .getOrCreate()
    val sc = spark.sparkContext


    // 读取数据库odps
    val accessKeyId = "LTAIWYUujJAm7CbH"
    val accessKeySecret = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
    val odpsUrl = "http://service.odps.aliyun.com/api"
    val tunnelUrl = "http://dt.cn-hangzhou.maxcompute.aliyun-inc.com"

    val project = "loghubods"
    val tableItem = "alg_recsys_video_info"
    val tableUser = "alg_recsys_user_info"
    val partition = "dt=20231220"

    val odpsOps = OdpsOps(sc, accessKeyId, accessKeySecret, odpsUrl, tunnelUrl)

    //用户测特征处理
    val userData = odpsOps.readTable(project = project, table = tableUser, partition = partition, transfer = handleUser, numPartition = 100)
    val userDataTake = userData.take(10)
    userDataTake.foreach(r=>{
      println(r.get(0) + "\t" + r.get(1))
    })

    val userDataTakeRddRun = userData.mapPartitions(row=>{
      val redisTemplate = env.getRedisTemplate()
      val redisFormat = new util.HashMap[String, String]
      row.foreach(r =>{
        val key = r.get(0)
        val value = r.get(1)
        redisFormat.put(key, value)
        if (redisTemplate.hasKey(key)){
          redisTemplate.delete(key)
        }
      })
      // redisTemplate.delete(redisFormat.keySet().toList)
      redisFormat.iterator
    })
    println("delete.user.action.count="+userDataTakeRddRun.count())


  }

  def handleUser(record: Record, schema: TableSchema): util.ArrayList[String] = {
    val feature = RecommRedisFeatureConstructor.constructUserFeature(record)
    val key = String.format("user_info_%s", feature.getUid)
    val value = feature.getValue
    val kv = new util.ArrayList[String](2)
    kv.add(key)
    kv.add(value)
    kv
  }



}