gpt4 book ai didi

python - CosmosDB 和 Python3 : how to query?

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:43 24 4
gpt4 key购买 nike

我在我的项目中使用 CosmosDB (Azure documentDB),用 Python 3 编写。

我已经找了一段时间了,但我不知道如何查询我的表。我已经看到了一些示例代码,但没有看到如何查询的示例...我所能做的就是获取所有文档(当我的数据库 > 80GB 时并不理想)。

GitHub 存储库显示了一组非常小的数据库和集合操作:https://github.com/Azure/azure-documentdb-python/blob/master/samples/CollectionManagement/Program.py

还有following SO帖子展示了如何读取所有文档...但没有展示如何执行查询,例如“WHERE = X;”

如果有人能给我指出正确的方向,并可能提供一个示例来展示如何运行查询,我将非常感激。

最佳答案

根据我的理解,我认为您想知道如何使用Python执行类似SQL的查询来检索DocumentDB API的Azure CosmosDB上的文档,请引用下面来自here的代码.

A query is performed using SQL

# Query them in SQL
query = { 'query': 'SELECT * FROM server s' }

options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2

result_iterable = client.QueryDocuments(collection['_self'], query, options)
results = list(result_iterable);

print(results)

上面的代码使用的是QueryDocuments方法.

如有任何疑问,请随时告诉我。

<小时/>

更新:与您链接的其他 SO 线程的示例代码结合起来,如下所示。

from pydocumentdb import document_client

uri = 'https://ronyazrak.documents.azure.com:443/'
key = '<your-primary-key>'

client = document_client.DocumentClient(uri, {'masterKey': key})

db_id = 'test1'
db_query = "select * from r where r.id = '{0}'".format(db_id)
db = list(client.QueryDatabases(db_query))[0]
db_link = db['_self']

coll_id = 'test1'
coll_query = "select * from r where r.id = '{0}'".format(coll_id)
coll = list(client.QueryCollections(db_link, coll_query))[0]
coll_link = coll['_self']

query = { 'query': 'SELECT * FROM server s' }
docs = client.QueryDocuments(coll_link, query)
print list(docs)

关于python - CosmosDB 和 Python3 : how to query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44755311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com