ERROR: 'Slice not a member' when implementing Asynchronous-iterators in Phantom-DSL
ERROR: 'Slice not a member' when implementing Asynchronous-iterators in Phantom-DSL
我正在尝试遵循 wiki
中的异步迭代器示例
我收到以下错误:
value slice is not a member of 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
}
}
语法有点错误,当你想在枚举器上使用方法时,这就是你需要的:
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
方法,它会返回一个令牌,您需要将其存储在客户端。当用户想要获取 "the next page" 时,需要提供上一页的 pagingState
字符串,将分页状态视为指向 Cassandra table 中特定位置的指针,因此这样 Cassandra 就知道如何 "jump" 或 "skip pages".
所以你的下一个 API 调用,假设你从第 0 页开始,应该包含一个 pagingState
作为字符串。
然后您可以 PagingState.fromString(pagingState)
并传递此结果以获得 "next page"。
我会在phantom中添加一个这样的例子,但是这应该基本上解决了你现在的问题。
我正在尝试遵循 wiki
中的异步迭代器示例我收到以下错误:
value slice is not a member of 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
}
}
语法有点错误,当你想在枚举器上使用方法时,这就是你需要的:
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
方法,它会返回一个令牌,您需要将其存储在客户端。当用户想要获取 "the next page" 时,需要提供上一页的 pagingState
字符串,将分页状态视为指向 Cassandra table 中特定位置的指针,因此这样 Cassandra 就知道如何 "jump" 或 "skip pages".
所以你的下一个 API 调用,假设你从第 0 页开始,应该包含一个 pagingState
作为字符串。
然后您可以 PagingState.fromString(pagingState)
并传递此结果以获得 "next page"。
我会在phantom中添加一个这样的例子,但是这应该基本上解决了你现在的问题。