如何在python中从Azure的CosmosDB中读取数据 [英] How to read data from Azure's CosmosDB in python

查看:259
本文介绍了如何在python中从Azure的CosmosDB中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure上有一个试用帐户,并且已将一些JSON文件上传到CosmosDB中.我正在创建一个python程序来查看数据,但是这样做很麻烦.这是我到目前为止的代码:

I have a trial account with Azure and have uploaded some JSON files into CosmosDB. I am creating a python program to review the data but I am having trouble doing so. This is the code I have so far:

import pydocumentdb.documents as documents
import pydocumentdb.document_client as document_client
import pydocumentdb.errors as errors

url = 'https://ronyazrak.documents.azure.com:443/'
key = '' # primary key

# Initialize the Python DocumentDB client
client = document_client.DocumentClient(url, {'masterKey': key})

collection_link = '/dbs/test1/colls/test1'

collection = client.ReadCollection(collection_link)

result_iterable = client.QueryDocuments(collection)

query = { 'query': 'SELECT * FROM server s' }

我在某处读到,该密钥将是我可以在我的Azure帐户中找到的主密钥按键.我已经用图片中显示的主键填充了键字符串,但是这里的键是空的,只是出于隐私目的.

I read somewhere that the key would be my primary key that I can find in my Azure account Keys. I have filled the key string with my primary key shown in the image but key here is empty just for privacy purposes.

如果在我的数据位于集合"test1"中,我还读到某个地方collection_link应该为"/dbs/test1/colls/test1".

I also read somewhere that the collection_link should be '/dbs/test1/colls/test1' if my data is in collection 'test1' Collections.

我的代码在函数client.ReadCollection()处出错.

My code gets an error at the function client.ReadCollection().

那是我有"pydocumentdb.errors.HTTPFailure:状态代码:401的错误 {代码":未经授权",消息":输入授权令牌无法处理请求.请检查是否已根据协议构建了预期的有效负载,并检查了所使用的密钥.服务器使用了以下有效负载签名:'get \ ncolls \ ndbs/test1/colls/test1 \ nmon,2017年5月29日19:47:28 gmt \ n \ n'\ r \ nActivityId:03e13e74-8db4-4661-837a-f8d81a2804cc}"

That's the error I have "pydocumentdb.errors.HTTPFailure: Status code: 401 {"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ncolls\ndbs/test1/colls/test1\nmon, 29 may 2017 19:47:28 gmt\n\n'\r\nActivityId: 03e13e74-8db4-4661-837a-f8d81a2804cc"}"

一旦该错误得到解决,剩下要做的是什么?我想将JSON文件作为一个大词典来获取,以便查看数据.

Once this error is fixed, what is there left to do? I want to get the JSON files as a big dictionary so that I can review the data.

我在正确的道路上吗?我是否以错误的方式处理此问题?如何阅读数据库中的文档?谢谢.

Am I in the right path? Am I approaching this the wrong way? How can I read documents that are in my database? Thanks.

推荐答案

根据您的错误信息,这似乎是由于您的密钥身份验证失败所致,因为下面的

According to your error information, it seems to be caused by the authentication failed with your key as the offical explaination said below from here.

因此,请检查您的密钥,但我认为关键点是错误地使用了pydocumentdb. DatabaseCollection&中的这些id Document与它们的链接不同.这些API ReadCollectionQueryDocuments需要通过传递相关链接.您需要通过资源链接而不是资源ID来检索Azure CosmosDB中的所有资源.

So please check your key, but I think the key point is using pydocumentdb incorrectly. These id of Database, Collection & Document are different from their links. These APIs ReadCollection, QueryDocuments need to be pass related link. You need to retrieve all resource in Azure CosmosDB via resource link, not resource id.

根据您的描述,我认为您想在集合ID路径/dbs/test1/colls/test1下列出所有文档.作为参考,下面是我的示例代码.

According to your description, I think you want to list all documents under the collection id path /dbs/test1/colls/test1. As reference, here is my sample code as below.

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']

docs = client.ReadDocuments(coll_link)
print list(docs)

请从此处.

这篇关于如何在python中从Azure的CosmosDB中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