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.

set_option(name, value)

Set a raw option in the query. This option is encoded as part of the query parameters without any client-side verification. Use this for settings not directly exposed by the Python client.

Parameters:
  • name – The name of the option
  • value – The value of the option
consistent_with(state)

Indicate that the query should be consistent with one or more mutations.

Parameters:state (MutationState) – The state of the mutations it should be consistent with.
encoded

Get an encoded representation of the query.

This is used internally by the client, and can be useful to debug queries.

class couchbase.analytics.AnalyticsRequest[source]
__init__(params, host, parent)[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:
  • params – An AnalyticsQuery object.
  • host – the host to send the request to.
  • parent – The parent Bucket object

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

__iter__()
execute()

Execute the statement and raise an exception on failure.

This method is useful for statements which modify data or indexes, where the application does not need to extract any data, but merely determine success or failure.

meta
get_single_result()

Execute the statement and return its single result.

This should only be used on statements which are intended to return only a single result.

Returns:The single result, as encapsulated by the row_factory