簡體   English   中英

插入 Azure Cosmos DB 時分區鍵不起作用

[英]Partition key is not working while inserting in Azure Cosmos DB

我正在嘗試使用存儲過程在 Azure CosmosDB 集合中插入文檔。 早些時候我沒有使用分區鍵,這段代碼正在運行,但我知道對於刪除/更新,需要分區鍵,所以我也傳遞了一個分區鍵,但是在運行腳本時它給出了以下錯誤:

源自腳本的請求不能引用除提交客戶端請求的分區鍵之外的分區鍵。

這是代碼:

import azure.cosmos.cosmos_client as cosmos_client
import azure.cosmos.documents as documents
import pandas as pd
import numpy as np
import json
import time
config = {
        'ENDPOINT': 'PUT_YOUR_ENDPOINT',
        'PRIMARYKEY': 'PRIMARYKEY',
        'DATABASE': 'DB_NAME'
    }

# Initialize the Cosmos client
client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={
                                    'masterKey': config['PRIMARYKEY']})

try:
    db_id = config['DATABASE']
    db_query = "select * from r where r.id = '{0}'".format(db_id)
    db = list(client.QueryDatabases(db_query))[0]
    db_link = db['_self']
except Exception as e:
    db = client.CreateDatabase({'id': config['DATABASE']},options={"offerThroughput": 10000})
    db_link = db['_self']

def get_data_from_collection(filter, collection_id, only_country_level=False):
    coll_query = "select * from r where r.id = '{0}'".format(collection_id)
    coll = list(client.QueryContainers(db_link, coll_query))[0]
    #print(coll)
    coll_link = coll['_self']
    offer = list(client.QueryOffers('SELECT * FROM c WHERE c.resource = \'{0}\''.format(coll_link)))[0]
    offer['content']['offerThroughput'] = 10000
    offer = client.ReplaceOffer(offer['_self'], offer)
    print('Replaced Offer. Offer Throughput is now \'{0}\''.format(offer['content']['offerThroughput']))

    data = []
    query = 'select value f.data from collection f'
    docs = (client.QueryItems(coll_link,query,{'enableCrossPartitionQuery': True}))#(client.QueryItems(coll_link,query).fetch_next_block())
    for i in docs:
        #print("docs: ",(i))
        data.extend(np.array(i).flatten())

    offer = list(client.QueryOffers('SELECT * FROM c WHERE c.resource = \'{0}\''.format(coll_link)))[0]
    offer['content']['offerThroughput'] = 400
    offer = client.ReplaceOffer(offer['_self'], offer)
    print('Replaced Offer. Offer Throughput is now \'{0}\''.format(offer['content']['offerThroughput']))
    #print(len(np.array(data).flatten()))
    #print("flat data: ",(np.array(data).flatten()))
    return np.array(data).flatten()

def store_in_cosmosDB(collection_name, dataframe):
    # Create container options
    options = {
    'offerThroughput': 10000
    }

    container_definition = {'id': collection_name,
    'partitionKey':
                {
                    'paths': ['/Country'],
                    'kind': documents.PartitionKind.Hash
                }
    }

    try:
        # Create a container
        container = client.CreateContainer(db_link, container_definition, options)
        coll_link = container['_self']
    except Exception as e:
        coll_id = collection_name
        coll_query = "select * from r where r.id = '{0}'".format(coll_id)
        coll = list(client.QueryContainers(db_link, coll_query))[0]
        coll_link = coll['_self']
        offer = list(client.QueryOffers('SELECT * FROM c WHERE c.resource = \'{0}\''.format(coll_link)))[0]
        offer['content']['offerThroughput'] = 10000
        offer = client.ReplaceOffer(offer['_self'], offer)
        print('Replaced Offer. Offer Throughput is now \'{0}\''.format(offer['content']['offerThroughput']))

        query = """SELECT VALUE f._self FROM collection f"""
        #delete_bulk(query,client,coll_link)

    sproc = {
                'id': 'storedProcedure',
                'body': (
                    'function (data) {' +
                    'data = JSON.parse(data);' +
                    '   var client = getContext().getCollection();' +
                    # ' for(var i=0;i<data.length;i++){' +
                    '   client.createDocument(client.getSelfLink(), data, {}, function(err, docCreated, options) { ' +
                    '   if(err){ throw new Error(\'Error while creating document: \' + err.message);}' +
                    '   else {' +
                             '   getContext().getResponse().setBody(1);' +
                    '        }' +
                    '   });}')
            }

    try:
        # Create a container
        created_sproc = client.CreateStoredProcedure(coll_link, sproc)
        proc_link = created_sproc['_self']
    except Exception as e:
        proc_id = sproc['id']
        proc_query = "select * from r where r.id = '{0}'".format(proc_id)
        proc = list(client.QueryStoredProcedures(coll_link, proc_query))[0]
        proc_link = proc['_self']


    #dataframe.drop_duplicates(inplace=True)
    dataframe.fillna("(blank)",inplace=True)
    dataframe.replace([np.inf, -np.inf], "(blank)",inplace=True)
    df_list = np.array_split(dataframe, 50)
    size = 0
    start_time = time.time()
    # for split_df in df_list:
    #     json_temp = dict()
    #     json_temp['data'] = split_df.to_dict('records')
    #     size += len(split_df)
    #     st = str(json_temp).replace('\'','"')
    #     #print(st)
    client.ExecuteStoredProcedure(proc_link, json.dumps(dataframe.to_dict('records'), default=str), {'partitionKey':'India'})

    print("split size is ",size)
    # for item in df.to_dict('records'):
    #     client.CreateItem(container['_self'],item)


    print("--- %s seconds ---" % (time.time() - start_time))

    offer = list(client.QueryOffers('SELECT * FROM c WHERE c.resource = \'{0}\''.format(coll_link)))[0]
    offer['content']['offerThroughput'] = 400
    offer = client.ReplaceOffer(offer['_self'], offer)
    print('Replaced Offer. Offer Throughput is now \'{0}\''.format(offer['content']['offerThroughput']))

data = pd.DataFrame({'Country':['India','India','India'], 'num_Patients':[12,36,100]})

try:
    store_in_cosmosDB("dummy_data",data,None)
    print('Uploading Done!!!')

except Exception as e:
    raise e

您收到此錯誤的原因是因為您嘗試在存儲過程中操作的文檔中的PartitionKey值是一個具有['India','India','India']的數組,而當您執行存儲過程,為相同指定的值是India

值應該匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM