private static async Task RunDemoAsync( CosmosClient client, string databaseId, string containerId) { CosmosDatabase database = await client.CreateDatabaseIfNotExistsAsync(DatabaseId); CosmosContainerSettings containerSettings = new CosmosContainerSettings(containerId, "/LastName"); // Delete the existing container to prevent create item conflicts await database.GetContainer(containerId).DeleteAsync(); // Create with a throughput of 1000 RU/s CosmosContainer container = await database.CreateContainerIfNotExistsAsync( containerSettings, requestUnitsPerSecond : 1000); //Run a simple script await Program.RunSimpleScript(container); // Run Bulk Import await Program.RunBulkImport(container); // Run OrderBy await Program.RunOrderBy(container); //// Uncomment to Cleanup //await database.DeleteAsync(); }
public void ValidateUriGenerationForResources() { string databaseId = "db1234"; string crId = "cr42"; string spId = "sp9001"; string trId = "tr9002"; string udfId = "udf9003"; CosmosClient mockClient = MockDocumentClient.CreateMockCosmosClient(); CosmosDatabase db = new CosmosDatabase(mockClient, databaseId); Assert.AreEqual(db.Link, "/dbs/" + databaseId); CosmosContainer container = new CosmosContainer(db, crId); Assert.AreEqual(container.Link, "/dbs/" + databaseId + "/colls/" + crId); CosmosStoredProcedure sp = new CosmosStoredProcedure(container, spId); Assert.AreEqual(sp.Link, "/dbs/" + databaseId + "/colls/" + crId + "/sprocs/" + spId); CosmosTrigger tr = new CosmosTrigger(container, trId); Assert.AreEqual(tr.Link, "/dbs/" + databaseId + "/colls/" + crId + "/triggers/" + trId); CosmosUserDefinedFunction udf = new CosmosUserDefinedFunction(container, udfId); Assert.AreEqual(udf.Link, "/dbs/" + databaseId + "/colls/" + crId + "/udfs/" + udfId); }
private async void button_settings_import_Click(object sender, EventArgs e) { var openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { var filePath = openFileDialog1.FileName; using (Stream str = openFileDialog1.OpenFile()) { SQLiteAsyncConnection db = new SQLiteAsyncConnection(filePath); var query = db.Table <Session>(); var result = await query.ToListAsync(); CosmosDatabase database = await cosmosClient.CreateDatabaseIfNotExistsAsync(_settings.DatabaseId); CosmosContainer container = await cosmosClient.GetDatabase(_settings.DatabaseId).CreateContainerIfNotExistsAsync(_settings.ContainerId, "/partition"); foreach (var item in result) { ItemResponse <Session> createResponse = await container.UpsertItemAsync(item, new PartitionKey(item.partition)); } } } catch (SecurityException ex) { MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" + $"Details:\n\n{ex.StackTrace}"); } } }
public CompetitionRepository( IOptions <CosmosDbConfiguration> options, ILogger <CompetitionRepository> logger) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } var configuration = options.Value; _logger = logger; _client = new CosmosClient(configuration.Uri, configuration.MasterKey); _database = ConnectToDatabase( configuration.DatabaseId, configuration.DatabaseThroughput) .Result; _container = ConnectToContainer( configuration.ContainerId, $"/{nameof(Competition.Location)}/{nameof(Location.State)}") .Result; }
private void SetUpCosmosDB() { var data = _config.CosmosDbSettings.ConnectionString; CosmosClient cosmosClient = new CosmosClient(_config.CosmosDbSettings.ConnectionString); _database = cosmosClient.CreateDatabaseIfNotExistsAsync(_config.CosmosDbSettings.DatabaseName) .GetAwaiter() .GetResult(); _database.CreateContainerIfNotExistsAsync( _config.CosmosDbSettings.BikeContainerName, _config.CosmosDbSettings.BikePartitionKeyPath, 400) .GetAwaiter() .GetResult(); _database.CreateContainerIfNotExistsAsync( "Enquiry", "/customerAttachedEmail", 400 ) .GetAwaiter() .GetResult(); _database.CreateContainerIfNotExistsAsync( _config.CosmosDbSettings.BikeReservationContainerName, _config.CosmosDbSettings.BikeReservationPartitionKeyPath, 400) .GetAwaiter() .GetResult(); }
public static IServiceCollection AddDataServices(this IServiceCollection services) { var serviceProvider = services.BuildServiceProvider(); var cosmoDbConfiguration = serviceProvider.GetRequiredService <ICosmosDbDataServiceConfiguration>(); CosmosClientOptions cosmosClientOptions = new CosmosClientOptions { SerializerOptions = new CosmosSerializationOptions() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase } }; CosmosClient cosmosClient = new CosmosClient(cosmoDbConfiguration.ConnectionString, cosmosClientOptions); CosmosDatabase database = cosmosClient.CreateDatabaseIfNotExistsAsync(cosmoDbConfiguration.DatabaseName) .GetAwaiter() .GetResult(); CosmosContainer container = database.CreateContainerIfNotExistsAsync( cosmoDbConfiguration.ContainerName, cosmoDbConfiguration.PartitionKeyPath, 400) .GetAwaiter() .GetResult(); services.AddSingleton(cosmosClient); services.AddSingleton <IDataService <FileAnalysisResult>, CosmosDbDataService <FileAnalysisResult> >(); return(services); }
private static async Task <CosmosContainer> CreateContainerIfNotExistsAsync( ICosmosService service, CosmosDatabase database, string containerName, string partitionKey, int?throughput) { string partitionKeyPath = null; if (!string.IsNullOrEmpty(partitionKey)) { if (!partitionKey.StartsWith("/")) { partitionKeyPath = $"/{partitionKey}"; } } if (throughput == 0) { throughput = null; } return(await service.CreateContainerIfNotExistsAsync( database, new ContainerProperties { Id = containerName, PartitionKeyPath = partitionKeyPath }, throughput)); }
private static async Task CreateDatabaseAsync(CosmosClient cosmosClient) { // Create a new database CosmosDatabase database = await cosmosClient.CreateDatabaseIfNotExistsAsync(Program.DatabaseId); Console.WriteLine("Created Database: {0}\n", database.Id); }
private static async Task DeleteDatabaseAndCleanupAsync(CosmosClient cosmosClient) { CosmosDatabase database = cosmosClient.GetDatabase(Program.DatabaseId); DatabaseResponse databaseResourceResponse = await database.DeleteAsync(); Console.WriteLine("Deleted Database: {0}\n", Program.DatabaseId); }
public static IServiceCollection AddDataServices(this IServiceCollection services) { var serviceProvider = services.BuildServiceProvider(); var cosmoDbConfiguration = serviceProvider.GetRequiredService <ICosmosDbDataServiceConfiguration>(); CosmosClientOptions cosmosClientOptions = new CosmosClientOptions { SerializerOptions = new CosmosSerializationOptions() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase } }; CosmosClient cosmosClient = new CosmosClient(cosmoDbConfiguration.ConnectionString, cosmosClientOptions); CosmosDatabase database = cosmosClient.CreateDatabaseIfNotExistsAsync(cosmoDbConfiguration.DatabaseName) .GetAwaiter() .GetResult(); CosmosContainer container = database.CreateContainerIfNotExistsAsync( cosmoDbConfiguration.ContainerName, cosmoDbConfiguration.PartitionKeyPath, 400) .GetAwaiter() .GetResult(); services.AddSingleton(cosmosClient); var sqlDbConfiguration = serviceProvider.GetRequiredService <ISqlDbDataServiceConfiguration>(); services.AddDbContext <SqlDbContext>(c => c.UseSqlServer(sqlDbConfiguration.ConnectionString)); services.AddScoped(typeof(IDataService <Product>), typeof(SqlDbDataService <Product>)); services.AddSingleton(typeof(IDataService <ProductLocation>), typeof(CosmosDbDataService <ProductLocation>)); return(services); }
public static async Task <RecipesReader> InitializeRecipesCosmosInstance(IOptions <RecipesReaderConfig> options, ILogger <RecipesReader> logger) { var cosmosConfig = options.Value; CosmosClientOptions cosmosClientOptions = new CosmosClientOptions { MaxRetryAttemptsOnRateLimitedRequests = 9, MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(60) }; CosmosClient cosmosClient = new CosmosClient( cosmosConfig.endPointUrl, cosmosConfig.authorizationKey, cosmosClientOptions ); /** * * Need some try catch logic here * **/ RecipesReader recipesCosmosClient = new RecipesReader(logger, cosmosConfig, cosmosClient); CosmosDatabase cosmosDatabase = cosmosClient.GetDatabase(cosmosConfig.databaseId); cosmosDatabase.GetContainer(cosmosConfig.containerId); return(recipesCosmosClient); }
public CosmosDbSqlProviderV3(CosmosDbSqlProviderV3Options options) { EnsureArg.IsNotNull(options, nameof(options)); EnsureArg.IsNotNull(options.Client, nameof(options.Client)); // https://azure.microsoft.com/en-us/blog/azure-cosmos-dotnet-sdk-version-3-0-now-in-public-preview/ // https://github.com/Azure/azure-cosmos-dotnet-v3 // https://github.com/Azure/azure-cosmos-dotnet-v3/issues/68 this.client = options.Client; this.partitionKeyPath = options.PartitionKeyPath.EmptyToNull() ?? "/Discriminator"; // needed? each type T is persisted in own collection this.partitionKeyValue = typeof(T).FullName; this.database = /*await */ this.client.Databases .CreateDatabaseIfNotExistsAsync(options.Database.EmptyToNull() ?? "master", throughput: options.ThroughPut).Result; this.container = /*await*/ this.database.Containers .CreateContainerIfNotExistsAsync( new CosmosContainerSettings( options.Container.EmptyToNull() ?? typeof(T).PrettyName().Pluralize().ToLower(), partitionKeyPath: this.partitionKeyPath) { IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }) }, throughput: options.ThroughPut).Result; }
private async Task ContainerOperations(CosmosDatabase database, bool dbNotExist) { // Create should fail if the database does not exist if (dbNotExist) { CosmosContainerSettings newcontainerSettings = new CosmosContainerSettings(id: DoesNotExist, partitionKeyPath: "/pk"); this.VerifyNotFoundResponse(await database.CreateContainerStreamAsync(newcontainerSettings, requestUnitsPerSecond: 500)); } CosmosContainer doesNotExistContainer = database.GetContainer(DoesNotExist); this.VerifyNotFoundResponse(await doesNotExistContainer.ReadStreamAsync()); CosmosContainerSettings containerSettings = new CosmosContainerSettings(id: DoesNotExist, partitionKeyPath: "/pk"); this.VerifyNotFoundResponse(await doesNotExistContainer.ReplaceStreamAsync(containerSettings)); this.VerifyNotFoundResponse(await doesNotExistContainer.DeleteStreamAsync()); // Validate Child resources await this.ItemOperations(doesNotExistContainer, true); // The database exists create a container and validate it's children if (!dbNotExist) { CosmosContainer containerExists = await database.CreateContainerAsync( id : "NotFoundTest" + Guid.NewGuid().ToString(), partitionKeyPath : "/pk"); await this.ItemOperations(containerExists, false); } }
public async Task ValidateQueryNotFoundResponse() { CosmosDatabase db = await CosmosNotFoundTests.client.CreateDatabaseAsync("NotFoundTest" + Guid.NewGuid().ToString()); CosmosContainer container = await db.CreateContainerAsync("NotFoundTest" + Guid.NewGuid().ToString(), "/pk", 500); dynamic randomItem = new { id = "test", pk = "testpk" }; await container.CreateItemAsync(randomItem); await container.DeleteAsync(); var crossPartitionQueryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2); var queryResponse = await crossPartitionQueryIterator.FetchNextSetAsync(); Assert.IsNotNull(queryResponse); Assert.AreEqual(HttpStatusCode.Gone, queryResponse.StatusCode); var queryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 1, partitionKey: new Cosmos.PartitionKey("testpk")); this.VerifyQueryNotFoundResponse(await queryIterator.FetchNextSetAsync()); var crossPartitionQueryIterator2 = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2); this.VerifyQueryNotFoundResponse(await crossPartitionQueryIterator2.FetchNextSetAsync()); await db.DeleteAsync(); }
public async Task SharedThroughputTests() { string databaseId = Guid.NewGuid().ToString(); int throughput = 10000; CosmosDatabaseResponse createResponse = await this.CreateDatabaseHelper(databaseId, databaseExists : false, throughput : throughput); Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode); CosmosDatabase cosmosDatabase = createResponse; int? readThroughput = await cosmosDatabase.ReadProvisionedThroughputAsync(); Assert.AreEqual(throughput, readThroughput); string containerId = Guid.NewGuid().ToString(); string partitionPath = "/users"; CosmosContainerResponse containerResponse = await cosmosDatabase.Containers.CreateContainerAsync(containerId, partitionPath); Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode); CosmosContainer container = containerResponse; readThroughput = await container.ReadProvisionedThroughputAsync(); Assert.IsNull(readThroughput); await container.DeleteAsync(); await cosmosDatabase.DeleteAsync(); }
public async Task ValidateDatabaseNotFoundResponse() { CosmosDatabase database = CosmosNotFoundTests.client.GetDatabase(DoesNotExist); this.VerifyNotFoundResponse(await database.ReadStreamAsync()); this.VerifyNotFoundResponse(await database.DeleteStreamAsync()); }
public async Task ValidateCurrentWriteQuorumAndReplicaSetHeader() { CosmosClient client = TestCommon.CreateCosmosClient(false); CosmosDatabase db = null; try { db = await client.CreateDatabaseAsync(Guid.NewGuid().ToString()); PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection <string>(new[] { "/id" }), Kind = PartitionKind.Hash }; CosmosContainerSettings containerSetting = new CosmosContainerSettings() { Id = Guid.NewGuid().ToString(), PartitionKey = partitionKeyDefinition }; CosmosContainer coll = await db.CreateContainerAsync(containerSetting); Document documentDefinition = new Document { Id = Guid.NewGuid().ToString() }; ItemResponse <Document> docResult = await coll.CreateItemAsync <Document>(documentDefinition); Assert.IsTrue(int.Parse(docResult.Headers[WFConstants.BackendHeaders.CurrentWriteQuorum], CultureInfo.InvariantCulture) > 0); Assert.IsTrue(int.Parse(docResult.Headers[WFConstants.BackendHeaders.CurrentReplicaSetSize], CultureInfo.InvariantCulture) > 0); } finally { await db.DeleteAsync(); } }
public async Task ImplicitConversion() { string databaseName = Guid.NewGuid().ToString(); CosmosDatabaseResponse cosmosDatabaseResponse = await this.cosmosClient.Databases[databaseName].ReadAsync(cancellationToken: this.cancellationToken); CosmosDatabase cosmosDatabase = cosmosDatabaseResponse; CosmosDatabaseSettings cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNull(cosmosDatabaseSettings); cosmosDatabaseResponse = await this.CreateDatabaseHelper(); cosmosDatabase = cosmosDatabaseResponse; cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNotNull(cosmosDatabaseSettings); cosmosDatabaseResponse = await cosmosDatabase.DeleteAsync(cancellationToken : this.cancellationToken); cosmosDatabase = cosmosDatabaseResponse; cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNull(cosmosDatabaseSettings); }
/* * Create the database if it does not exist */ private async Task CreateDatabase() { // Create a new database this.database = await this.cosmosClient.Databases.CreateDatabaseIfNotExistsAsync(databaseId); Console.WriteLine("Created Database: {0}\n", this.database.Id); }
public DatabaseNodeViewModel( AccountNodeViewModel account, CosmosDatabase database, IDatabaseContext context, DatabaseCommands databaseCommands, ContainerCommands containerCommands, IViewModelFactory viewModelFactory, IMessenger messenger) { Account = account; Id = database.Id; Context = context; _viewModelFactory = viewModelFactory; Commands = new[] { new CommandViewModel("Create container", containerCommands.CreateCommand, this), CommandViewModel.Separator(), new CommandViewModel("Refresh", RefreshCommand), CommandViewModel.Separator(), new CommandViewModel("Create database", databaseCommands.CreateCommand, Account), new CommandViewModel("Edit database", databaseCommands.EditCommand, this), new CommandViewModel("Delete database", databaseCommands.DeleteCommand, this), }; messenger.Subscribe(this).To <ContainerCreatedMessage>((vm, message) => vm.OnContainerCreated(message)); messenger.Subscribe(this).To <ContainerDeletedMessage>((vm, message) => vm.OnContainerDeleted(message)); }
private static async Task RunDemoAsync( CosmosClient client, string databaseId, string containerId) { CosmosDatabase database = await client.Databases.CreateDatabaseIfNotExistsAsync(DatabaseId); CosmosContainerSettings containerSettings = new CosmosContainerSettings(containerId, "/LastName"); // Use the recommended indexing policy which supports range queries/sorting on strings containerSettings.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }); // Delete the existing container to prevent create item conflicts await database.Containers[containerId].DeleteAsync(); // Create with a throughput of 1000 RU/s CosmosContainer container = await database.Containers.CreateContainerIfNotExistsAsync( containerSettings, throughput : 1000); //Run a simple script await Program.RunSimpleScript(container); // Run Bulk Import await Program.RunBulkImport(container); // Run OrderBy await Program.RunOrderBy(container); //// Uncomment to Cleanup //await database.DeleteAsync(); }
/// <summary> /// Initialize a static instance of the <see cref="CosmosClient"/>. /// </summary> /// <returns></returns> private static CosmosClient InitializeCosmosClient() { string endpoint = configuration["EndPointUrl"]; if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("Please specify a valid endpoint in the appSettings.json"); } string authKey = configuration["AuthorizationKey"]; if (string.IsNullOrEmpty(authKey) || string.Equals(authKey, "Super secret key")) { throw new ArgumentException("Please specify a valid AuthorizationKey in the appSettings.json"); } var cosmosConfiguration = new CosmosConfiguration(endpoint, authKey); // Customize client configuration var cosmosClient = new CosmosClient(cosmosConfiguration); // Optional. Initialize container CosmosDatabaseResponse databaseResponse = cosmosClient.Databases.CreateDatabaseIfNotExistsAsync("mydb").Result; CosmosDatabase database = databaseResponse.Database; var containerResponse = database.Containers.CreateContainerIfNotExistsAsync("mycoll", "/id").Result; return(cosmosClient); }
public async Task <List <Product> > GetProducts() { var products = new List <Product>(); string databaseId = "db"; string containerId = "products"; string partitionKey = "/id"; // Create new instance of CosmosClient using (CosmosClient cosmosClient = new CosmosClient("https://vssummit2019th.documents.azure.com:443/", "zZMBHNJQ5Ai82hvIlIFBvOPM1tdGeVK3MxpIRSnserXKSqbV2Fe4l7KmGBXwkZAyv1VRDCkBY4FdNKxk2lobbA==")) { // Create new database CosmosDatabase database = await cosmosClient.Databases.CreateDatabaseIfNotExistsAsync(databaseId); // Create new container CosmosContainer container = await database.Containers.CreateContainerIfNotExistsAsync(containerId, partitionKey); var product = container.Items.GetItemIterator <Product>(maxItemCount: 1); var setIterator = container.Items.GetItemIterator <Product>(maxItemCount: 1); while (setIterator.HasMoreResults) { foreach (Product item in await setIterator.FetchNextSetAsync()) { products.Add(item); } } return(products); } }
public async Task InitAsync() { _cosmosClient = new CosmosClient(_cosmosConnectionString); _database = await _cosmosClient.Databases.CreateDatabaseIfNotExistsAsync(DatabaseId); _container = await _database.Containers.CreateContainerIfNotExistsAsync(ContainerId, PartitionKey); }
private static async Task Initialize(CosmosClient client) { database = await client.Databases.CreateDatabaseIfNotExistsAsync(databaseId); // Delete the existing container to prevent create item conflicts await database.Containers[containerId].DeleteAsync(); // We create a partitioned collection here which needs a partition key. Partitioned collections // can be created with very high values of provisioned throughput (up to Throughput = 250,000) // and used to store up to 250 GB of data. You can also skip specifying a partition key to create // single partition collections that store up to 10 GB of data. // For this demo, we create a collection to store SalesOrders. We set the partition key to the account // number so that we can retrieve all sales orders for an account efficiently from a single partition, // and perform transactions across multiple sales order for a single account number. CosmosContainerSettings containerSettings = new CosmosContainerSettings(containerId, partitionKeyPath: "/AccountNumber"); // Use the recommended indexing policy which supports range queries/sorting on strings containerSettings.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 }); // Create with a throughput of 1000 RU/s container = await database.Containers.CreateContainerIfNotExistsAsync( containerSettings, throughput : 1000); }
public async Task <CosmosContainer> CreateContainerIfNotExistsAsync( CosmosDatabase database, ContainerProperties containerProperties, int?throughput = null, RequestOptions requestOptions = null) { return(await database.CreateContainerIfNotExistsAsync(containerProperties, throughput, requestOptions)); }
internal CosmosContainerFluentDefinitionForCreate( CosmosDatabase cosmosContainers, string name, string partitionKeyPath = null) : base(name, partitionKeyPath) { this.cosmosContainers = cosmosContainers; }
public void PacificShouldBeDeserializedWith8HoursOffset() { var serializerSettings = CosmosDatabase.CreateJsonSerializerSettings(); var jsonValue = "\"2019-05-08T08:00:00.0000000+08:00\""; var result = JsonConvert.DeserializeObject <DateTimeOffset>(jsonValue, serializerSettings); Assert.Equal(8, result.Offset.Hours); }
/// <summary> /// Get a DocuemntContainer by id, or create a new one if one with the id provided doesn't exist. /// </summary> /// <param name="id">The id of the CosmosContainer to search for, or create.</param> /// <returns>The matched, or created, CosmosContainer object</returns> private static async Task <CosmosContainer> GetOrCreateContainerAsync(CosmosDatabase database, string containerId) { CosmosContainerSettings containerDefinition = new CosmosContainerSettings(id: containerId, partitionKeyPath: "/LastName"); return(await database.CreateContainerIfNotExistsAsync( containerSettings : containerDefinition, requestUnitsPerSecond : 400)); }
public void Deserialize_custom() { var entity = JsonConvert.DeserializeObject <TestEntity>( "{ \"type\": \"MyObject\", \"version\": \"v3\" }", CosmosDatabase.CreateJsonSerializerSettings(_options)); Assert.Equal(Version.Third, entity.Version); }