import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
object SparkStreamingRedis {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local[2]").setAppName(this.getClass.getSimpleName)
val ssc = new StreamingContext(conf,Seconds(3));
val InputStream = ssc.socketTextStream("b02master",222);
InputStream.foreachRDD(rdd=> {
val resultRdd = rdd.flatMap(_.split(" +")).map((_, 1)).reduceByKey(_ + _)
resultRdd.foreach(tp=>{
val jedis = Utils.getRedis
jedis.hincrBy("b02Streaming",tp._1,tp._2)
jedis.close()
})
})
ssc.start()
ssc.awaitTermination()
}
}
Utils
import redis.clients.jedis.JedisPool
object Utils {
private val pool = new JedisPool("b02slave1",6379)
def getRedis={
val jedis = pool.getResource
jedis
}
}