Analytics Queries

class couchbase.analytics.AnalyticsQuery[source]
__init__(querystr, *args, **kwargs)[source]

Create an Analytics Query object. This may be passed as the params argument to AnalyticsRequest.

Parameters
  • querystr – The query string to execute

  • args – Positional placeholder arguments. These satisfy the placeholder values for positional placeholders in the query string, demarcated by ?.

  • kwargs – Named placeholder arguments. These satisfy named placeholders in the query string, such as $name, $email and so on. For the placeholder values, omit the leading dollar sign ($).

Use positional parameters:

q = AnalyticsQuery("SELECT VALUE bw FROM breweries "
                   "bw WHERE bw.name = ?", 'Kona Brewing')

for row in cb.analytics_query(q, "127.0.0.1"):
    print('Got {}'.format(str(row))

Use named parameters:

q = AnalyticsQuery("SELECT VALUE bw FROM breweries "
                   "bw WHERE bw.name = $brewery", brewery='Kona Brewing')
for row in cb.analytics_query(q, "127.0.0.1"):
    print('Got {}'.format(str(row))

When using placeholders, ensure that the placeholder value is the unserialized (i.e. native) Python value, not the JSON serialized value. For example the query SELECT VALUE bw FROM breweries bw WHERE bw.name IN ['Kona Brewing','21st Amendment Brewery Cafe'] can be rewritten using placeholders:

Correct:

AnalyticsQuery('SELECT VALUE bw FROM breweries bw WHERE bw.name IN ?',
          ['Kona Brewing', '21st Amendment Brewery Cafe'])

Incorrect:

AnalyticsQuery('SELECT VALUE bw FROM breweries bw WHERE bw.name IN ?',
          "[\"Kona Brewing\",\"21st Amendment Brewery Cafe\"]")

Since the placeholders are serialized to JSON internally anyway.

class couchbase.analytics.AnalyticsResult[source]
__init__(*args, **kwargs)[source]

Object representing the execution of the request on the server.

Warning

You should typically not call this constructor by yourself, rather use the analytics_query() method (or one of its async derivatives).

Parameters

To actually receive results of the query, iterate over this object.