1

我正在尝试遵循wiki中的异步迭代器示例

我收到以下错误:

值切片不是 play.api.libs.iteratee.Enumerator 的成员

非常感谢您对问题的任何输入。


这样我就可以对大型集合的结果进行分页


libraryDependencies ++= Seq(
  "com.websudos" %% "phantom-dsl" % 1.22.0,
  "com.websudos" %% "phantom-reactivestreams" % 1.22.0 
)

import com.datastax.driver.core.{ResultSet, Row}
import com.websudos.phantom.CassandraTable
import com.websudos.phantom.dsl._
import com.websudos.phantom.iteratee.Iteratee
import org.dyne.danielsan.openblockchain.data.entity.Block

import scala.concurrent.Future
import com.websudos.phantom.reactivestreams._
import scala.concurrent.Await
import scala.concurrent.duration._

sealed class BlocksModel extends CassandraTable[BlocksModel, Block] {

  override def fromRow(row: Row): Block = {
    Block(
      hash(row),
      height(row)
}

  object hash extends StringColumn(this) with PartitionKey[String]

  object height extends IntColumn(this) with ClusteringOrder[Int] with Descending

  object order_id extends LongColumn(this) with ClusteringOrder[Long] with Descending

abstract class ConcreteBlocksModel extends BlocksModel with RootConnector {

  override val tableName = "blocks"

  def getBlocks(start: Int, limit: Int): Future[Set[Block]] = {
    select.fetchEnumerator.slice(start, limit).collect
  }
}
4

1 回答 1

3

语法稍有错误,当您想在枚举器上使用方法时,这就是您需要的:

abstract class ConcreteBlocksModel extends BlocksModel with RootConnector {

  override val tableName = "blocks"

  def getBlocks(start: Int, limit: Int): Future[Iterator[Block]] = {
    select.fetchEnumerator run Iterator.slice(start, limit)
  }
}

如果您想对记录进行分页,在 Cassandra 中可以通过自动分页来执行此操作。

def getPage(limit: Int, paging: Option[PagingState] = None): Future[ListResult[JodaRow]] = {
  select.limit(limit).fetchRecord(paging)
}

基本上,您需要为下一个要运行的查询提供分页状态。现在Future返回的将在其中包含一个ListResult项目,这意味着您将获得 2 个方法:

def records: List[R] // the list of records from the db, just like fetch()
def pagingState: PagingState // the state you care about.

基本上pagingState有一个toString方法会给你一个你需要存储在客户端的令牌。当用户想要获取“下一页”时,您需要提供pagingState上一页的字符串,将分页状态视为指向 Cassandra 表中特定位置的指针,这样 Cassandra 就知道如何“跳转”或“跳过页面”。

因此,假设您从第 0 页开始,您的下一个 API 调用应该包含 apagingState作为字符串。

然后,您可以PagingState.fromString(pagingState)将结果传递给“下一页”。

我将在幻像中添加一个示例,但这应该基本上可以解决您当前的问题。

于 2016-08-08T08:50:43.203 回答