public async Task InitializeAsync()
 {
     if (_container == null)
     {
         string partitionKey = $"/{_props.PartitionKey?.Name ?? "partkey"}";
         _container = await _database.CreateContainerIfNotExistsAsync(_name, partitionKey);
     }
 }
Пример #2
0
        private async Task InitAsync()
        {
            if (CosmosClient == null || Database == null || Containers == null || Containers.Count == 0)
            {
                CosmosClient = new CosmosClient(Endpoint, PrimaryKey);
                Database     = await CosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseId);

                foreach (KeyValuePair <string, string> entry in ContainerData)
                {
                    Container c = await Database.CreateContainerIfNotExistsAsync(new ContainerProperties(entry.Key, entry.Value));

                    Containers.Add(entry.Key, c);
                }
            }
        }
Пример #3
0
        public static async void GenerateData(
            string key,
            string dbName,
            string endpoint,
            string containerName,
            string partitionKey,
            int documentsCount,
            int batchSize)
        {
            var options = new CosmosClientOptions()
            {
                AllowBulkExecution = true
            };

            var cosmosClient = new CosmosClient(endpoint, key, options);

            Microsoft.Azure.Cosmos.Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(dbName);

            Container idContainer = await database.CreateContainerIfNotExistsAsync(containerName, partitionKey);

            var invoices = GenerateInvoices();

            List <Task> concurrentTasks = new List <Task>();

            for (int i = 0; i < documentsCount / batchSize; i++)
            {
                List <Task> indexedTasks = new List <Task>();
                foreach (var invoice in invoices.Take(batchSize))
                {
                    indexedTasks.Add(idContainer.CreateItemAsync(invoice));
                }

                Console.WriteLine($"Starting insertion of {batchSize} documents into {containerName} in {dbName}");
                await Task.WhenAll(indexedTasks);

                Console.WriteLine($"Completed insertion of {batchSize} documents into {containerName} in {dbName}");
            }
        }
        public async Task CreateCollectionIfNotExistsAsync(string endPointUri, string secretKey, string databaseName, string collectionName, int throughput, string partitionKey)
        {
            Microsoft.Azure.Cosmos.Database db = await destinationCollectionClient.CreateDatabaseIfNotExistsAsync(databaseName);

            containerToStoreDocuments = await db.CreateContainerIfNotExistsAsync(collectionName, "/" + partitionKey);
        }
        public async Task Setup_CosmosResources()
        {
            // Create resources for Scenario 0 - setup
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("Running setup...\n");

            Console.ResetColor();

            // Create database for partitioning demo. We set the RU/s high to simulate having a large workload with many partitions, to demo the impact of cross-partition queries on larger datasets. If you do not wish to demo Scenario 2, replace the throughput with the minimum of 400 RU/s.
            var partitionDemoDatabaseName = "PartitioningDemo";

            Microsoft.Azure.Cosmos.Database partitioningDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : partitionDemoDatabaseName, throughput : 40000);

            Console.WriteLine("\tCreated database {0}", partitioningDemoDB.Id);

            // Create 2 containers with different partition keys for Scenarios
            UserReviewsV1Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v1", partitionKeyPath : "/id");

            Console.WriteLine("\tCreated container {0} with partition key 'id'", UserReviewsV1Container.Id);

            UserReviewsV2Container = await partitioningDemoDB.CreateContainerIfNotExistsAsync(id : "UserReviews_v2", partitionKeyPath : "/username");

            Console.WriteLine("\tCreated container {0} with partition key 'username'", UserReviewsV2Container.Id);

            // Create database Indexing policy demo
            var indexingDemoDatabaseName = "IndexingDemo";

            Microsoft.Azure.Cosmos.Database indexingDemoDB = await cosmosClient.CreateDatabaseIfNotExistsAsync(id : indexingDemoDatabaseName, throughput : 400);

            // Create containers for Indexing policy demo
            var defaultIndexingPolicyContatinerName = "DefaultIndexingPolicy";
            var tunedIndexingPolicyContainerName    = "TunedIndexingPolicy";

            ContainerResponse defaultIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : defaultIndexingPolicyContatinerName, partitionKeyPath : "/partitionKey")
                                                               .CreateIfNotExistsAsync();

            Console.WriteLine("\tCreated container {0} with default indexing policy", defaultIndexingPolicyContainer.Resource.Id);

            ContainerResponse tunedIndexingPolicyContainer = await indexingDemoDB.DefineContainer(name : tunedIndexingPolicyContainerName, partitionKeyPath : "/partitionKey")
                                                             // Define indexing policy with included and excluded paths
                                                             .WithIndexingPolicy()
                                                             .WithIncludedPaths()
                                                             .Path("/username/*")
                                                             .Path("/productName/*")
                                                             .Path("/verifiedPurchase/*")
                                                             .Attach()
                                                             .WithExcludedPaths()
                                                             .Path("/*")
                                                             .Attach()
                                                             .Attach()
                                                             .CreateIfNotExistsAsync();

            Console.WriteLine("\tCreated container {0} with tuned indexing policy", tunedIndexingPolicyContainer.Resource.Id);

            // Import data into the containers

            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine("\nStarting data import: this will take a few minutes...");

            Console.ResetColor();

            using (WebClient wc = new WebClient())
            {
                var            json = wc.DownloadString("https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json");
                List <dynamic> userProductReviews = JsonConvert.DeserializeObject <List <dynamic> >(json);

                Console.WriteLine("\tFinished downloading data from 'https://cosmosnotebooksdata.blob.core.windows.net/notebookdata/ProductReviewsData.json'");
                Console.WriteLine("\tWriting data into Cosmos containers...");

                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                // Enable bulk execution mode for faster import
                cosmosClient.ClientOptions.AllowBulkExecution = true;
                List <Task <int> > tasks = new List <Task <int> >();

                for (var i = 0; i < userProductReviews.Count; i++)
                {
                    var documentType = userProductReviews[i].documentType;

                    if (documentType == "user")
                    {
                        User user = JsonConvert.DeserializeObject <User>(userProductReviews[i].ToString());
                        tasks.Add(InsertUserItemsAsync(user));
                    }
                    else if (documentType == "review")
                    {
                        ProductReview productReview = JsonConvert.DeserializeObject <ProductReview>(userProductReviews[i].ToString());
                        tasks.Add(InsertProductReviewItemsAsync(productReview));
                    }
                }

                await Task.WhenAll(tasks);

                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;
                //Print results
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds / 10);

                Console.WriteLine("\tImport of {0} items into {1} and {2} containers completed. Total time: {3}", tasks.Sum(task => task.Result) / 2, UserReviewsV1Container.Id, UserReviewsV2Container.Id, elapsedTime);
            }

            // Scale down the database to the minimum RU/s.
            var currentThroughput = await partitioningDemoDB.ReadThroughputAsync(requestOptions : new RequestOptions());

            int minThroughput      = (int)currentThroughput.MinThroughput;
            var throughputResponse = await partitioningDemoDB.ReplaceThroughputAsync(minThroughput);

            Console.WriteLine("\tScaled PartitioningDemo database down to minimum {0} RU/s", throughputResponse.Resource.Throughput);

            // Reset bulk execution mode to false for rest of demo
            cosmosClient.ClientOptions.AllowBulkExecution = false;
        }