我正在编写基于https://github.com/websudos/phantom#partial-select-queries中描述的“大型记录集的异步迭代器”的代码
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import com.anomaly42.aml.dao.CassandraConnector
import com.websudos.phantom.CassandraTable
import com.websudos.phantom.Implicits._
object People extends People {
def getPersonByUpdatedAt(from:String, to:String, start: Int, limit: Int) = {
val dtf:DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
val fromDateTime = dtf.parseDateTime(from)
val toDateTime = dtf.parseDateTime(to)
People.select(_.updated_at, _.firstName).allowFiltering.where(_.updated_at gte fromDateTime).and(_.updated_at lte toDateTime).fetchEnumerator().slice(start, limit).collect
}
}我使用了以下库依赖关系:
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"com.websudos" %% "phantom-dsl" % "1.5.4",
many more...
)但是我在编译的时候遇到了以下错误:
value slice is not a member of play.api.libs.iteratee.Enumerator[(org.joda.time.DateTime, Option[String])]我想要做的是编写一个查询,每次调用getPersonByUpdatedAt()方法时,都会返回从'start‘开始的下一个'limit’结果数。
发布于 2015-04-08 09:04:44
这里有相当多的实现细节需要解决。首先,如果您正在进行分页,那么使用简单的范围查询而不是过滤数据可能会有一种更容易的方法来实现这一点。
看一下如何使用CLUSTERING ORDER,对ALLOW FILTERING的调用不应该出现在那里。此外,如果没有CLUSTERING ORDER,默认的Murmur3分区程序实际上不是有序的,因此您无法保证以编写数据的相同顺序检索数据。
这很可能意味着您的分页根本不会起作用。最后但并非最不重要的一点是,直接使用枚举器可能不是您想要的。
它们是异步的,所以你必须在未来中映射才能获得切片,但除此之外,当像Spark这样的东西一次加载整个表时,枚举数是有用的,例如许多许多结果。
总而言之,在people表中:
object id extends UUIDColumn(this) with PartitionKey[UUID]// doesn't have to be UUID
object start extends DateTimeColumn(this) with ClusteringOrder[DateTime] with Ascending
object end extends DateTimeColumn(this) with ClusteringOrder[DateTime] with Ascending只需使用Scala集合库中的fetch()和Seq.slice即可。上面假设你想要按升序分页,例如首先检索最旧的。
如果2个用户同时更新,最坏的情况是你丢失数据,并以FIFO队列结束,例如,在给定时间的最后一次更新“获胜”。
而且,您很可能需要有几个表来存储人员,以便能够涵盖所需的所有查询。
发布于 2015-07-23 20:18:34
你应该使用Play框架中的Iteratee和Enumerator。在您的情况下,您需要:
import com.websudos.phantom.iteratee.Iteratee
val enumerator = People.select(_.updated_at, _.firstName).allowFiltering.where(_.updated_at gte fromDateTime).and(_.updated_at lte toDateTime).fetchEnumerator
val iteratee = Iteratee.slice[PeopleCaseClass](start, limit)
enumerator.run( iteratee ).map( _.foldLeft( List.empty[PeopleCaseClass] )( (l,e) => { e :: l } ))希望这能有所帮助
https://stackoverflow.com/questions/28967030
复制相似问题