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

时间:2018-06-04 12:19:19

标签: scala couchbase couchbase-java-api

我尝试使用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

此时,我被这个错误所震撼,我不知道如何修复它...我该如何解决?

1 个答案:

答案 0 :(得分: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()