다음을 통해 공유


CosmosClient Class

Definition

Provides a client-side logical representation of the Azure Cosmos DB account. This client can be used to configure and execute requests in the Azure Cosmos DB database service.

CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to performance guide at https://docs.microsoft.com/azure/cosmos-db/performance-tips.

public class CosmosClient : IDisposable
type CosmosClient = class
    interface IDisposable
Public Class CosmosClient
Implements IDisposable
Inheritance
CosmosClient
Implements

Examples

This example create a CosmosClient, CosmosDatabase, and a CosmosContainer. The CosmosClient is created with the connection string and configured to use "East US 2" region.

using Azure.Cosmos;

CosmosClient cosmosClient = new CosmosClient(
            "connection-string-from-portal", 
            new CosmosClientOptions()
            {
                ApplicationRegion = Regions.EastUS2,
            });

Database db = await client.CreateDatabaseAsync("database-id");
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

This example create a CosmosClient, CosmosDatabase, and a CosmosContainer. The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and configured to use "East US 2" region.

using Azure.Cosmos;

CosmosClient cosmosClient = new CosmosClient(
            "account-endpoint-from-portal", 
            "account-key-from-portal", 
            new CosmosClientOptions()
            {
                ApplicationRegion = Regions.EastUS2,
            });

Database db = await client.CreateDatabaseAsync("database-id");
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

This example create a CosmosClient, CosmosDatabase, and a CosmosContainer. The CosmosClient is created through builder pattern CosmosClientBuilder.

using Azure.Cosmos;
using Azure.Cosmos.Fluent;

CosmosClient cosmosClient = new CosmosClientBuilder("connection-string-from-portal")
    .WithApplicationRegion("East US 2")
    .Build();

Database db = await client.CreateDatabaseAsync("database-id")
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

Remarks

https://docs.microsoft.com/azure/cosmos-db/performance-tips https://docs.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk https://docs.microsoft.com/azure/cosmos-db/distribute-data-globally https://docs.microsoft.com/azure/cosmos-db/partitioning-overview https://docs.microsoft.com/azure/cosmos-db/request-units

Constructors

CosmosClient()

Create a new CosmosClient used for mock testing

CosmosClient(String, CosmosClientOptions)

Create a new CosmosClient with the connection string

CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to performance guide at https://docs.microsoft.com/azure/cosmos-db/performance-tips.

CosmosClient(String, String, CosmosClientOptions)

Create a new CosmosClient with the account endpoint URI string and account key

CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to performance guide at https://docs.microsoft.com/azure/cosmos-db/performance-tips.

Properties

ClientOptions

The CosmosClientOptions used initialize CosmosClient

Endpoint

Gets the endpoint Uri for the Azure Cosmos DB service.

Methods

CreateDatabaseAsync(String, Nullable<Int32>, RequestOptions, CancellationToken)

Send a request for creating a database.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

CreateDatabaseIfNotExistsAsync(String, Nullable<Int32>, RequestOptions, CancellationToken)

Check if a database exists, and if it doesn't, create it. Only the database id is used to verify if there is an existing database. Other database properties such as throughput are not validated and can be different then the passed properties.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

CreateDatabaseStreamAsync(DatabaseProperties, Nullable<Int32>, RequestOptions, CancellationToken)

Send a request for creating a database.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

Dispose()

Dispose of cosmos client

Dispose(Boolean)

Dispose of cosmos client

GetContainer(String, String)

Returns a proxy reference to a container.

GetDatabase(String)

Returns a proxy reference to a database.

GetDatabaseQueryIterator<T>(QueryDefinition, String, QueryRequestOptions, CancellationToken)

This method creates a query for databases under an Cosmos DB Account using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition.

GetDatabaseQueryIterator<T>(String, String, QueryRequestOptions, CancellationToken)

This method creates a query for databases under an Cosmos DB Account using a SQL statement. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

GetDatabaseQueryStreamIterator(QueryDefinition, String, QueryRequestOptions, CancellationToken)

This method creates a query for databases under an Cosmos DB Account using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition.

GetDatabaseQueryStreamIterator(String, String, QueryRequestOptions, CancellationToken)

This method creates a query for databases under an Cosmos DB Account using a SQL statement. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

ReadAccountAsync()

Read Azure Cosmos DB account properties AccountProperties

Applies to