如何在天蓝色的宇宙数据库中创建/更新大量记录

时间:2020-06-13 11:38:11

标签: javascript azure rest azure-cosmosdb

我在azure宇宙数据库中有1500条记录要创建,我只是使用Javascript代码进行遍历。我具有与数据库的REST API连接。我将要更新的数据作为JSON数组提供。

问题是当我传递整个数据,数据库突然超时或发送ECONNECTIONRESET时。你们中的许多人将在数据库中创建大量记录,并且可能有一些有效的方法来克服此问题。我要你的建议。

由于找不到大多数记录,因此很多时候创建新记录的部分都被命中,而我之前从未推送过如此大量的数据。任何建议或新想法都会对我有帮助。

注意:我使用mocha单元测试运行此javascript代码

下面是代码段

Record.js

const fetch = require('node-fetch');
let connectionAzureDataBase = "abc...";
let SubscriptionKey = "xyz";
let promises = [];
let j = -1;

module.exports = {
checkRecord
}
function checkRecord (req) {
 for (let i = 0; i < req.body.length; i++) {
    promises[j] = new Promise(async function(resolve, reject) {
      //check if record exist in azure
      var apiUrl = APICheckRecord( req.body[i].recordName);
            fetch(apiUrl , { headers:connectionAzureDataBase})
            .then(res => res.json())
            .then(record => {
              if(record) {
                console.log("Record Found");
              } else {
                console.log("Record not Found, calling API to create Record");
                var apiUrl = APICreateNewRecord( req.body[i].recordName);
                fetch(apiUrl , { headers:connectionAzureDataBase})
                .then(res => res.json())
                .then(recordCreated => {
                  if(recordCreated) {
                   console.log("record created successfully");
                   resolve("record created successfully");
                  } else {
                   console.log("Encountered some unexpected condition");
                   resolve("Encountered some unexpected condition");
                  }
                 }).catch(err => {
                     console.log("record could not be created");
                      resolve("record could not be created");
                 }) 
              }
             }).catch(err => {
                console.log("record not found");
                resolve("record not found");
             })
     })// close promise

 }// close for

 let replies = await Promise.all(promises); 
 let promise1 = new Promise (function(resolve,reject) {
   resolve(replies);
 }) 
}


Record.spec.js

const Records = require("Record.js);

 it("should find/create records", async function() {
        this.timeout(6000000);
        try { 
         let req =[
          {
            "recordName": "Xyz",
            "recordDate": "12-06-2020"
          },
          {
            "recordName": "Abc",
            "recordDate": "13-06-2020"
           }
          ]
          let reply = await Records.checkRecord(req);
          console.log(JSON.stringify(reply));

        } catch(err) {
            console.log(err);
        }     
    })

错误

  message: 'request to https://apim-dev.azure-api.net/api/portal/records/?recordName="Xyz" failed, reason: read ECONNRESET',
  type: 'system',
  errno: 'ECONNRESET',
  code: 'ECONNRESET' }

message: 'request to https://apim-dev.azure-api.net/api/portal/createRecords/ failed, reason: read ECONNRESET',
  type: 'system',
  errno: 'ECONNRESET',
  code: 'ECONNRESET' }

这是传递的数据示例,我有1500条这样的记录 SampleData

[
 {
   "recordName": "Xyz",
    "recordDate": "12-06-2020"
 },
{
   "recordName": "Abc",
    "recordDate": "13-06-2020"
 }
]

1 个答案:

答案 0 :(得分:1)

您可以使用Azure Cosmos DB Data Migration tool,它可以将来自各种来源的数据导入Azure Cosmos容器。您可以从JSON文件导入。您可以将该数据迁移到集合以与Azure Cosmos DB一起使用。从SQL API从单个分区集合迁移到多分区集合时,也可以使用数据迁移工具。

导入JSON文件

通过JSON文件源导入程序选项,您可以导入一个或多个单个文档JSON文件或每个都包含一组JSON文档的JSON文件。添加具有要导入的JSON文件的文件夹时,可以选择在子文件夹中递归搜索文件。