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

标签 scala cassandra-2.0 phantom-dsl

我正在编写基于 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() 方法时,从“开始”开始返回下一个“限制”结果数。

最佳答案

这里有很多实现细节需要解决。首先,如果您在分页之后,可能有一种更简单的方法可以通过简单的范围查询而不是过滤数据来实现这一目标。

看看使用 CLUSTERING ORDER , 调用 ALLOW FILTERING不应该在那里。此外,没有 CLUSTERING ORDER默认的 Murmur3 分区器实际上并没有排序,因此您无法保证以您编写它的相同顺序检索数据。

这可能意味着您的分页根本不起作用。最后但同样重要的是,直接使用枚举器可能不是您所追求的。

它们是异步的,所以你必须在 future 中进行映射才能得到一个切片,但除此之外,当像 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

只需使用 fetch()然后 Seq.slice来自 Scala 集合库。以上假设您要按升序进行分页,例如首先检索最旧的。

您还需要弄清楚实际的分区键可能是什么。如果同时更新 2 个用户,最坏的情况是您丢失数据并以 FIFO 队列结束,例如在给定时间的最后一次更新“获胜”。我用了id上面,但这显然不是你需要的。

而且您可能需要有几个表来存储人员,这样您就可以覆盖所有需要的查询。

关于scala - 值切片不是 play.api.libs.iteratee.Enumerator 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967030/

相关文章:

cassandra - Cassandra 中没有回滚,那么 Cassandra 是如何删除失败的写入的呢?

scala - Spark 中有哪些不同的联接类型?

Scala Generic,通过变量指定泛型

cassandra-2.0 - 哈希模四的第一个字节的含义是什么

java - Cassandra java 驱动程序设置全局一致性级别

scala - 更改 phantom embedded Cassandra 的日志级别

Scala代码不获取s3文件

scala - 理解 scala 界限的困惑

scala - 如何更新已索引的字段?

scala - 数据库自动创建 -> 找不到参数 session 的隐式值