【问题标题】:DocumentDB select document at specific indexDocumentDB 在特定索引处选择文档
【发布时间】:2017-07-29 10:30:14
【问题描述】:

是否可以选择特定索引处的文档?

我有一个文档导入过程,我从我的数据源中获取一页项目(一次 250 个项目),然后将这些项目同时导入 DocumentDB。假设我将这些项目插入 DocumentDB 时出错,我将不确定哪些单个项目或哪些项目失败。 (我可以解决但不想)。再次 Upsert 页面中的所有项目会更容易。

我插入的项目有一个升序的 id。因此,如果我查询 DocumentDB(按 id 排序)并选择位置的 id(所有 Id 的计数 - 页面大小),我可以再次从该点开始导入。

我知道 SKIP 没有实现,我想检查是否有其他选项?

【问题讨论】:

  • 是什么让您不知道哪个文档失败了?正确处理异常应该可以非常容易地查看哪些异常没有进入数据库

标签: azure-cosmosdb


【解决方案1】:

您可以尝试批量导入存储过程。下面的 sproc 创建代码来自Azure's github repo。该 sproc 将报告在批处理中创建的文档数量,并在 sproc 超时​​时继续尝试在多个批处理中创建文档。

由于存储过程是 ACID,如果抛出任何异常,您将不得不从头(或最后一个成功的批次)重试。 如果您只想在抛出任何异常时重试整个批处理过程,也可以将 createDocument 函数更改为 upsertDocument。

{
    id: "bulkImportSproc",
    body: 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);
            return;
        }

        // Call the CRUD 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.
            // This condition will happen when this stored procedure has been running too long
            // and is about to get cancelled by the server. This will allow the calling client
            // to resume this batch from the point we got to before isAccepted was set to false
            if (!isAccepted) getContext().getResponse().setBody(count);
        }

        // This is called when collection.createDocument is done and the document has been persisted.
        function callback(err, doc, options) {
            if (err) throw err;

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2014-06-24
    • 2012-07-31
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多