Reputation: 127
I have a bucket 'A', with scope 'B' and collection 'C'. How can i retrieve all documents from this collection from couchbase?
import pandas as pd
from couchbase_core.cluster import PasswordAuthenticator
from couchbase.cluster import Cluster, ClusterOptions, QueryOptions
cluster = Cluster('couchbase://localhost', ClusterOptions(PasswordAuthenticator('xxx', 'xxx')))
cb = cluster.bucket("A")
cb_coll = cb.scope("B").collection("C")
How can i extract all documents from this collection 'C' using python and save it into dataframe?
Upvotes: 1
Views: 538
Reputation: 646
If you are looking for something like select *
from the collection,
you need to create a primary index on the collection.
CREATE PRIMARY INDEX ON `A`.B.C;
SELECT * FROM A.B.C;
The primary index can be created using the Couchbase web interface.
To perform these operations using the Python SDK, you can run the queries using the cluster object. Note that index creation is an asynchronous operation.
from couchbase.exceptions import QueryIndexAlreadyExistsException
# Create Primary Index for querying
try:
cluster.query("CREATE PRIMARY INDEX ON A.B.C")
except QueryIndexAlreadyExistsException:
print("Index already exists")
# Query all documents
result = cluster.query("SELECT * from A.B.C")
# Get all the documents
results = [row for row in result]
# Convert the results into a Pandas Dataframe
results_df = pd.DataFrame(results)
Upvotes: 4