2017-07-28 44 views
0

是否可以在特定索引處選擇文檔?DocumentDB在特定索引處選擇文檔

我有一個文檔導入過程,我從我的數據源(一次250個項目)中獲取一個項目頁面,然後將它們同時導入到DocumentDB中。假設我在將這些項插入到DocumentDB時出現錯誤,我不確定個別項或項失敗。 (我可以解決,但不想)。再次從頁面上插入所有項目會更容易。

我插入的項目有一個升序ID。因此,如果我查詢DocumentDB(按ID排序)並選擇位置(所有Id的頁數 - 頁面大小)的ID,我可以再次從該點開始導入。

我知道SKIP沒有實現,我想檢查是否有另一個選項?

+0

什麼阻止您知道哪個文檔失敗?正確處理異常應該很容易看出哪一個沒有進入數據庫 –

回答

0

您可以嘗試批量導入存儲過程。下面的sproc創建代碼是從Azure's github repo。此sproc將報告批次中創建的文檔數量,並在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); 
      } 
     } 
    } 
} 
相關問題