Azure Cosmos DB - 在 python 程序中使用查询资源管理器

Azure's CosmosDB - Use query explorer in python program

我有一堆 JSON 文件存储在 Azure 的 CosmosDB 数据库中。我还有一个读取 JSON 文件的 python 程序。我想 运行 来自 python

的 Azure 查询资源管理器查询
    SELECT VALUE Block 
      FROM c 
      JOIN Block IN c.radar50p01

到目前为止,我的 python 程序中的内容如下

    def getCosmosDBClient():
        # Initialize the Python DocumentDB client
        client = document_client.DocumentClient(Constants.URL, {'masterKey': Constants.KEY})
        return client

    def getCosmosDBColl_link():
        client = getCosmosDBClient()

        db_id = Constants.RADAR_DATABASE_NAME
        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 = Constants.RADAR_COLL_NAME
        coll_query = "select * from r where r.id = '{0}'".format(coll_id)
        coll = list(client.QueryCollections(db_link, coll_query))
        if coll:
            coll = coll[0]
        else:
            raise ValueError("Collection not found in database.")
        coll_link = coll['_self']

        docs = client.ReadDocuments(coll_link)
        return docs

那么有没有一种方法可以在 python 中使用上面的查询,这样我就可以得到我特别需要的东西?

谢谢。

如果您的查询在 Azure 门户的查询资源管理器中 运行 成功,您只需使用 client.QueryDocuments(collection_link, query) method to do your query, as the code below from 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)

希望对您有所帮助。如有任何疑问,请随时告诉我。