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

Added the option to set ttl from a column. #274

Open
wants to merge 4 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BinaryRedisPersistence extends RedisPersistence[Array[Byte]] {
override def load(pipeline: Pipeline, key: String, requiredColumns: Seq[String]): Unit =
pipeline.get(key.getBytes(UTF_8))

override def encodeRow(keyName: String, value: Row): Array[Byte] = {
override def encodeRow(keyName: String, value: Row, ttlColumn: Option[String] = None): Array[Byte] = {
val fields = value.schema.fields.map(_.name)
val valuesArray = fields.map(f => value.getAs[Any](f))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we exclude TTL column here as well?

SerializationUtils.serialize(valuesArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HashRedisPersistence extends RedisPersistence[Any] {
pipeline.hmget(key, requiredColumns: _*)
}

override def encodeRow(keyName: String, value: Row): Map[String, String] = {
override def encodeRow(keyName: String, value: Row, ttlColumn: Option[String] = None): Map[String, String] = {
val fields = value.schema.fields.map(_.name)
val kvMap = value.getValuesMap[Any](fields)
kvMap
Expand All @@ -39,6 +39,13 @@ class HashRedisPersistence extends RedisPersistence[Any] {
// don't store key values
k != keyName
}
.filter { case (k, _) =>
// don't store TTLs
ttlColumn match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably more efficient in terms of the performance to check if ttlColumn is specified only once rather than for every field.

case Some(ttl) => k != ttl
case None => true
}
}
.map { case (k, v) =>
k -> String.valueOf(v)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ trait RedisPersistence[T] extends Serializable {
*
* @param keyName field name that should be encoded in special way, e.g. in Redis keys.
* @param value row to encode.
* @param ttlColumn field name to be used for setting the ttl and not added as a value
* @return encoded row
*/
def encodeRow(keyName: String, value: Row): T
def encodeRow(keyName: String, value: Row, ttlColumn: Option[String] = None): T

/**
* Decode dataframe row stored in Redis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class RedisSourceRelation(override val sqlContext: SQLContext,
private val persistence = RedisPersistence(persistenceModel)
private val tableNameOpt: Option[String] = parameters.get(SqlOptionTableName)
private val ttl = parameters.get(SqlOptionTTL).map(_.toInt).getOrElse(0)
private val ttlColumn: Option[String] = parameters.get(SqlOptionTTLColumn)

/**
* redis key pattern for rows, based either on the 'keys.pattern' or 'table' parameter
Expand Down Expand Up @@ -106,6 +107,12 @@ class RedisSourceRelation(override val sqlContext: SQLContext,
s"You should only use either one.")
}

// check if both ttl column and ttl are set
if (ttlColumn.isDefined && ttl > 0) {
throw new IllegalArgumentException(s"Both options '$SqlOptionTTL' and '$SqlOptionTTLColumn' are set. " +
s"You should only use either one.")
}

override def schema: StructType = {
if (currentSchema == null) {
currentSchema = userSpecifiedSchema.getOrElse {
Expand Down Expand Up @@ -142,8 +149,9 @@ class RedisSourceRelation(override val sqlContext: SQLContext,
val conn = node.connect()
foreachWithPipeline(conn, keys) { (pipeline, key) =>
val row = rowsWithKey(key)
val encodedRow = persistence.encodeRow(keyName, row)
persistence.save(pipeline, key, encodedRow, ttl)
val encodedRow = persistence.encodeRow(keyName, row, ttlColumn)
val recordTTL = if (ttlColumn.isEmpty) ttl else row.getAs[Int](ttlColumn.get)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rewrite it with val recordTTL = ttlColumn.map(v => row.getAs[Int](v)).getOrElse(ttl). It looks to be more idiomatic approach.

persistence.save(pipeline, key, encodedRow, recordTTL)
}
conn.close()
}
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/org/apache/spark/sql/redis/redis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package object redis {
val SqlOptionInferSchema = "infer.schema"
val SqlOptionKeyColumn = "key.column"
val SqlOptionTTL = "ttl"
val SqlOptionTTLColumn = "ttl.column"

val SqlOptionMaxPipelineSize = "max.pipeline.size"
val SqlOptionScanCount = "scan.count"
Expand Down