title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
12:202
|
Since databases just have a name and no configuration, you can Add a container directly and then place the database where you wish:
|
12:203
| |
12:204
|
Figure 12.6: Adding a container in Azure Cosmos DB
|
12:205
|
Here, you can decide on database and container names and the property to use for sharding (the partition key). Since NoSQL entries are object trees, property names are specified as paths. You can also add properties whose values are required to be unique.
|
12:206
|
However, the uniqueness of IDs is checked inside each partition, so this option is only useful in certain situations, such as multi-tenant applications (where each tenant is included in a single shard). The fees depend on the collection throughput that you choose.
|
12:207
|
This is where you need to target all resource parameters to your needs. Throughput is expressed in request units per second, where request units per second are defined as the throughput we have when performing a read of 1 KB per second. Hence, if you check the Provision database throughput option, the chosen throughput is shared with the whole database instead of being reserved as a single collection.
|
12:208
|
Accessing Azure Cosmos DB
|
12:209
|
After creating the Azure Cosmos container, you will be able to access data. To get connection information, you can select the Keys menu. There, you will see all the information you need to connect with your Cosmos DB account from your application. The connection information page will provide you with the account URI and two connection keys, which can be used interchangeably to connect with the account.
|
12:210
| |
12:211
|
Figure 12.7: Connection information page
|
12:212
|
There are also keys with read-only privileges. Every key can be regenerated, and each account has two equivalent keys, like many other Azure components. This approach enables operations to be handled efficiently; that is, when a key is changed, the other one is kept. Therefore, existing applications can continue using the other key before upgrading to the new key.
|
12:213
|
Defining database consistency
|
12:214
|
Considering that you are in the context of a distributed database, Azure Cosmos DB enables you to define the default read consistency level you will have. By selecting Default consistency in the main menu of your Cosmos DB account, you can choose the default replication consistency that you wish to apply to all your containers.
|
12:215
|
This default can be overridden in each container, either from Data Explorer or programmatically. Consistency problems in read/write operations are a consequence of data replication. More specifically, the results of various read operations may be incoherent if the read operations are executed on different replicas that have received different partial updates.
|
12:216
|
The following are the available consistency levels. These have been ordered from the weakest to the strongest:
|
12:217
| |
12:218
|
Eventual: After enough time has passed, if no further write operations are done, all the reads converge and apply all the writes. The order of writes is also not guaranteed, so while writes are being processed, you could also end up reading an earlier version than the one you have previously read.
|
12:219
|
Consistent prefix: All the writes are executed in the same order on all the replicas. So, if there are n write operations, each read is consistent with the result of applying the first m writes for some m less than or equal to n.
|
12:220
|
Session: This is the same as the consistency prefix but also guarantees that each writer sees the result of its own writes in all subsequent read operations and that subsequent reads of each reader are coherent (either the same database or a more updated version of it).
|
12:221
|
Bounded staleness: This is associated either with a delay time, Delta, or with several operations, N. Each read sees the results of all the write operations that were performed before a time Delta (or before the last N operations). That is, its reads converge with the result of all the writes with a maximum time delay of Delta (or a maximum operations delay of N).
|
12:222
|
Strong: This is bounded staleness combined with Delta = 0. Here, each read reflects the result of all previous write operations.
|
12:223
| |
12:224
|
The strongest consistency can be obtained to the detriment of performance. By default, the consistency is set to Session, which is a good compromise between coherence and performance. A lower level of consistency is difficult to handle in applications and is only usually acceptable if sessions are either read-only or write-only.
|
12:225
|
If you select the Settings option in the Data Explorer menu of the container of your database, you can configure which paths to index and which kind of indexing to apply to each data type of each path. The configuration consists of a JSON object. Let us analyze its various properties:
|
12:226
|
{
|
12:227
|
`indexingMode`: `consistent`,
|
12:228
|
`automatic`: true,
|
12:229
|
...
|
12:230
| |
12:231
|
If you set indexingMode to none instead of consistent, no index is generated, and the collection can be used as a key-value dictionary that is indexed by the collection’s primary key. In this scenario, no secondary indexes are generated, so the primary key cannot efficiently be searched.
|
12:232
|
When automatic is set to true, all document properties are automatically indexed:
|
12:233
|
{
|
12:234
|
...
|
12:235
|
`includedPaths`: [
|
12:236
|
{
|
12:237
|
`path`: `/*`,
|
12:238
|
`indexes`: [
|
12:239
|
{
|
12:240
|
`kind`: `Range`,
|
12:241
|
`dataType`: `Number`,
|
12:242
|
`precision`: -1
|
12:243
|
},
|
12:244
|
{
|
12:245
|
`kind`: `Range`,
|
12:246
|
`dataType`: `String`,
|
12:247
|
`precision`: -1
|
12:248
|
},
|
12:249
|
{
|
12:250
|
`kind`: `Spatial`,
|
12:251
|
`dataType`: `Point`
|
12:252
|
}
|
12:253
|
]
|
12:254
|
}
|
12:255
|
]
|
12:256
|
},
|
12:257
|
...
|
12:258
| |
12:259
|
Each entry in includedPaths specifies a path pattern such as /subpath1/subpath2/? (settings apply just to the /subpath1/subpath2/property) or /subpath1/subpath2/* (settings apply to all the paths starting with /subpath1/subpath2/).
|
12:260
|
Patterns contain the [] symbol when settings must be applied to child objects contained in collection properties; for example, /subpath1/subpath2/[]/?, /subpath1/subpath2/[]/childpath1/?, and so on. Settings specify the index type to apply to each data type (string, number, geographic point, and so on). Range indexes are needed for comparison operations, while hash indices are more efficient if we need equality comparisons.
|
12:261
|
It is possible to specify a precision, that is, the maximum number of characters or digits to use in all the index keys. -1 means the maximum precision and is always recommended:
|
12:262
|
...
|
12:263
|
`excludedPaths`: [
|
12:264
|
{
|
12:265
|
`path`: `/`_etag`/?`
|
12:266
|
}
|
12:267
|
]
|
12:268
| |
12:269
|
Paths contained in excludedPaths are not indexed at all. Index settings can also be specified programmatically.
|
12:270
|
Here, you have two options to connect to Cosmos DB: use a version of its official client for your preferred programming language or use Cosmos DB’s Entity Framework Core provider. In the following subsections, we will have a look at both options. Then, we will describe how to use Cosmos DB’s Entity Framework Core provider with a practical example.
|
12:271
|
The Cosmos DB client
|
12:272
|
The Cosmos DB client for .NET 8 is available through the Microsoft.Azure.Cosmos NuGet package. It offers full control of all Cosmos DB features, while the Cosmos DB Entity Framework provider is easier to use but hides some Cosmos DB peculiarities. Follow these steps to interact with Cosmos DB through the official Cosmos DB client for .NET 8.
|
12:273
|
The following code sample shows the creation of a database and a container using the client component. Any operation requires the creation of a client object. Do not forget that the client must be disposed of by calling its Dispose method (or by enclosing the code that references it in a using statement) when you do not need it anymore:
|
12:274
|
public static async Task CreateCosmosDB()
|
12:275
|
{
|
12:276
|
using var cosmosClient = new CosmosClient(endpoint, key);
|
12:277
|
Database database = await
|
12:278
|
cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);
|
12:279
|
ContainerProperties cp = new ContainerProperties(containerId,
|
12:280
|
`/DestinationName`);
|
12:281
|
Container container = await database.CreateContainerIfNotExistsAsync(cp);
|
12:282
|
await AddItemsToContainerAsync(container);
|
12:283
|
}
|
12:284
| |
12:285
|
During collection creation, you can pass a ContainerProperties object, where you can specify the consistency level, how to index properties, and all the other collection features.
|
12:286
|
Then, you must define the .NET classes that correspond to the structure of the JSON document you need to manipulate in your collections. You can also use the JsonProperty attribute to map class property names to JSON names if they are not equal:
|
12:287
|
public class Destination
|
12:288
|
{
|
12:289
|
[JsonPropertyName(`id`)]
|
12:290
|
public string Id { get; set; }
|
12:291
|
public string DestinationName { get; set; }
|
12:292
|
public string Country { get; set; }
|
12:293
|
public string Description { get; set; }
|
12:294
|
public Package[] Packages { get; set; }
|
12:295
|
}
|
12:296
| |
12:297
| |
12:298
|
NoSQL means Not Only SQL, so it is also possible to map properties. The great thing about NoSQL is that you must map these properties without causing damage to other properties or information you have in the document you are connecting to.
|
12:299
| |
12:300
|
Once you have all the necessary classes, you can use client methods to ReadItemAsync, CreateItemAsync, and DeleteItemAsync. You can also query data using a QueryDefinition object that accepts SQL commands. You can find a complete introduction to this library at https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-get-started.
|
12:301
|
The Cosmos DB Entity Framework Core provider
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.