我有大json,我传递给Cosmos DB中的存储过程。我需要拆分json文档并创建多个文档。如何循环到存储过程中的json。
示例:使用下面的Json我需要为每个子节点创建一个单独的文档,另一个没有子节点。共3个文件。如何在存储过程中循环并拆分它?
{
"id": "d93af7d3706e4f28882920366c017cd7",
"FamilyId": 989,
"LastName": "Andersen",
"Parents": [
{
"FamilyName": null,
"FirstName": "Thomas"
},
{
"FamilyName": null,
"FirstName": "Mary Kay"
}
],
"Children": [
{
"ChildId":001,
"FamilyName": null,
"FirstName": "Henriette Thaulow",
"Gender": "female",
"Grade": 5,
"Pets": [
{
"GivenName": "Fluffy"
}
]
},
{
"ChildId":002
"FamilyName": null,
"FirstName": "Maria Thaulow",
"Gender": "female",
"Grade": 12,
"Pets": [
{
"GivenName": "Grizzly"
}
]
}
],
"Address": {
"State": "WA",
"County": "King",
"City": "Seattle"
},
"IsRegistered": false
}
答案 0 :(得分:0)
看一下这个样本:https://github.com/Azure/azure-documentdb-js-server/blob/master/samples/stored-procedures/BulkImport.js。请注意,当前存储过程可以在一个分区键的上下文中运行(在请求选项中提供以执行sproc),即如果文档中有多个PK值,则需要对文档进行分组通过PK值。
答案 1 :(得分:0)
实际上,您可以通过存储过程实现批量导入数据。
请参阅official tutorial中的代码。
function bulkImport(docs) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
// The count of imported docs, also used as current doc index.
var count = 0;
// Validate input.
if (!docs) throw new Error("The array is undefined or null.");
var docsLength = docs.length;
if (docsLength == 0) {
getContext().getResponse().setBody(0);
}
// Call the create API to create a document.
tryCreate(docs[count], callback);
// Note that there are 2 exit conditions:
// 1) The createDocument request was not accepted.
// In this case the callback will not be called, we just call setBody and we are done.
// 2) The callback was called docs.length times.
// In this case all documents were created and we don’t need to call tryCreate anymore. Just call setBody and we are done.
function tryCreate(doc, callback) {
var isAccepted = collection.createDocument(collectionLink, doc, callback);
// If the request was accepted, callback will be called.
// Otherwise report current count back to the client,
// which will call the script again with remaining set of docs.
if (!isAccepted) getContext().getResponse().setBody(count);
}
// This is called when collection.createDocument is done in order to process the result.
function callback(err, doc, options) {
if (err) throw err;
// One more document has been inserted, increment the count.
count++;
if (count >= docsLength) {
// If we created all documents, we are done. Just set the response.
getContext().getResponse().setBody(count);
} else {
// Create next document.
tryCreate(docs[count], callback);
}
}
}
当从客户端执行存储过程时,RequestOptions指定分区键,存储过程将在此分区的上下文中运行,并且无法在具有不同分区键值的文档上运行(例如,创建)。
您可以做的是从客户端为每个分区键执行存储过程。例如,如果存储过程是批量创建文档,则可以按分区键对文档进行分组,并将每个组(可以并行完成)发送到存储过程,在RequestOptions中提供分区键值。
希望它对你有所帮助。