javacouchbasecouchbase-java-apicouchbase-analytics

Couchbase Analytics Java SDK connection creation + Security Roles


I am using the Couchbase Java SDK to query a couchbase analytics service. The process is covered in this tutorial: https://docs.couchbase.com/java-sdk/2.7/analytics-using-sdk.html

The Java SDK provides a Bucket object as the means of accessing couchbase. However a bucket is a separate entity from the analytics data-set. For example, my bucket is called data, and I have an analytics data-set that I want to query called requests.

I cannot find a means of connecting to just the requests data-set. The SDK will only connect to the data bucket. From there I can query the requests data-set by writing some N1QL. This work-around means that the user credentials I'm using to run analytics queries must also have access to my main production data bucket, which I'd rather prevent.

Is there a way to connect to simply the analytics data-set using the SDK?

The code I have currently creating the connection looks like this:

public class CouchbaseConfig {

  @Bean
  public Bucket bucket(CouchbaseProperties properties) {
    return cluster().openBucket("data"); // Changing this to the data-set name returns error
  }

  private Cluster cluster() {
    Cluster cluster = CouchbaseCluster.create("localhost");
    cluster.authenticate("Administrator", "password");
    return cluster;
  }

}

Using the requests data-set name in the bucket name results in this error:

Failed to instantiate [com.couchbase.client.java.Bucket]: Factory method 'bucket' threw exception; nested exception is com.couchbase.client.java.error.BucketDoesNotExistException: Bucket "requests" does not exist.

Using the data bucket name, but authentication username / password "analytics-reader" / "password" (with only Analytics Reader) roles results in this error:

Could not load bucket configuration: FAILURE({"message":"Forbidden. User needs one of the following permissions","permissions":["cluster.bucket[data].settings!read"]})

The only work around I have found is to give the analytics-reader user 'Application Access' to the data` bucket 😢


Solution

  • Connecting directly to analytics is possible with SDK3 and Couchbase 6.5 . In all the previous versions (SDK 2.7 included), the only way to query analytics is to connect to a bucket first.