Cross Bucket Querying using Python SDK

Hi,
My couchbase version is - 4.5.0-2601 Community Edition (build-2601)
The Python SDK version is - 2.2.6
OS = Linux ubuntucorei3 3.13.0-32-generic

I have a requirement for querying two PASSWORD PROTECTED buckets.
I tried the following Python code, which did not work:

Python Code [as per link - https://issues.couchbase.com/browse/PYCBC-377]
from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery

import connectionModule (custom module)

try:
bucket1 = connectionModule.bucket1BucketObj()
bucket1.add_bucket_creds(‘bucket1’, ‘password1’)
bucket1.add_bucket_creds(‘bucket2’, ‘password2’)
bucket1.cross_bucket = True
bucket1.adhoc = False
bucket1.consistency = ‘not_bounded’

search_sql = \
'''
SELECT ...
'''
query = N1QLQuery(search_sql)
for row in bucket1.n1ql_query(query):
	print("bucket1Field = " + str(row['bucket1Field']))
	print("bucket2Field = " + str(row['bucket2Field']))

except Exception as e:
raise

The exception is
File “/usr/local/lib/python3.4/dist-packages/couchbase/n1ql.py”, line 388, in iter
raw_rows = self.raw.fetch(self._mres)
couchbase.exceptions.HTTPError: <RC=0x3B[HTTP Operation failed. Inspect status code for details],
HTTP Request failed. Examine ‘objextra’ for full result, Results=1,
C Source=(src/http.c,140), OBJ=ViewResult<rc=0x3B[HTTP Operation failed.
Inspect status code for details], value={‘requestID’: ‘076399c4-aa2b-4a50-8e42-951dfcc956e3’,
‘metrics’: {‘errorCount’: 1, ‘executionTime’: ‘8.999034ms’, ‘resultSize’: 0, ‘elapsedTime’: ‘9.065921ms’, ‘resultCount’: 0},
‘errors’: [{‘code’: 10000, ‘msg’: ‘Authorization Failed Keyspace bucket2’}], ‘results’: [],
‘status’: ‘stopped’}, http_status=401>>

Queries:

  1. Is cross bucket querying available as per my couchbase & Python SDK vesrsions?
  2. If YES, can you please help me fix the above sample code or point me to a working example.

Regards,
Sachin Vyas.

Hi @ssharma7884

I’m very sorry for the slow reply.

I believe cross-bucket queries should work and I think it may be a bug in the Python SDK. I have raised PYCBC-440 to track the issue.

@ssharma7884

Actually, just a I hit return on my previous reply, I got this to execute without error:

from couchbase.bucket import Bucket
from couchbase.n1ql import N1QLQuery

bucket = Bucket('couchbase://localhost/bucket-1', password='password')
bucket.add_bucket_creds('bucket-2', 'password')

query = N1QLQuery(
    "SELECT b1.name FROM `bucket-1` b1 "
    "INNER JOIN `bucket-2` b2 on KEYS b1.name "
)
query.cross_bucket = True

result = bucket.n1ql_query(query).execute()

Does that help?

MikeGoldsmith,
This did help & solved the issue.
The problem was with my code.
As per my sample code posted in my original post:

bucket1.cross_bucket = True

should have been

query.cross_bucket = True

Thanks.
Sachin Vyas.

Great, glad you got it working.