scala中的Netflix Astyanax
我从你那里得到这个密码 我是scala新手,我的问题是将这个for循环转换为scala循环 你能帮忙吗scala中的Netflix Astyanax,scala,for-loop,Scala,For Loop,我从你那里得到这个密码 我是scala新手,我的问题是将这个for循环转换为scala循环 你能帮忙吗 result = keyspace .prepareQuery(CQL3_CF) .withCql("SELECT * FROM employees WHERE empId='111';") .execute(); for (Row<Integer, String> row : result.getResult().getRows(
result = keyspace
.prepareQuery(CQL3_CF)
.withCql("SELECT * FROM employees WHERE empId='111';")
.execute();
for (Row<Integer, String> row : result.getResult().getRows()) {
LOG.info("CQL Key: " + row.getKey());
ColumnList<String> 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));
}
result=keyspace
.准备工作(CQL3\u CF)
.withCql(“从empId='111';的员工中选择*”)
.execute();
for(行:result.getResult().getRows()){
LOG.info(“CQL键:+row.getKey());
ColumnList 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));
}
非常感谢将此代码转换为基于scala的循环构造的一种方法如下:
import scala.collection.JavaConversions._
val result = keyspace
.prepareQuery(CQL3_CF)
.withCql("SELECT * FROM employees WHERE empId='111';")
.execute();
result.getResult().getRows() foreach { row =>
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))
}
通过在JavaConversions中导入。我们可以访问隐式转换,该转换将Java Iterable(Rows对象是)转换为scala.collection.Iterable
,从而允许您使用foreach
循环构造
现在,这段代码在语法上是合理的,但它不是真正好的Scala代码。由于循环本身不返回任何东西,所以它的功能不是很强。它还有一些处理空值的混乱逻辑,可能应该改用选项。使用map
将结果数据映射到一个简单的case类中,这是一个更实用的解决方案示例,如下所示:
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] => T)):Option[T] = {
func(cl) match{
case null => None
case nonnull => Some(nonnull)
}
}
val employees = result.getResult().getRows() map { row =>
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
}
可能有一种更优雅的方法来处理null-to
选项
转换(可能通过隐式转换),但这种方法同样有效。完成map
操作后,您将有一个scala.collection.Iterable
的Employee
实例,然后您可能会返回到UI进行显示 将此代码转换为基于scala的循环构造的一种方法如下:
import scala.collection.JavaConversions._
val result = keyspace
.prepareQuery(CQL3_CF)
.withCql("SELECT * FROM employees WHERE empId='111';")
.execute();
result.getResult().getRows() foreach { row =>
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))
}
通过在JavaConversions中导入。我们可以访问隐式转换,该转换将Java Iterable(Rows对象是)转换为scala.collection.Iterable
,从而允许您使用foreach
循环构造
现在,这段代码在语法上是合理的,但它不是真正好的Scala代码。由于循环本身不返回任何东西,所以它的功能不是很强。它还有一些处理空值的混乱逻辑,可能应该改用选项。使用map
将结果数据映射到一个简单的case类中,这是一个更实用的解决方案示例,如下所示:
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] => T)):Option[T] = {
func(cl) match{
case null => None
case nonnull => Some(nonnull)
}
}
val employees = result.getResult().getRows() map { row =>
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
}
可能有一种更优雅的方法来处理null-to
选项
转换(可能通过隐式转换),但这种方法同样有效。完成map
操作后,您将有一个scala.collection.Iterable
的Employee
实例,然后您可能会返回到UI进行显示 您可以用选项(func(cl))替换选项的主体
。
您可以用选项(func(cl))替换选项的主体