Java API

In addition to the primary Scala API, the connector provides convenience APIs when accessed from Java.

Couchbase from the SparkContext

To use the Java API in spark, you need to initialize a JavaSparkContext:

SparkConf conf = new SparkConf()
    .set("", "");

JavaSparkContext sc = new JavaSparkContext(conf);

Since Java doesn’t have the implicit imports like Scala, the connector provides a helper class to achieve similar functionality:

// The Couchbase-Enabled spark context
CouchbaseSparkContext csc = couchbaseContext(sc);

The context is a static import. In general you want to statically import the following:

import static com.couchbase.spark.japi.CouchbaseDocumentRDD.couchbaseDocumentRDD;
import static com.couchbase.spark.japi.CouchbaseSparkContext.couchbaseContext;

Now you can create RDDs through Key/Value, Views or N1QL:

// Load docs through K/V
List<JsonDocument> docs = csc
    .couchbaseGet(Arrays.asList("airline_10226", "airline_10748"))

// Perform a N1QL query
List<CouchbaseQueryRow> results = csc
    .couchbaseQuery(N1qlQuery.simple("SELECT * FROM `travel-sample` LIMIT 10"))


Mapping RDDs to Couchbase APIs

An RDD can be wrapped with the couchbaseRDD static method to expose all the functions available. So instead of fetching the documents right from the SparkContext it can also be done like this:

import static com.couchbase.spark.japi.CouchbaseRDD.couchbaseRDD;
JavaRDD<String> ids = sc.parallelize(Arrays.asList("airline_10226", "airline_10748"));
docs = couchbaseRDD(ids).couchbaseGet().collect();

The CouchbaseRDD exposes the following methods:

  • couchbaseGet: Fetch documents via their unique Document ID.

  • couchbaseSubdocLookup: Fetch fragments of a document.

  • couchbaseView: Query a Couchbase View.

  • couchbaseSpatialView: Query a Couchbase Spatial View.

  • couchbaseQuery: Perform a N1QL Query.

Using Couchbase SparkSQL from Java

Using SparkSQL from Java is possible because the Java API provides wrappers for both the DataFrameReader and DataFrameWriter APIs. All you need to do is wrap the ones that are returned by Spark and wrap them like in the following example to get access to all couchbase specific methods:

import static com.couchbase.spark.japi.CouchbaseDataFrameReader.couchbaseReader;

// Use SparkSQL from Java
SQLContext sql = new SQLContext(sc);

// Wrap the Reader and create the DataFrame from Couchbase
DataFrame airlines = couchbaseReader( EqualTo("type", "airline"));

// Print the number of airline
System.out.println("Number of Airlines: " + airlines.count());

Using Datasets with Couchbase

Since Datasets work with actual Java objects, first create one:


public class Airport implements Serializable {
    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

Next, you can convert a DataFrame to a Dataset through the .as() API in Spark 1.6:

Dataset<Airport> airports = couchbaseReader(
	.couchbase(new EqualTo("type", "airport"))
	.select(new Column("airportname").as("name"))

	List<Airport> allAirports = airports.collectAsList();

Writing to Couchbase

If you want to store Documents in Couchbase, use the couchbaseDocumentRDD method:

    sc.parallelize(Arrays.asList(JsonDocument.create("doc1", JsonObject.empty())))