| name | analytics-ruby |
| summary | Couchbase Analytics Service (CBAS) for Ruby — 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 Ruby — 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 | Ruby SDK 3.x. Requires couchbase>=3.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-ruby"},{"condition":"user is upgrading to Server 8.x and wants the columnar engine","skill":"columnar-analytics"}]} |
Couchbase Analytics — Ruby
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 Ruby SDK uses cluster.analytics_query().
require 'couchbase'
cluster = Couchbase::Cluster.connect(
'couchbase://localhost',
Couchbase::Options::Cluster.new(
authenticator: Couchbase::PasswordAuthenticator.new(
ENV['CB_USERNAME'], ENV['CB_PASSWORD']
)
)
)
result = cluster.analytics_query(
'SELECT country, COUNT(*) AS cnt FROM orders GROUP BY country ORDER BY cnt DESC'
)
result.rows.each { |row| puts row }
Parameterized queries
result = cluster.analytics_query(
'SELECT * FROM orders WHERE country = $country',
Couchbase::Options::Analytics.new(
named_parameters: { country: 'US' }
)
)
Window functions
result = cluster.analytics_query(<<~SQL)
SELECT name, country, stars,
RANK() OVER (PARTITION BY country ORDER BY stars DESC) AS rank
FROM hotels
SQL
Key differences from operational SQL++
| Analytics (analytics_query) | 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
Ruby SDK 3.x does not support distributed ACID transactions, so there is no transactional write-back path from analytics results to the operational store.
To write analytics results back, use individual KV upsert operations:
result = cluster.analytics_query('SELECT id, total FROM orders WHERE status = "complete"')
collection = cluster.bucket('myapp').default_collection
result.rows.each do |row|
collection.upsert("summary::#{row['id']}", { total: row['total'], synced_at: Time.now.iso8601 })
end
For concurrent writes, see sdk-patterns-ruby for bulk upsert patterns.
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 |