def main(args: Array[String]): Unit = { val conf = new SparkConf().setMaster("local[*]") .setAppName("redis") .buildRedis() val sc = new SparkContext(conf) val inithost = sc.getConf.inithost()//原生的操作
val rediscontext=new RedisContext(sc)//原声native的支持,从redis读取数据,主要.getZSet(),要和redis存入的类型一致(list,zset,hash) val runKeyRDD = sc.fromRedisKeyPattern(inithost, "runkey").getZSet() runKeyRDD.foreach(println) val runTestRDD = sc.parallelize(Seq("DB2")).map(x => (x, x.length.toString)) //RDD的类型为RDD[String,String] sc.toRedisZSET(runTestRDD, "runkey", inithost)}//隐式类 implicit class ResiConf(conf: SparkConf) { def buildRedis(): SparkConf = { conf.set("redis.host", "127.0.0.1") conf.set("redis.port", "6379") } def inithost(): (String, Int) = { ("localhost", 6379) } }
spark 支持native redis Api(精华)
最新推荐文章于 2024-08-01 09:35:30 发布