0

我正在尝试使用 couchbase 异步存储桶 n1ql 查询,如下所示:

我在下面有一个可以运行的非编译代码示例:(注意我必须使用scala:2.11.7)

https://scastie.scala-lang.org/nkWOuCORRbKbzieEK2D8yA

couchbaseBucket.async().query(N1qlQuery.simple(s"SELECT META(${couchbaseBucket.name()}).id FROM diy WHERE META(${couchbaseBucket.name()}).id LIKE MyKeysPrefix%;"))
  .flatMap(_.rows()) // missing parameter type for expanded function ((x$12) => x$12.rows())
  .map(result => result.asInstanceOf[AsyncN1qlQueryRow].value().toMap)
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

我排成一排:.flatMap(_.rows())

缺少扩展函数的参数类型 ((x$12) => x$12.rows())

我尝试将其更新为:

.flatMap(rows => rows.rows())

然后我得到:

缺少参数类型

所以我尝试更新为:

.flatMap((rows: AsyncN1qlQueryResult) => rows.rows())

然后我得到:

具有替代方法的重载方法值 flatMap:

overloaded method value flatMap with alternatives:
[error]   [U, R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x$2: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R], x$3: Int)rx.Observable[R] <and>
[error]   [U, R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x$2: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R])rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x$3: rx.functions.Func0[_ <: rx.Observable[_ <: R]], x$4: Int)rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x$3: rx.functions.Func0[_ <: rx.Observable[_ <: R]])rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x$2: Int)rx.Observable[R] <and>
[error]   [R](x$1: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]])rx.Observable[R]
[error]  cannot be applied to (com.couchbase.client.java.query.AsyncN1qlQueryResult => rx.Observable[com.couchbase.client.java.query.AsyncN1qlQueryRow])
[error]       .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
[error]        ^
[error] one error found

在这一点上,我被这个错误震惊了,我不知道如何修复它......我该如何修复它?

4

1 回答 1

2

尝试

new MockCouchbaseServer()
  .createClient().async()
  .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
  .flatMap(new rx.functions.Func1[AsyncN1qlQueryResult, rx.Observable[AsyncN1qlQueryRow]] {
    override def call(rows: AsyncN1qlQueryResult): rx.Observable[AsyncN1qlQueryRow] = rows.rows()
  })
  .map[java.util.Map[String, Object]](new rx.functions.Func1[AsyncN1qlQueryRow, java.util.Map[String, Object]] {
    override def call(result: AsyncN1qlQueryRow): util.Map[String, Object] = result.value().toMap
  })
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

此代码与您在 Scastie 的导入和以下代码一起编译build.sbt

scalaVersion := "2.11.7"

libraryDependencies += "com.couchbase.client" % "java-client" % "2.5.8"
libraryDependencies += "com.couchbase.mock" % "CouchbaseMock" % "1.5.19"

或者,您可以手动定义隐式转换,这简化了代码:

implicit def convert[T, R](f: T => R): rx.functions.Func1[T, R] = (t: T) => f(t)

def main(args: Array[String]): Unit = {
  new MockCouchbaseServer()
    .createClient().async()
    .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
    .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
    .map[java.util.Map[String, Object]]((result: AsyncN1qlQueryRow) => result.value().toMap)
    .toList
    .timeout(10, TimeUnit.SECONDS)
    .toBlocking
    .single()
}

在 Scala 2.12 中,这样写就足够了:

new MockCouchbaseServer()
  .createClient().async()
  .query(N1qlQuery.simple("SELECT META(somebucket).id FROM diy WHERE META(somebucket).id LIKE MyKeyPrefix%;"))
  .flatMap((rows: AsyncN1qlQueryResult) => rows.rows())
  .map[java.util.Map[String, Object]](result => result.asInstanceOf[AsyncN1qlQueryRow].value().toMap)
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()
于 2018-06-06T19:54:52.733 回答