Выберите данные из Azure Cosmos и выведите их в Python Script - PullRequest
0 голосов
/ 06 июля 2019

В моем Azure Cosmos существует база данных, коллекция.Проблема в том, что я застрял в том, как выводить данные запроса на выборку из Azure.Мне показываются ошибки 'TypeError:' QueryIterable 'объект не подписан на python'

Как вывести мой код из моего скрипта?

import pydocumentdb
import pydocumentdb.document_client as document_client
# Initialize the Python DocumentDB client
client = document_client.DocumentClient('https://domain.documents.azure.com:443/', {'masterKey': 'xxxxxxxxx'})
db_id = 'ppDB'
db_query = "select * from c where c.id = '{0}'".format(db_id)
db = list(client.QueryDatabases(db_query))[0]
db_link = db['_self']

coll_id = 'ppCollection'
coll_query = "select * from c where c.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']

c_id = '121ad45f-a278-3218-d2ba-63aaccd1fbab' 
# Query them in SQL
query = "SELECT * from c where c.id ".format(c_id)     

options = {} 
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2
res =client.QueryDocuments(coll_link, query, options)
print(res)

Ответы [ 2 ]

0 голосов
/ 08 июля 2019

Я получил результаты, изменив код следующим образом.

import pydocumentdb;
import pydocumentdb.document_client as document_client

 config = {
'ENDPOINT': 'https://dev.documents.azure.com:443/',
'MASTERKEY': 'xxxxxxxxxxxxxxxx',
'DOCUMENTDB_DATABASE': 'ppDb',
'DOCUMENTDB_COLLECTION': 'ppCollection'
};

# Initialize the Python DocumentDB client
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})
# use a SQL based query to get a bunch of documents
query = { 'query': 'SELECT * FROM server s where s._ts > 1562173751'}
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2

result_iterable = client.QueryDocuments('dbs/ppDB/colls/ppCollection', query, options)
results = list(result_iterable);
print(results)
0 голосов
/ 07 июля 2019

используйте функцию python iter (), например:

res = client.QueryDocuments(coll_link, query, options)

for doc in iter(res):
    print(doc['id'])  #etc.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...