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

feat: zRange add key:value:score #372

Open
wants to merge 1 commit into
base: branch-2.7
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
12 changes: 12 additions & 0 deletions src/main/scala/com/redislabs/provider/redis/rdd/RedisRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ class RedisZSetRDD[T: ClassTag](prev: RDD[String],
nodeKeys.flatMap{k =>
ignoreJedisWrongTypeException(Try(conn.zrange(k, startPos, endPos))).get
}.flatten.iterator
} else if (classTag[T] == classTag[(String, String, Double)]) {
nodeKeys.flatMap { k =>
val a = ignoreJedisWrongTypeException(Try(conn.zrangeWithScores(k, startPos, endPos))).get
a.get.map(x => (k, x.getElement, x.getScore))
}.iterator
} else {
throw new scala.Exception("Unknown RedisZSetRDD type")
}
Expand Down Expand Up @@ -330,6 +335,13 @@ class RedisKeysRDD(sc: SparkContext,
new RedisZSetRDD(this, zsetContext, classOf[String], readWriteConfig)
}


def getZSetWithKey(): RDD[(String, String, Double)] = {
val zsetContext: ZSetContext = new ZSetContext(0, -1, Double.MinValue, Double.MaxValue, true, "byRange")
new RedisZSetRDD(this, zsetContext, classOf[(String, String, Double)], readWriteConfig)
}


/**
* filter the 'zset' type keys and get all the elements(with scores) of them
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/com/redislabs/provider/redis/redisFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ class RedisContext(@transient val sc: SparkContext) extends Serializable {
}
}

def fromRedisZSetWithKey[T](keysOrKeyPattern: T,
partitionNum: Int = 3)
(implicit
redisConfig: RedisConfig = RedisConfig.fromSparkConf(sc.getConf),
readWriteConfig: ReadWriteConfig = ReadWriteConfig.fromSparkConf(sc.getConf)):
RDD[(String, String, Double)] = {
keysOrKeyPattern match {
case keyPattern: String => fromRedisKeyPattern(keyPattern, partitionNum).getZSetWithKey()
case keys: Array[String] => fromRedisKeys(keys, partitionNum).getZSetWithKey()
case _ => throw new scala.Exception(IncorrectKeysOrKeyPatternMsg)
}
}

/**
* @param keysOrKeyPattern an array of keys or a key pattern
* @param partitionNum number of partitions
Expand Down