简体   繁体   中英

Bulk insert documentdb using tcp or stored procedure in c#

Here's my c# code

public static async Task<Database> GetDatabase(string databaseName)
{
        if (client.CreateDatabaseQuery().Where(db => db.Id ==
              databaseName).AsEnumerable().Any())
        {
            return client.CreateDatabaseQuery().Where(db => db.Id ==
               databaseName).AsEnumerable().FirstOrDefault();
        }
        return await client.CreateDatabaseAsync(new Database
        {
            Id = databaseName
        });
}

//check if collection already exists
public static async Task<DocumentCollection> GetCollection(Database database, string collName)
{
        if (client.CreateDocumentCollectionQuery
              (database.SelfLink).Where(coll => coll.Id ==
              collName).ToArray().Any())
        {
            return client.CreateDocumentCollectionQuery(database.SelfLink).
                  Where(coll => coll.Id ==
           collName).ToArray().FirstOrDefault();
        }
        return await client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection
        { Id = collName });
}

[Route("getHotelDetails")]
[HttpPost]
public HttpResponseMessage getHotelDetails(RootObj rootObj)
{
        var result = "";

        Database database = GetDatabase("sampledb").Result;

        DocumentCollection collection = GetCollection(database, "samplecollection").Result;          

        string convertListToJson = JsonConvert.SerializeObject(rootObj);   

        try
        {
            var url = "http://www.example.com";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json; encoding='utf-8'";
            request.Credentials = GetCredential();
            request.PreAuthenticate = true;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(convertListToJson);
                streamWriter.Flush();
            }
            var httpResponse = (HttpWebResponse)request.GetResponse();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            // Log exception and throw as for GET example above

            HttpResponseMessage resp = Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message.ToString());
            return resp;
        }

        RootObject obj = JsonConvert.DeserializeObject<RootObject>(result);
        HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, obj);
        return res;
}

private CredentialCache GetCredential()
{
        string url = @"http://www.example.com";
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential("xxx", "yyy"));
        return credentialCache;
}

And the model class for the parameter rootObj

public class HotelCriteria
{
    public string HotelCode { get; set; }
}

public class RoomRatePlans
{
    public HotelCriteria HotelCriteria { get; set; }
}

    public class RootObj
    {
        public string Version { get; set; }
        public string EchoToken { get; set; }
        public RoomRatePlans RoomRatePlans { get; set; }
    }

End result which I need is explained.

So initially i am calling an external API to get the hotel details and their inventory so here's a sample request and the response that i get back after calling the external API

Request as json:

{
   "Version": "1.2",
   "EchoToken": "879791878",
   "RoomRatePlans": {
  "HotelCriteria": {
     "HotelCode": "101920"
  }
  }
}

And the response that I get back

{
"HotelCriteria": {
    "HotelCode": "NONIDS",
    "HotelName": "TestThe Zuri Whitefield Bengaluru"
},
"RoomTypes": {
    "RoomTypeList": [
        {
            "InvTypeCode": "ZCR",
            "Name": "Zuri Club Room",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 66,
            "IsRoomActive": 1,
            "RoomDescription": ""
        },
        {
            "InvTypeCode": "ZRR",
            "Name": "Zuri Room",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 90,
            "IsRoomActive": 1,
            "RoomDescription": ""
        },
        {
            "InvTypeCode": "ZSR",
            "Name": "Zuri Suite Room",
            "BaseOccupancy": 2,
            "MaxOccupancy": 3,
            "Quantity": 4,
            "IsRoomActive": 1,
            "RoomDescription": ""
        }
    ]
},
"RatePlans": {
    "RatePlanList": [
        {
            "RatePlanCode": "B2C00001",
            "RatePlanCategory": "B2C",
            "RatePlanStatusType": 1,
            "RatePlanName": "Channel Rates",
            "Description": "Channel Rates",
            "InvTypeCode": "ZCR",
            "MealPlanCode": "CP",
            "MealPlanDesc": "Continental Plan",
            "Start": "2016-06-27",
            "End": "2017-03-31",
            "CurrencyCode": "INR"
        },
        {
            "RatePlanCode": "B2C00001",
            "RatePlanCategory": "B2C",
            "RatePlanStatusType": 1,
            "RatePlanName": "Channel Rates",
            "Description": "Channel Rates",
            "InvTypeCode": "ZRR",
            "MealPlanCode": "CP",
            "MealPlanDesc": "Continental Plan",
            "Start": "2016-06-27",
            "End": "2017-03-31",
            "CurrencyCode": "INR"
        },
        {
            "RatePlanCode": "B2C00001",
            "RatePlanCategory": "B2C",
            "RatePlanStatusType": 1,
            "RatePlanName": "Channel Rates",
            "Description": "Channel Rates",
            "InvTypeCode": "ZSR",
            "MealPlanCode": "CP",
            "MealPlanDesc": "Continental Plan",
            "Start": "2016-06-27",
            "End": "2017-03-31",
            "CurrencyCode": "INR"
        }
    ]
},
"Inclusions": {
    "InclusionList": [
        {
            "MealPlanCode": "CP",
            "MealPlanDesc": "Continental Plan"
        }
    ]
}
}

Model class for the response i get back

        public class HotelCriteria
    {
        public string HotelCode { get; set; }
        public string HotelName { get; set; }
    }

    public class RoomTypeList
    {
        public string InvTypeCode { get; set; }
        public string Name { get; set; }
        public int BaseOccupancy { get; set; }
        public int MaxOccupancy { get; set; }
        public int Quantity { get; set; }
        public int IsRoomActive { get; set; }
        public string RoomDescription { get; set; }
    }

    public class RoomTypes
    {
        public List<RoomTypeList> RoomTypeList { get; set; }
    }

    public class RatePlanList
    {
        public string RatePlanCode { get; set; }
        public string RatePlanCategory { get; set; }
        public int RatePlanStatusType { get; set; }
        public string RatePlanName { get; set; }
        public string Description { get; set; }
        public string InvTypeCode { get; set; }
        public string MealPlanCode { get; set; }
        public string MealPlanDesc { get; set; }
        public string Start { get; set; }
        public string End { get; set; }
        public string CurrencyCode { get; set; }
    }

    public class RatePlans
    {
        public List<RatePlanList> RatePlanList { get; set; }
    }

    public class InclusionList
    {
        public string MealPlanCode { get; set; }
        public string MealPlanDesc { get; set; }
    }

    public class Inclusions
    {
        public List<InclusionList> InclusionList { get; set; }
    }

    public class RootObject
    {
        public HotelCriteria HotelCriteria { get; set; }
        public RoomTypes RoomTypes { get; set; }
        public RatePlans RatePlans { get; set; }
        public Inclusions Inclusions { get; set; }

    }

So i have a list of 1500 hotels for which i will call the rest API to get each individual hotel and their inventory details.In turn i want each response to be saved in documentdb as a single document so in total i should have 1500 documents in my collection.So if i do a foreach and use createdocumentasync method will it be a right choice or i can bulk insert records after having all my 1500 document details in a list.Need your suggestions and help !

Thanks in advance !

your code is not the traditional ado.net but in my opinion I highly suggest you to use bulkinsert since this is designed for large scale insertion of files/data/documents. You just need to loop all 1500 documents before you insert at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM