1

So i am calling this method which was in the sample code provided by microsoft for documentdb but i get a null response when trying to create a new stored procedure.

private static async Task RunBulkImport(string collectionLink)
    {


        string Datafilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data");
        string inputDirectory = Datafilepath;
        string inputFileMask = "*.json";
        int maxFiles = 2000;
        int maxScriptSize = 50000;

        // 1. Get the files.
        string[] fileNames = Directory.GetFiles(inputDirectory, inputFileMask);
        DirectoryInfo di = new DirectoryInfo(inputDirectory);
        FileInfo[] fileInfos = di.GetFiles(inputFileMask);

        // 2. Prepare for import.
        int currentCount = 0;
        int fileCount = maxFiles != 0 ? Math.Min(maxFiles, fileNames.Length) : fileNames.Length;

        // 3. Create stored procedure for this script.
        string procedurepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SP\BulkImport.js");
        string body = File.ReadAllText(procedurepath);
        StoredProcedure sproc = new StoredProcedure
        {
            Id = "BulkImport",
            Body = body
        };

        await TryDeleteStoredProcedure(collectionLink, sproc.Id);
        sproc = await client.CreateStoredProcedureAsync(collectionLink, sproc);

        while (currentCount < fileCount)
        {
            // 5. Create args for current batch.
            //    Note that we could send a string with serialized JSON and JSON.parse it on the script side,
            //    but that would cause script to run longer. Since script has timeout, unload the script as much
            //    as we can and do the parsing by client and framework. The script will get JavaScript objects.
            string argsJson = CreateBulkInsertScriptArguments(fileNames, currentCount, fileCount, maxScriptSize);
            var args = new dynamic[] { JsonConvert.DeserializeObject<dynamic>(argsJson) };

            // 6. execute the batch.
            StoredProcedureResponse<int> scriptResult = await client.ExecuteStoredProcedureAsync<int>(
                sproc.SelfLink,
                new RequestOptions { PartitionKey = new PartitionKey("Andersen") },
                args);

            // 7. Prepare for next batch.
            int currentlyInserted = scriptResult.Response;
            currentCount += currentlyInserted;
        }

        // 8. Validate
        int numDocs = 0;
        string continuation = string.Empty;
        do
        {
            // Read document feed and count the number of documents.
            FeedResponse<dynamic> response = await client.ReadDocumentFeedAsync(collectionLink, new FeedOptions { RequestContinuation = continuation });
            numDocs += response.Count;

            // Get the continuation so that we know when to stop.
            continuation = response.ResponseContinuation;
        }
        while (!string.IsNullOrEmpty(continuation));

        Console.WriteLine("Found {0} documents in the collection. There were originally {1} files in the Data directory\r\n", numDocs, fileCount);
    }


    private static async Task TryDeleteStoredProcedure(string collectionLink, string sprocId)
    {
        StoredProcedure sproc = client.CreateStoredProcedureQuery(collectionLink).Where(s => s.Id == sprocId).AsEnumerable().FirstOrDefault();
        if (sproc != null)
        {
            await client.DeleteStoredProcedureAsync(sproc.SelfLink);
        }
    }

    private static string CreateBulkInsertScriptArguments(string[] docFileNames, int currentIndex, int maxCount, int maxScriptSize)
    {
        var jsonDocumentArray = new StringBuilder();
        jsonDocumentArray.Append("[");

        if (currentIndex >= maxCount) return string.Empty;
        jsonDocumentArray.Append(File.ReadAllText(docFileNames[currentIndex]));

        int scriptCapacityRemaining = maxScriptSize;
        string separator = string.Empty;

        int i = 1;
        while (jsonDocumentArray.Length < scriptCapacityRemaining && (currentIndex + i) < maxCount)
        {
            jsonDocumentArray.Append(", " + File.ReadAllText(docFileNames[currentIndex + i]));
            i++;
        }

        jsonDocumentArray.Append("]");
        return jsonDocumentArray.ToString();
    }

And here is the BulkImport.js file

function bulkImport(docs) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();

   //count used as 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);
}

// CRUD API to create a document.
tryCreate(docs[count], callback);

function tryCreate(doc, callback) {
    var options = {
        disableAutomaticIdGeneration: true
    };

    var isAccepted = collection.createDocument(collectionLink, doc, options, callback);

    if (!isAccepted) getContext().getResponse().setBody(count);
}

function callback(err, doc, options) {
    if (err) throw err;
    count++;

    if (count >= docsLength) {
          getContext().getResponse().setBody(count);
    } else {

        tryCreate(docs[count], callback);
    }
}
}

And in the data folder i have 100 json files which was provided in the sample itself.Kindly help me to get create the new procedure i am using documentdb emulator.

Melvin
  • 877
  • 3
  • 11
  • 27

1 Answers1

0

I can create the new procedure with documentdb emulator. As I know it is not support to execute stored procedure with documentDB Emulator exploer currently. Following is my detail steps:

1.Download the documentdb-emulator and install it in the local machine

2.Download the document demo code from github.

3.We can get local documentdb account master key from the document, and add it to the project

The DocumentDB Emulator supports only a single fixed account and a well-known master key. Key regeneration is not possible in the DocumentDB Emulator

// Connect to the DocumentDB Emulator running locally

   DocumentClient client = new DocumentClient(new Uri("https://localhost:8081"),"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");

enter image description here

4.Open documentdb emulator to create db and collection

enter image description here

enter image description here

5.Execute the demo code and check the result from the azure emulator exploer.

enter image description here

enter image description here

Note: According to the screenshot, we can find that there no input or result field on the selected procedure tab with azure documentdb emulator.

If we use azure documentdb then we can use executed the procedure from the Azure portal with Azure documentdb.

enter image description here

If we have any issue about documentdb emulator we can give our feedback the azure documentdb team by click feedback on the documentdb emulator exploer.

enter image description here

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Thanks for that but is it possible to run the same using c# code if possible can you tell me why i am getting a null response while i use createstoredprocedure ?Also i see u have used partition may i know the correct time to use that ? – Melvin Mar 30 '17 at 06:21
  • Could you supply more info about how to call the mothod? RunDemoAsync(DatabaseName, CollectionName).Wait(); ? – Tom Sun - MSFT Mar 30 '17 at 06:25
  • I get latest demo code from the [github](https://github.com/Azure/azure-documentdb-dotnet). when create the collection and add the partition key as screenshot. **/Lastname** is the value in the demo code. – Tom Sun - MSFT Mar 30 '17 at 06:29