Couchbase scala observable n1ql 查询结果缺少扩展函数的参数类型 ((x$12) => x$12.rows())

Couchbase scala observable n1ql query results with missing parameter type for expanded function ((x$12) => x$12.rows())

我正在尝试使用 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) => x.rows())
  .map(result => result.asInstanceOf[AsyncN1qlQueryRow].value().toMap)
  .toList
  .timeout(10, TimeUnit.SECONDS)
  .toBlocking
  .single()

我排在:.flatMap(_.rows())

missing parameter type for expanded function ((x) => x.rows())

我尝试将其更新为:

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

然后我得到:

missing parameter type

所以我尝试更新为:

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

然后我得到:

overloaded method value flatMap with alternatives:

overloaded method value flatMap with alternatives:
[error]   [U, R](x: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R], x: Int)rx.Observable[R] <and>
[error]   [U, R](x: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: U]], x: rx.functions.Func2[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ >: U, _ <: R])rx.Observable[R] <and>
[error]   [R](x: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x: rx.functions.Func0[_ <: rx.Observable[_ <: R]], x: Int)rx.Observable[R] <and>
[error]   [R](x: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x: rx.functions.Func1[_ >: Throwable, _ <: rx.Observable[_ <: R]], x: rx.functions.Func0[_ <: rx.Observable[_ <: R]])rx.Observable[R] <and>
[error]   [R](x: rx.functions.Func1[_ >: com.couchbase.client.java.query.AsyncN1qlQueryResult, _ <: rx.Observable[_ <: R]], x: Int)rx.Observable[R] <and>
[error]   [R](x: 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

此时,我被这个错误吓坏了,我不知道如何修复它...我该如何修复它?

尝试

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()