-
Notifications
You must be signed in to change notification settings - Fork 371
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can rewrite it with |
||
persistence.save(pipeline, key, encodedRow, recordTTL) | ||
} | ||
conn.close() | ||
} | ||
|
There was a problem hiding this comment.
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?