Search code examples
scaladependenciescouchbasecouchbase-viewsql++

How to execute n1ql query of Couchbase in scala?


I want to execute a Nickel Query in my project and want to convert all the task that I am actually doing using view to perform it with Nickel. Is it possible..??

If yes please provide it with an example and for executing the Nickel queries in Scala,and do I have to add any additional dependencies in my project. If yes please provide the link too.

Any suggestions are appreciated


Solution

  • We are planning/working on a scala driver, but right now you can just use the java SDK through scala pretty easily. So I recommend you to follow the official docs, but convert the java examples to scala: http://docs.couchbase.com/developer/java-2.1/java-intro.html

    You want the java-client dependency first:

    libraryDependencies := "com.couchbase.client" % "java-client" % "2.1.4"
    

    If you use the implict conversions, you can make your life a little easier too:

    import com.couchbase.client.java.CouchbaseCluster
    import com.couchbase.client.java.query.Query
    import scala.collection.JavaConversions._
    
    object ConnectAndQuery {
    
      def main(args: Array[String]): Unit = {
        val cluster = CouchbaseCluster.create()
        val bucket = cluster.openBucket("travel-sample")
    
        val result = bucket.query(Query.simple("SELECT * FROM `travel-sample`LIMIT 10"))
        result.allRows().map(_.value()).foreach(println)
    
        cluster.disconnect()
      }
    
    }