Devi Devi - 2 months ago 36
Scala Question

How to write data into CouchBase using Spark & Scala?

I am newbie in CouchBase.I am trying to write data into CouchBase in local mode.My sample code is following,

val cfg = new SparkConf()
.setAppName("couchbaseQuickstart")
.setMaster("local[*]")
.set("com.couchbase.bucket.MyBucket","pwd")

val sc = new SparkContext(cfg)
val doc1 = JsonDocument.create("doc1", JsonObject.create().put("some","content"))
val doc2 = JsonArrayDocument.create("doc2", JsonArray.from("more", "content", "in", "here"))
val data = sc.parallelize(Seq(doc1, doc2))


But I can't access data.saveToCouchbase().

I am using Spark 1.6.1 & Scala 2.11.8

I gave following dependencies in built.sbt

libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "1.6.1"
libraryDependencies += "com.couchbase.client" % "spark-connector_2.11" % "1.2.1"


How can I write data into CouchBase using Spark & Scala?

Answer

Looks like you are just missing an import statement that'll enable you to use Couchbase functions on RDDs and dataframes:

import com.couchbase.spark._

val cfg = new SparkConf()
.setAppName("couchbaseQuickstart")
.setMaster("local[*]")
.set("com.couchbase.bucket.MyBucket","pwd") 

val sc = new SparkContext(cfg)
val doc1 = JsonDocument.create("doc1", 

JsonObject.create().put("some","content"))
val doc2 = JsonArrayDocument.create("doc2", JsonArray.from("more", "content", "in", "here"))

val data = sc.parallelize(Seq(doc1, doc2))

data.saveToCouchbase()