| name | analytics-scala |
| summary | Couchbase Analytics Service (CBAS) for Scala — run OLAP queries, window functions, and external data links without impacting operational workloads (Server 6.x–7.x; use columnar-analytics for Server 8+) |
| description | Couchbase Analytics Service (CBAS) for Scala — run OLAP queries, window functions, and external data links without impacting operational workloads (Server 6.x–7.x; use columnar-analytics for Server 8+) |
| compatibility | Scala SDK 1.x. Requires couchbase-scala-client>=1.0. |
| metadata | {"last_verified":"2026-05","min_server_version":"6.0","deprecated_by":"columnar-analytics","deprecated_since":"8.0","max_server_version":"7.6","handoff":[{"condition":"user asks about SQL++ queries","skill":"server-querying-scala"},{"condition":"user is upgrading to Server 8.x and wants the columnar engine","skill":"columnar-analytics"},{"condition":"user wants to write results back to operational collections transactionally","type":"variant","skill":"transactions-scala"}]} |
Couchbase Analytics — Scala
Version disambiguation: This skill covers Couchbase Server 6.x–7.x (legacy CBAS). If the user mentions Server 8, Couchbase 8, or the columnar engine, route to columnar-analytics instead. If the server version is unknown, ask before proceeding.
Analytics runs OLAP queries on a shadow copy of your data, isolated from KV and Query Service workloads. Use it for aggregations, reports, and joins across large datasets.
Setup — Create an Analytics Collection
CREATE DATAVERSE myapp IF NOT EXISTS;
USE myapp;
CREATE ANALYTICS COLLECTION orders ON `myapp`.`_default`.`orders` IF NOT EXISTS;
Running Analytics Queries
The Scala SDK uses cluster.analyticsQuery() — the same API shape as cluster.query().
import com.couchbase.client.scala._
import com.couchbase.client.scala.analytics.AnalyticsOptions
val cluster = Cluster.connect("couchbase://localhost",
sys.env("CB_USERNAME"), sys.env("CB_PASSWORD")).get
// Simple aggregation
val result = cluster.analyticsQuery(
"SELECT country, COUNT(*) AS cnt FROM orders GROUP BY country ORDER BY cnt DESC"
).get
result.rowsAs[String].get.foreach(println)
Parameterized queries
val result = cluster.analyticsQuery(
"SELECT * FROM orders WHERE country = $country",
AnalyticsOptions().parameters(Map("country" -> "US"))
).get
Window functions
val result = cluster.analyticsQuery("""
SELECT name, country, stars,
RANK() OVER (PARTITION BY country ORDER BY stars DESC) AS rank
FROM hotels
""").get
Key differences from operational SQL++
| Analytics (analyticsQuery) | Operational (query) |
|---|
| Data source | Shadow copy (async replicated) | Live data |
| Isolation | No impact on KV/Query | Shares Query Service resources |
| Consistency | Eventually consistent | Configurable (request_plus) |
| Use case | OLAP, reports, large joins | OLTP, low-latency lookups |
Analytics vs Query Service: See shared/server/analytics-vs-query.md for the full comparison, setup steps, and common errors.
Writing Results Back
Use transactions for atomic write-back, or individual KV upserts for non-atomic bulk writes.
val result = cluster.analyticsQuery(
"""SELECT id, total FROM orders WHERE status = "complete""""
).get
val collection = cluster.bucket("myapp").defaultCollection()
result.rowsAs[JsonObject].get.foreach { row =>
collection.upsert(s"summary::${row.str("id")}",
JsonObject.create.put("total", row.dbl("total"))).get
}
For atomic multi-document write-back, wrap in a transaction — see transactions-scala.
Troubleshooting
| Error | Cause | Fix |
|---|
DataverseNotFoundException | Analytics collection not created | Run CREATE ANALYTICS COLLECTION |
CompilationException | SQL++ syntax error | Check field names and dataverse |
| Slow query | Missing index or large dataset | Add Analytics index or filter earlier |