Ejemplo n.º 1
0
        private static async Task DeleteRoute()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];
            string route  = cliArgs[3];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosQueryUtil util = new CosmosQueryUtil(cosmosClient, config.IsVerbose());
            await util.SetCurrentDatabase(dbname);

            await util.SetCurrentContainer(cname);

            string sql = $"select c.id, c.pk from c where c.route = '{route}'";

            QueryResponse respObj = await util.ExecuteQuery(sql);

            string jstr    = respObj.ToJson();
            string outfile = "out/delete_route.json";
            await File.WriteAllTextAsync(outfile, jstr);

            Console.WriteLine($"file written: {outfile}");

            for (int i = 0; i < respObj.itemCount; i++)
            {
                dynamic         item = respObj.items[i];
                string          id   = item["id"];
                string          pk   = item["pk"];
                GenericDocument gd   = new GenericDocument(id, pk);
                Console.WriteLine($"deleting {gd.ToJson()}");
                ItemResponse <GenericDocument> resp = await util.DeleteGenericDocument(gd);

                Console.WriteLine($"resp, status: {resp.StatusCode} ru: {resp.RequestCharge}");
            }
        }
Ejemplo n.º 2
0
        private static async Task DeleteDatabase()
        {
            string dbname = cliArgs[1];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util       = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            int             statusCode = await util.DeleteDatabase(dbname);

            Console.WriteLine($"DeleteDatabase {dbname} -> statusCode {statusCode}");  // 204 expected
        }
Ejemplo n.º 3
0
        private static async Task DeleteContainer()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util       = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            int             statusCode = await util.DeleteContainer(dbname, cname);

            Console.WriteLine($"DeleteContainer {dbname} {cname} -> statusCode {statusCode}");
        }
Ejemplo n.º 4
0
        private static async Task UpdateDatabaseThroughput()
        {
            string dbname   = cliArgs[1];
            int    sharedRu = Int32.Parse(cliArgs[2]);

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util       = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            int             statusCode = await util.UpdateDatabaseThroughput(dbname, sharedRu);

            Console.WriteLine($"UpdateDatabaseThroughput {dbname} {sharedRu} -> statusCode {statusCode}");
        }
Ejemplo n.º 5
0
        private static async Task UpdateContainerIndexing()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];
            string infile = cliArgs[3];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            // int statusCode = await util.UpdateContainerIndexing(dbname, cname, infile);
            // Console.WriteLine($"UpdateContainerThroughput {dbname} {cname} {ru} -> statusCode {statusCode}");
            await Task.Delay(0);
        }
Ejemplo n.º 6
0
        private static async Task UpdateContainerThroughput()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];
            int    ru     = Int32.Parse(cliArgs[3]);

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util       = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            int             statusCode = await util.UpdateContainerThroughput(dbname, cname, ru);

            Console.WriteLine($"UpdateContainerThroughput {dbname} {cname} {ru} -> statusCode {statusCode}");
        }
Ejemplo n.º 7
0
        private static async Task CreateDatabase()
        {
            string dbname   = cliArgs[1];
            int    sharedRu = Int32.Parse(cliArgs[2]);

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil  util = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            DatabaseResponse resp = await util.CreateDatabase(dbname, sharedRu);

            Console.WriteLine(
                $"CreateDatabase {dbname} {sharedRu} -> status code {resp.StatusCode}, RU {resp.RequestCharge}");
        }
Ejemplo n.º 8
0
        private static async Task CreateContainer()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];
            string pk     = cliArgs[3];
            int    ru     = Int32.Parse(cliArgs[4]);

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            string          id   = await util.CreateContainer(dbname, cname, pk, ru);

            Console.WriteLine($"CreateContainer {dbname} {cname} {ru} -> Id {id}");
        }
