Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to convert this code into a scala based looping construct would be as follows:</p> <pre><code> import scala.collection.JavaConversions._ val result = keyspace .prepareQuery(CQL3_CF) .withCql("SELECT * FROM employees WHERE empId='111';") .execute(); result.getResult().getRows() foreach { row =&gt; LOG.info("CQL Key: " + row.getKey()) val columns = row.getColumns() LOG.info(" empid : " + columns.getIntegerValue("empid", null)) LOG.info(" deptid : " + columns.getIntegerValue("deptid", null)) LOG.info(" first_name : " + columns.getStringValue ("first_name", null)) LOG.info(" last_name : " + columns.getStringValue ("last_name", null)) } </code></pre> <p>By importing in JavaConversions._, we gain access to an implicit conversion that will convert a Java Iterable (which the Rows object is) to a <code>scala.collection.Iterable</code>, thus allowing you to use the <code>foreach</code> looping construct.</p> <p>Now, this code is syntactically sound, but it's not really good Scala code. It's not very functional in that the loop itself does not return anything. It also has some messy logic dealing with nulls that should probably use Options instead. A more functional example of the solution, using <code>map</code> to map the result data into a simple case class could look like this:</p> <pre><code>import scala.collection.JavaConversions._ val result = keyspace .prepareQuery(CQL3_CF) .withCql("SELECT * FROM employees WHERE empId='111';") .execute(); case class Employee(id:Option[Int], depId:Option[Int], firstName:Option[String], lastName:Option[String]) def optFor[T](cl:ColumnList[String], func:(ColumnList[String] =&gt; T)):Option[T] = { func(cl) match{ case null =&gt; None case nonnull =&gt; Some(nonnull) } } val employees = result.getResult().getRows() map { row =&gt; LOG.info("CQL Key: " + row.getKey()) val cl = row.getColumns() val employee = Employee(optFor(cl, _.getIntegerValue("empid", null)), optFor(cl, _.getIntegerValue("deptid", null)), optFor(cl, _.getStringValue("first_name", null)), optFor(cl, _.getStringValue("last_name", null))) LOG.info(employee) employee } </code></pre> <p>There's probably a more elegant way to handle the null to <code>Option</code> conversion (via implicits perhaps), but this works as well. When done with the <code>map</code> operation, you will have an <code>scala.collection.Iterable</code> of <code>Employee</code> instances that you can then then perhaps return out to the UI for display. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload