Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support writing RDD of multiple zsets #383

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/scala/com/redislabs/provider/redis/redisFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.redislabs.provider.redis.util.PipelineUtils._
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import scala.collection.JavaConversions.mapAsJavaMap
import scala.collection.JavaConverters.mapAsJavaMapConverter

/**
* RedisContext extends sparkContext's functionality with redis functions
Expand Down Expand Up @@ -303,6 +304,17 @@ class RedisContext(@transient val sc: SparkContext) extends Serializable {
kvs.foreachPartition(partition => setZset(zsetName, partition, ttl, redisConfig, readWriteConfig))
}

/**
* @param kvs Write RDD of (zset name, zset member -> score)
* @param ttl time to live
*/
def toRedisZSETs(kvs: RDD[(String, Map[String, String])], ttl: Int = 0)
(implicit
redisConfig: RedisConfig = RedisConfig.fromSparkConf(sc.getConf),
readWriteConfig: ReadWriteConfig = ReadWriteConfig.fromSparkConf(sc.getConf)) {
kvs.foreachPartition(partition => setZset(partition, ttl, redisConfig, readWriteConfig))
}

/**
* @param vs RDD of values
* @param setName target set's name which hold all the vs
Expand Down Expand Up @@ -503,6 +515,33 @@ object RedisContext extends Serializable {
conn.close()
}

/**
* @param zsets zsetName: map of member -> score to be saved in the target host
* @param ttl time to live
*/
def setZset(zsets: Iterator[(String, Map[String, String])],
ttl: Int,
redisConfig: RedisConfig,
readWriteConfig: ReadWriteConfig) {
implicit val rwConf: ReadWriteConfig = readWriteConfig

zsets
.map { case (key, memberScores) =>
(redisConfig.getHost(key), (key, memberScores))
}
.toArray
.groupBy(_._1)
.foreach { case (node, arr) =>
withConnection(node.endpoint.connect()) { conn =>
foreachWithPipeline(conn, arr) { (pipeline, a) =>
val (key, memberScores) = a._2
pipeline.zadd(key, memberScores.mapValues((v) => Double.box(v.toDouble)).asJava)
if (ttl > 0) pipeline.expire(key, ttl.toLong)
}
}
}
}

/**
* @param setName
* @param arr values which should be saved in the target host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ trait RedisRddExtraSuite extends SparkRedisSuite with Keys with Matchers {
verifyHash("hash2", map2)
}

test("toRedisZETs") {
val map1 = Map("k1" -> "3.14", "k2" -> "2.71")
val map2 = Map("k3" -> "10", "k4" -> "12", "k5" -> "8", "k6" -> "2")
val zsets = Seq(
("zset1", map1),
("zset2", map2)
)
val rdd = sc.parallelize(zsets)
sc.toRedisZSETs(rdd)

verifyZSET("zset1", map1)
verifyZSET("zset2", map2)
}

test("connection fails with incorrect user/pass") {
assertThrows[JedisConnectionException] {
new RedisConfig(RedisEndpoint(
Expand Down Expand Up @@ -112,4 +126,9 @@ trait RedisRddExtraSuite extends SparkRedisSuite with Keys with Matchers {
}
}

def verifyZSET(zset: String, vals: Map[String, String]): Unit = {
val zsetWithScore = sc.fromRedisZSetWithScore(zset).sortByKey().collect
zsetWithScore should be(vals.mapValues((v) => v.toDouble).toArray.sortBy(_._1))
}

}