The project is based on numerous articles touching upon the topic of integration Cassandra Datastax driver with Scala. I was fed up copy-pasting those extra classes into all my projects. This is an open-source solution to that problem.
The integration is divided into two parts.
Minimal supported cassandra-driver-core
version is 3.2.0
.
Previous versions were based on guava 16.0.1
which was really difficult to use.
Project cassandra-scala
allows for easier cassandra operations providing with CQL
.
Add import com.github.atais.cassandra.ScalaIntegration._
to use it.
Example usage:
implicit protected val session: com.datastax.driver.core.Session
implicit protected val executor: java.util.concurrent.Executor
val selectCQL: ListenableFuture[PreparedStatement] = cql"SELECT * FROM $table WHERE key = ?"
val result: ListenableFuture[ResultSet] = execute(selectCQL, "my-key")
result.map(rs => rs.one())
.map(...)
Asynchronously create PreparedStatement
for later reuse.
cql"SELECT * FROM $table WHERE key = ?"
Asynchronously execute PreparedStatement
passing parameters.
ScalaIntegration.execute(cql"SELECT * FROM $table WHERE key = ?", "testValue")
Project listenable-future-scala
allows for Scala-like handling of ListenableFuture
.
Add import com.github.atais.listenablefuture.ListenableFutureScala._
to use it.
Futures.immediateFuture("abc")
.map(text => text.length)
.map(i => i + 1)
Futures.immediateFuture("abc")
.flatMap(text => Futures.immediateFuture(text.length))
.flatMap(i => Futures.immediateCancelledFuture())
for {
text <- Futures.immediateFuture("abc")
number <- Futures.immediateFuture(123)
} yield {
text + number
}
scala.concurrent.Future
uses PartialFunction
to handle recover
cases, however it is not possible with Guava
.
Instead one has to pass the Throwable
class as function parameter matching with handling function.
Futures.immediateFuture("abc")
.map(_ => throw new UnsupportedOperationException)
.recover[UnsupportedOperationException] {
_: UnsupportedOperationException => "it's fine"
}
Has to be used similarly to recover, however function T => ListenableFuture[A]
has to be provided.
Futures.immediateFuture("abc")
.map(_ => throw new UnsupportedOperationException)
.recoverWith[UnsupportedOperationException] {
_: UnsupportedOperationException => Futures.immediateFuture("it's fine")
}
Converts to scala.concurrent.Future
Futures.immediateFuture("abc")
.future
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.