Ejemplo n.º 9
0
        private static async Task ExecuteQueries()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];
            string infile = cliArgs[3];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosQueryUtil util = new CosmosQueryUtil(cosmosClient, config.IsVerbose());
            await util.SetCurrentDatabase(dbname);

            await util.SetCurrentContainer(cname);

            // Console.WriteLine("warming sdk client...");
            for (int i = 0; i < 2; i++)
            {
                await util.CountDocuments("");
            }

            foreach (string line in File.ReadLines(infile))
            {
                string[] tokens = line.Split("|");
                if (tokens.Length > 1)
                {
                    string qname = tokens[0].Trim();
                    string sql   = tokens[1].Trim();
                    if (qname.StartsWith("q"))
                    {
                        Console.WriteLine("");
                        Console.WriteLine("================================================================================");
                        Console.WriteLine($"executing qname: {qname}, db: {dbname}, cname: {cname}, sql: {sql}");
                        QueryResponse respObj = await util.ExecuteQuery(sql);

                        respObj.queryName = qname;
                        respObj.sql       = sql;
                        respObj.dbname    = dbname;
                        respObj.cname     = cname;
                        respObj.Finish();
                        Console.WriteLine(respObj.ToString());
                        string jstr = respObj.ToJson();
                        if (config.IsVerbose())
                        {
                            Console.WriteLine(jstr);
                        }
                        await File.WriteAllTextAsync(respObj.filename, jstr);

                        Console.WriteLine($"file written: {respObj.filename}");
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private static async Task CountDocuments()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosQueryUtil util = new CosmosQueryUtil(cosmosClient, config.IsVerbose());
            await util.SetCurrentDatabase(dbname);

            await util.SetCurrentContainer(cname);

            int count = (await util.CountDocuments("")).items[0];

            Console.WriteLine($"CountDocuments {dbname} {cname} -> {count}");
        }
Ejemplo n.º 11
0
        private static async Task TruncateContainer()
        {
            string dbname = cliArgs[1];
            string cname  = cliArgs[2];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            await util.SetCurrentDatabase(dbname);

            await util.SetCurrentContainer(cname);

            int deleteOperations = await util.TruncateContainer();

            Console.WriteLine($"TruncateContainer {dbname} {cname} -> deleteOperations {deleteOperations}");
        }
Ejemplo n.º 12
0
        private static async Task ListContainers()
        {
            string dbname = cliArgs[1];

            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util          = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            List <string>   containerList = await util.ListContainers(dbname);

            Console.WriteLine($"ListContainers - count {containerList.Count}");
            for (int i = 0; i < containerList.Count; i++)
            {
                string cname = containerList[i];
                Console.WriteLine($"container in db: {dbname} -> {cname}");
            }
        }
Ejemplo n.º 13
0
        private static async Task ListDatabases()
        {
            cosmosClient = CosmosClientFactory.RegularClient();
            CosmosAdminUtil util   = new CosmosAdminUtil(cosmosClient, config.IsVerbose());
            List <string>   dbList = await util.ListDatabases();

            Console.WriteLine($"ListDatabases - count {dbList.Count}");
            for (int i = 0; i < dbList.Count; i++)
            {
                string   dbname = dbList[i];
                Database db     = await util.GetDatabase(dbname);

                int?currentRU = await db.ReadThroughputAsync();

                if (currentRU == null)
                {
                    Console.WriteLine($"database {i+1}: {dbname} non-shared");
                }
                else
                {
                    Console.WriteLine($"database {i+1}: {dbname} current RU {currentRU}");
                }
            }
        }
Ejemplo n.º 14
0
        private static async Task BulkLoadContainer()
        {
            string dbname        = null;
            string cname         = null;
            string infile        = null;
            int    maxBatchCount = 0;
            int    batchSize     = 500;
            int    batchCount    = 0;
            int    taskCount     = 0;
            long   startEpoch    = 0;
            int    exceptions    = 0;

            try {
                dbname        = cliArgs[1];
                cname         = cliArgs[2];
                infile        = cliArgs[3];
                maxBatchCount = Int32.Parse(cliArgs[4]);

                Console.WriteLine($"dbname:   {dbname}");
                Console.WriteLine($"cname:    {cname}");
                Console.WriteLine($"infile:   {infile}");
                Console.WriteLine($"mbc:      {maxBatchCount}");

                cosmosClient = CosmosClientFactory.BulkLoadingClient();
                Database    database  = cosmosClient.GetDatabase(dbname);
                Container   container = database.GetContainer(cname);
                List <Task> tasks     = new List <Task>(batchSize);
                Console.WriteLine(
                    $"LoadContainer - db: {dbname}, container: {cname}, infile: {infile}, maxBatchCount: {maxBatchCount}");
                startEpoch = EpochMsTime();

                using (FileStream fs = File.Open(infile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    using (BufferedStream bs = new BufferedStream(fs)) {
                        using (StreamReader sr = new StreamReader(bs)) {
                            string jsonLine;
                            while ((jsonLine = sr.ReadLine()) != null)
                            {
                                if (batchCount < maxBatchCount)
                                {
                                    // Parse the JSON line into a "dynamic" object, like a python dict
                                    dynamic jsonDoc = JsonConvert.DeserializeObject <ExpandoObject>(
                                        jsonLine.Trim(), new ExpandoObjectConverter());
                                    // jsonDoc is an instance of class System.Dynamic.ExpandoObject (dynamic)

                                    // Set some additional attributes on the dynamic jsonDoc
                                    DateTimeOffset now = DateTimeOffset.UtcNow;
                                    jsonDoc.doc_epoch = now.ToUnixTimeMilliseconds();
                                    jsonDoc.doc_time  = now.ToString("yyyy/MM/dd-HH:mm:ss");
                                    jsonDoc.id        = Guid.NewGuid().ToString();

                                    // Add an UpsertItemAsync task to the list of tasks for a batch
                                    tasks.Add(container.UpsertItemAsync(jsonDoc, new PartitionKey(jsonDoc.pk)));
                                    taskCount++;

                                    if (tasks.Count == batchSize)
                                    {
                                        // Display the last jsonDoc in this batch
                                        Console.WriteLine(JsonConvert.SerializeObject(jsonDoc));

                                        batchCount++;
                                        Console.WriteLine($"writing batch {batchCount} ({tasks.Count}) at {EpochMsTime()}");

                                        // execute all of the Tasks in this batch, and await them
                                        await Task.WhenAll(tasks);

                                        // reset the tasks list to an empty list for the next batch
                                        tasks = new List <Task>(batchSize);
                                    }
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
                if (tasks.Count > 0)
                {
                    // execute the last batch if there are any tasks
                    batchCount++;
                    Console.WriteLine($"writing batch {batchCount} ({tasks.Count}) at {EpochMsTime()}");
                    await Task.WhenAll(tasks);
                }
            }
            catch (Exception e) {
                Console.WriteLine($"EXCEPTION: {e.Message}");
                exceptions++;
                throw;
            }
            finally {
                long   finishEpoch = EpochMsTime();
                long   elapsedMs   = finishEpoch - startEpoch;
                double elapsedSec  = (double)elapsedMs / (double)1000;
                double elapsedMin  = elapsedSec / (double)60.0;
                double docsPerSec  = taskCount / elapsedSec;
                Console.WriteLine("");
                Console.WriteLine("EOJ Totals:");
                Console.WriteLine($"  Database:             {dbname}");
                Console.WriteLine($"  Container:            {cname}");
                Console.WriteLine($"  Input Filename:       {infile}");
                Console.WriteLine($"  Max Batch Count:      {maxBatchCount}");
                Console.WriteLine($"  BulkLoad startEpoch:  {startEpoch}");
                Console.WriteLine($"  BulkLoad finishEpoch: {finishEpoch}");
                Console.WriteLine($"  BulkLoad elapsedMs:   {elapsedMs}");
                Console.WriteLine($"  BulkLoad elapsedSec:  {elapsedSec}");
                Console.WriteLine($"  BulkLoad elapsedMin:  {elapsedMin}");
                Console.WriteLine($"  Batch Size:           {batchSize}");
                Console.WriteLine($"  Batch Count:          {batchCount}");
                Console.WriteLine($"  Exceptions:           {exceptions}");
                Console.WriteLine($"  Document/Task count:  {taskCount}");
                Console.WriteLine($"  Document per Second:  {docsPerSec}");
                Console.WriteLine("");
            }
            await Task.Delay(0);
        }