使用redis进行火花rdd操作

时间:2019-07-15 03:14:05

标签: apache-spark spark-streaming

我正在尝试将所有值设置为fieldValueMap以便将其提取到Redis,但似乎该部分不起作用。

kafkaInputStream.foreachRdd { (rdd, time) => 
   val redisSink = new RedisSink(streamingConfig.redisClusterZone)
   val fieldValueMap: mutable.Map[String, String] = redisSink.getJedisCluster.hgetAll("mykey").asScala

   rdd.mapPartitions(_.map(record => {
      val value: JValue = JsonMethods.parse(record.value)
      val errString = value \ "errString"

      /** redis */
      val index = if (!fieldValueMap.contains(errString)) {
          fieldValueMap += (errString -> fieldValueMap.size.toString)
          fieldValueMap.size.toString
        } else {
          fieldValueMap(errString)
        }

      val output = ("errString" -> errString) ~ ("index" -> index)
      output
   }))

   fieldValueMap.foreach { case (k, v) =>
      redisSink.getJedisCluster.hsetnx("infotech-access", k, v)
   }
   redisSink.closeJedis()
}

0 个答案:

没有答案