Skip to content

Commit edaed49

Browse files
committed
Simplified TTL & Writetime logic - No internal commands (ttl or writetimes) on the select-query.
TTL & Writetime properties are optional
1 parent d069490 commit edaed49

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/main/java/datastax/astra/migrate/AbstractJobSession.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public abstract class AbstractJobSession {
2626
protected final RateLimiter writeLimiter;
2727
public Logger logger = LoggerFactory.getLogger(this.getClass().getName());
2828
protected PreparedStatement sourceSelectStatement;
29-
protected String sourceSelectCondition;
3029
protected PreparedStatement astraSelectStatement;
3130
protected PreparedStatement astraInsertStatement;
3231
protected Integer maxRetries = 10;
@@ -73,25 +72,26 @@ protected AbstractJobSession(CqlSession sourceSession, CqlSession astraSession,
7372

7473
isPreserveTTLWritetime = Boolean.parseBoolean(sparkConf.get("spark.preserveTTLWriteTime", "false"));
7574
if (isPreserveTTLWritetime) {
76-
String ttlColsStr = sparkConf.get("spark.source.ttl.cols");
75+
String ttlColsStr = sparkConf.get("spark.preserveTTLWriteTime.ttl.cols");
7776
if (null != ttlColsStr && ttlColsStr.trim().length() > 0) {
7877
for (String ttlCol : ttlColsStr.split(",")) {
7978
ttlCols.add(Integer.parseInt(ttlCol));
8079
}
8180
}
81+
82+
String writeTimestampColsStr = sparkConf.get("spark.preserveTTLWriteTime.writetime.cols");
83+
if (null != writeTimestampColsStr && writeTimestampColsStr.trim().length() > 0) {
84+
for (String writeTimeStampCol : writeTimestampColsStr.split(",")) {
85+
writeTimeStampCols.add(Integer.parseInt(writeTimeStampCol));
86+
}
87+
}
8288
}
8389

8490
writeTimeStampFilter = Boolean
8591
.parseBoolean(sparkConf.get("spark.source.writeTimeStampFilter", "false"));
8692
// batchsize set to 1 if there is a writeFilter
8793
if (writeTimeStampFilter) {
8894
batchSize = 1;
89-
String writeTimestampColsStr = sparkConf.get("spark.source.writeTimeStampFilter.cols");
90-
if (null != writeTimestampColsStr && writeTimestampColsStr.trim().length() > 0) {
91-
for (String writeTimeStampCol : writeTimestampColsStr.split(",")) {
92-
writeTimeStampCols.add(Integer.parseInt(writeTimeStampCol));
93-
}
94-
}
9595
}
9696

9797
String minWriteTimeStampFilterStr =
@@ -118,13 +118,27 @@ protected AbstractJobSession(CqlSession sourceSession, CqlSession astraSession,
118118

119119
String selectCols = sparkConf.get("spark.query.source");
120120
String partionKey = sparkConf.get("spark.query.source.partitionKey");
121+
String sourceSelectCondition = sparkConf.get("spark.query.condition", "");
122+
123+
124+
final StringBuilder selectTTLWriteTimeCols = new StringBuilder();
125+
if (isPreserveTTLWritetime) {
126+
String[] allCols = selectCols.split(",");
127+
ttlCols.forEach(col -> {
128+
selectTTLWriteTimeCols.append(",ttl(" + allCols[col] + ")");
129+
});
130+
writeTimeStampCols.forEach(col -> {
131+
selectTTLWriteTimeCols.append(",writetime(" + allCols[col] + ")");
132+
});
133+
}
134+
String fullSelectQuery = "select " + selectCols + selectTTLWriteTimeCols.toString() + " from " + sourceKeyspaceTable + " where token(" + partionKey.trim()
135+
+ ") >= ? and token(" + partionKey.trim() + ") <= ? " + sourceSelectCondition + " ALLOW FILTERING";
136+
sourceSelectStatement = sourceSession.prepare(fullSelectQuery);
137+
logger.info("PARAM -- Query used: " + fullSelectQuery);
138+
121139
selectColTypes = getTypes(sparkConf.get("spark.query.types"));
122140
String idCols = sparkConf.get("spark.query.destination.id", "");
123141
idColTypes = selectColTypes.subList(0, idCols.split(",").length);
124-
sourceSelectCondition = sparkConf.get("spark.query.condition", "");
125-
sourceSelectStatement = sourceSession.prepare(
126-
"select " + selectCols + " from " + sourceKeyspaceTable + " where token(" + partionKey.trim()
127-
+ ") >= ? and token(" + partionKey.trim() + ") <= ? " + sourceSelectCondition + " ALLOW FILTERING");
128142

129143
String insertCols = sparkConf.get("spark.query.destination", "");
130144
if (null == insertCols || insertCols.trim().isEmpty()) {
@@ -182,15 +196,15 @@ public List<MigrateDataType> getTypes(String types) {
182196
public int getLargestTTL(Row sourceRow) {
183197
int ttl = 0;
184198
for (Integer ttlCol : ttlCols) {
185-
ttl = Math.max(ttl, sourceRow.getInt(ttlCol));
199+
ttl = Math.max(ttl, sourceRow.getInt(selectColTypes.size() + ttlCol - 1));
186200
}
187201
return ttl;
188202
}
189203

190204
public long getLargestWriteTimeStamp(Row sourceRow) {
191205
long writeTimestamp = 0;
192206
for (Integer writeTimeStampCol : writeTimeStampCols) {
193-
writeTimestamp = Math.max(writeTimestamp, sourceRow.getLong(writeTimeStampCol));
207+
writeTimestamp = Math.max(writeTimestamp, sourceRow.getLong(selectColTypes.size() + ttlCols.size() + writeTimeStampCol - 1));
194208
}
195209
return writeTimestamp;
196210
}

src/resources/sparkConf.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ spark.batchSize 5
2222
spark.coveragePercent 100
2323
spark.printStatsAfter 100000
2424

25-
spark.query.source partition-key,clustering-key,order-date,amount,writetime(order-date),writetime(amount),ttl(order-date),ttl(amount)
25+
spark.query.source partition-key,clustering-key,order-date,amount
2626
spark.query.source.partitionKey partition-key
2727
spark.query.destination partition-key,clustering-key,order-date,amount
2828
spark.query.destination.id partition-key,clustering-key
@@ -33,10 +33,10 @@ spark.counterTable.cql
3333
spark.counterTable.cql.index 0
3434

3535
spark.preserveTTLWriteTime true
36-
spark.source.ttl.cols 6,7
36+
spark.preserveTTLWriteTime.ttl.cols 2,3
37+
spark.preserveTTLWriteTime.writetime.cols 2,3
3738

3839
spark.source.writeTimeStampFilter false
39-
spark.source.writeTimeStampFilter.cols 4,5
4040
spark.source.minWriteTimeStampFilter 0
4141
spark.source.maxWriteTimeStampFilter 9223372036854775807
4242

0 commit comments

Comments
 (0)