-
Notifications
You must be signed in to change notification settings - Fork 38
support converting null TTLs to default TTL relative to write timestamp #66
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
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ import org.apache.spark.sql.types.{ IntegerType, LongType, StructField, StructTy | |||||||||||||||||||||
import org.apache.spark.sql.{ DataFrame, Row, SparkSession } | ||||||||||||||||||||||
import org.apache.spark.unsafe.types.UTF8String | ||||||||||||||||||||||
|
||||||||||||||||||||||
import java.time.Instant | ||||||||||||||||||||||
import scala.collection.mutable.ArrayBuffer | ||||||||||||||||||||||
|
||||||||||||||||||||||
object Cassandra { | ||||||||||||||||||||||
|
@@ -92,7 +93,8 @@ object Cassandra { | |||||||||||||||||||||
def explodeRow(row: Row, | ||||||||||||||||||||||
schema: StructType, | ||||||||||||||||||||||
primaryKeyOrdinals: Map[String, Int], | ||||||||||||||||||||||
regularKeyOrdinals: Map[String, (Int, Int, Int)]) = | ||||||||||||||||||||||
regularKeyOrdinals: Map[String, (Int, Int, Int)], | ||||||||||||||||||||||
defaultTTL: Long) = | ||||||||||||||||||||||
if (regularKeyOrdinals.isEmpty) List(row) | ||||||||||||||||||||||
else { | ||||||||||||||||||||||
val rowTimestampsToFields = | ||||||||||||||||||||||
|
@@ -126,7 +128,17 @@ object Cassandra { | |||||||||||||||||||||
|
||||||||||||||||||||||
timestampsToFields | ||||||||||||||||||||||
.map { | ||||||||||||||||||||||
case ((ttl, writetime), fields) => | ||||||||||||||||||||||
case ((ttl, writetime), fields) => { | ||||||||||||||||||||||
val writetimestamp = writetime.getOrElse(CassandraOption.Unset) | ||||||||||||||||||||||
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. This looks suspicious because |
||||||||||||||||||||||
val baseTTL = if (defaultTTL > 0) defaultTTL else 0L | ||||||||||||||||||||||
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. I wonder if it would be clearer to keep the So, the parameter would have type |
||||||||||||||||||||||
val deltaTTL = if (writetimestamp == CassandraOption.Unset) { | ||||||||||||||||||||||
0 | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
baseTTL - (System.currentTimeMillis - writetimestamp.asInstanceOf[Long] / 1000) / 1000 | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+134
to
+138
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. I recommend to avoid using
Suggested change
|
||||||||||||||||||||||
// expire the record in 1s in case delta is negative, use given TTL if write timestamp wasn't found | ||||||||||||||||||||||
val finalttl = | ||||||||||||||||||||||
if (deltaTTL > 0) deltaTTL else if (deltaTTL == 0) baseTTL else 1L | ||||||||||||||||||||||
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. Here use assume that Maybe use the type
Suggested change
|
||||||||||||||||||||||
val newValues = schema.fields.map { field => | ||||||||||||||||||||||
primaryKeyOrdinals | ||||||||||||||||||||||
.get(field.name) | ||||||||||||||||||||||
|
@@ -135,9 +147,10 @@ object Cassandra { | |||||||||||||||||||||
else Some(row.get(ord)) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
.getOrElse(fields.getOrElse(field.name, CassandraOption.Unset)) | ||||||||||||||||||||||
} ++ Seq(ttl.getOrElse(0L), writetime.getOrElse(CassandraOption.Unset)) | ||||||||||||||||||||||
} ++ Seq(ttl.getOrElse(finalttl), writetimestamp) | ||||||||||||||||||||||
|
||||||||||||||||||||||
Row(newValues: _*) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -168,7 +181,8 @@ object Cassandra { | |||||||||||||||||||||
df: DataFrame, | ||||||||||||||||||||||
timestampColumns: Option[TimestampColumns], | ||||||||||||||||||||||
origSchema: StructType, | ||||||||||||||||||||||
tableDef: TableDef): DataFrame = | ||||||||||||||||||||||
tableDef: TableDef, | ||||||||||||||||||||||
defaultTTL: Long): DataFrame = | ||||||||||||||||||||||
timestampColumns match { | ||||||||||||||||||||||
case None => df | ||||||||||||||||||||||
case Some(TimestampColumns(ttl, writeTime)) => | ||||||||||||||||||||||
|
@@ -193,7 +207,8 @@ object Cassandra { | |||||||||||||||||||||
_, | ||||||||||||||||||||||
broadcastSchema.value, | ||||||||||||||||||||||
broadcastPrimaryKeyOrdinals.value, | ||||||||||||||||||||||
broadcastRegularKeyOrdinals.value) | ||||||||||||||||||||||
broadcastRegularKeyOrdinals.value, | ||||||||||||||||||||||
defaultTTL) | ||||||||||||||||||||||
}(RowEncoder(finalSchema)) | ||||||||||||||||||||||
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
@@ -269,7 +284,8 @@ object Cassandra { | |||||||||||||||||||||
spark.createDataFrame(rdd, selection.schema), | ||||||||||||||||||||||
selection.timestampColumns, | ||||||||||||||||||||||
origSchema, | ||||||||||||||||||||||
tableDef | ||||||||||||||||||||||
tableDef, | ||||||||||||||||||||||
source.defaultTTL.getOrElse(0L) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
SourceDataFrame(resultingDataframe, selection.timestampColumns, true) | ||||||||||||||||||||||
|
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.