【问题标题】:Extract document from couchbase collection using python使用python从couchbase集合中提取文档
【发布时间】:2021-11-27 07:04:19
【问题描述】:

我有一个存储桶“A”,范围为“B”,集合“C”。如何从 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")

如何使用 python 从这个集合“C”中提取所有文档并将其保存到数据框中?

【问题讨论】:

    标签: python pandas couchbase


    【解决方案1】:

    如果您正在从集合中寻找类似select * 的内容, 您需要在集合上创建一个主索引。

    CREATE PRIMARY INDEX ON `A`.B.C;
    SELECT * FROM A.B.C;
    

    可以使用 Couchbase Web 界面创建主索引。

    要使用 Python SDK 执行这些操作,您可以使用集群对象运行查询。请注意,索引创建是一个异步操作。

    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)
    

    【讨论】:

    • 感谢您的回答
    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多