通过cosmos仿真器批量插入数据到cosmos db

问题描述 投票:0回答:1

嗨,我有一个像这样创建的示例存储过程

Bulk insert

function bulkImport(items) {
    var container = getContext().getCollection();
    var containerLink = container.getSelfLink();

var items=[{ 
"UserAccountID":"1236", 
"FirstName": "Sanjeev", 
},{ 
"UserAccountID":"1235", 
"FirstName": "Sanjeev",  
}];

    // The count of imported items, also used as current item index.
    var count = 0;

    // Validate input.
    if (!items) throw new Error("The array is undefined or null.");

    var itemsLength = items.length;
    if (itemsLength == 0) {
        getContext().getResponse().setBody(0);
    }

    // Call the create API to create an item.
    tryCreate(items[count], callback);

    function tryCreate(item, callback) {
        var isAccepted = container.createDocument(containerLink, item, callback);

        if (!isAccepted) getContext().getResponse().setBody(count);
    }


    function callback(err, item, options) {
        if (err) throw err;

        // One more item has been inserted, increment the count.
        count++;

        if (count >= itemsLength) {
            // If we created all items, we are done. Just set the response.
            getContext().getResponse().setBody(count);
        } else {
            // Create next document.
            tryCreate(items[count], callback);
        }
    }
}

如何在cosmos仿真器中执行此sp。如何为每个项目唯一设置分区键。

由于分区键应该是唯一的,因此在执行时不能提供单个分区键。

stored-procedures azure-cosmosdb
1个回答
0
投票

首先,如果您不需要,可以在cosmos db中创建非分区集合。门户网站有一些限制,但sdk允许它。你可以参考我之前的案例:Is it still a good idea to create comos db collection without partition key?

其次,存储过程的批量导入不适用于分区集合,因为您需要为每次执行存储过程提供pk。请参考此链接:Azure Cosmos DB asking for partition key for stored procedure

最后,还有许多其他解决方案可以将数据导入cosmos db,例如Azure Cosmos DB Data migration tool,值得为您尝试。

© www.soinside.com 2019 - 2024. All rights reserved.