Select
Selects can retrieve the entire row, or only specific columns of the row. The query and the column specifier, if needed, are both represented via case classes.
In addition, you can choose to retrieve only a single row from Cassandra, represented by an Option[Row]
response,
where the Option will be None
if no values were found that matched the query.
Select Whole Row
For selects that will return the entire row:
scala> case class Query(s: String)
defined class Query
Retrieve all rows matching the query
scala> val selectStatement = sSession.selectStar("mytable", Query("a str"))
selectStatement: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str)
scala> selectStatement.execute()
res3: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
Retrieve a single row matching the query
scala> val selectOneStatement = sSession.selectOneStar("mytable", Query("a str"))
selectOneStatement: com.weather.scalacass.scsession.SCSelectOneStatement = SCSelectOneStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str LIMIT 1)
scala> selectOneStatement.execute()
res4: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[a str, 1234, 5678]))
Select Columns
For selects that only retrieve certain columns of that row, specify the columns as a case class. However, you will not actually use an instance of the case class in the statement, just pass it in as type parameter:
scala> case class ColumnsToRetrieve(s: String, l: Long)
defined class ColumnsToRetrieve
scala> val selectColumnsStatement = sSession.select[ColumnsToRetrieve]("mytable", Query("a str"))
selectColumnsStatement: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT s, l FROM mykeyspace.mytable WHERE s=a str)
scala> selectColumnsStatement.execute()
res5: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
scala> val selectColumnsOneStatement = sSession.selectOne[ColumnsToRetrieve]("mytable", Query("a str"))
selectColumnsOneStatement: com.weather.scalacass.scsession.SCSelectOneStatement = SCSelectOneStatement(SELECT s, l FROM mykeyspace.mytable WHERE s=a str LIMIT 1)
scala> selectColumnsOneStatement.execute()
res6: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[a str, 5678]))
Allow Filtering
You can ALLOW FILTERING
on the request (read more about “allow filtering” here)
scala> val selectAllowFiltering = selectStatement.allowFiltering
selectAllowFiltering: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str ALLOW FILTERING)
scala> selectAllowFiltering.execute()
res7: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
scala> val selectOneAllowFiltering = selectOneStatement.allowFiltering
selectOneAllowFiltering: com.weather.scalacass.scsession.SCSelectOneStatement = SCSelectOneStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str LIMIT 1 ALLOW FILTERING)
scala> selectOneAllowFiltering.execute()
res8: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[a str, 1234, 5678]))
You can remove the allow filtering option:
scala> val selectNoAllowFiltering = selectAllowFiltering.noAllowFiltering
selectNoAllowFiltering: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str)
scala> selectNoAllowFiltering.execute()
res9: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
scala> val selectOneNoAllowFiltering = selectOneAllowFiltering.noAllowFiltering
selectOneNoAllowFiltering: com.weather.scalacass.scsession.SCSelectOneStatement = SCSelectOneStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str LIMIT 1)
scala> selectOneNoAllowFiltering.execute()
res10: com.weather.scalacass.Result[Option[com.datastax.driver.core.Row]] = Right(Some(Row[a str, 1234, 5678]))
Limit
For queries that will return an iterator of responses (ie, not selectOne
statements), you can impose a limit on the
number of responses:
scala> val selectLimit = selectStatement.limit(100)
selectLimit: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str LIMIT 100)
scala> selectLimit.execute()
res11: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
Finally, you can disable the imposed limit:
scala> val selectNoLimit = selectLimit.noLimit
selectNoLimit: com.weather.scalacass.scsession.SCSelectItStatement = SCSelectItStatement(SELECT * FROM mykeyspace.mytable WHERE s=a str)
scala> selectNoLimit.execute()
res12: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
Reading from the Row
s
Scala-Cass provides a Scala-style method of extraction for Row
, either into Scala values, or directly into case
classes.
- Click here for a tutorial on how to extract values from
Row
- Click here for a mapping of Cassandra types to Scala types