用Python删除所有数据中的Cosmos DB问题

2024-09-26 22:12:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用azure的cosmosdb和使用python的sqlapi。你知道吗

我已经上传了一些数据到cosmos数据库,但现在我想删除它。你知道吗

我在这个问题上看到了如何做:Cosmos DB - Delete Document with Python

但对我来说不起作用,这是我的代码:

config = {
    'ENDPOINT': "ENDPOINT",
    'MASTERKEY': 'key',
    'DOCUMENTDB_DATABASE': 'database',
    'DOCUMENTDB_COLLECTION': 'collection'
};

# 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 c' }

options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = -1
options['partitionKey'] = '2017'

result_iterable = client.QueryDocuments('dbs/database/colls/collection', query, options)

results = list(result_iterable);

print(results)

#client.DeleteDocument('dbs/ToDoList/colls/nc4data/docs/'+result_iterableID,options)
for x in range(0, len (results)):
    docID = results[x]['id']
    print (docID)
    client.DeleteDocument('dbs/database/colls/collection/docs/'+docID, options=options)
    print ('deleted', docID)
#print ('delete success')

我的分区密钥是年,从2016年到2019年。我所尝试的是作为其中一年的一个选项,正如我在上面链接的答案:

options['partitionKey'] = '2017'

我也尝试过这样做:

options['partitionKey'] = 2017 (year is string, but I was trying)
options['partitionKey'] = 'year'
options['partitionKey'] = '/year'

当我在选项字典中插入这个“partitionKey”时,我在结果中获得了一个empy列表。你知道吗

我也尝试过不使用这个'partitionKey',然后我得到了所有的数据,但是我得到了这个错误:

{\“Errors\”:[\“x-ms-partitionkey头中提供的分区键的组件少于集合中定义的组件。你知道吗


Tags: clientconfigresultqueryyeardatabaseresultsendpoint
1条回答
网友
1楼 · 发布于 2024-09-26 22:12:43

我在上一个线程中测试了代码:Cosmos DB - Delete Document with Python,它运行良好。你知道吗

import pydocumentdb;
import pydocumentdb.document_client as document_client

config = {
    'ENDPOINT': 'Your url',
    'MASTERKEY': 'Your master key',
    'DOCUMENTDB_DATABASE': 'familydb',
    'DOCUMENTDB_COLLECTION': 'familycoll'
};

# 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' }

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

result_iterable = client.QueryDocuments('dbs/familydb/colls/familycoll', query, options)

results = list(result_iterable);

print(results)

client.DeleteDocument('dbs/familydb/colls/familycoll/docs/id1',options)

print 'delete success'

我搜索了github的确切问题,发现它是由sdk版本引起的。您可以升级包版本或通过Fiddler监视https请求工具。请参考以下螺纹:

1.https://github.com/Azure/azure-sdk-for-ios/issues/108

2.https://github.com/Azure/azure-sdk-for-android/issues/75

3.https://github.com/Azure/azure-sdk-for-ios/pull/114

相关问题 更多 >

    热门问题