Skip to content

Latest commit

 

History

History
103 lines (84 loc) · 3.99 KB

File metadata and controls

103 lines (84 loc) · 3.99 KB
author wchigit
ms.service service-connector
ms.topic include
ms.date 12/04/2023
ms.author wchi
  1. Install dependency.

    dotnet add package Microsoft.Azure.Cosmos
  2. Get the connection string from the environment variable added by Service Connector.

    using Microsoft.Azure.Cosmos;
    using System; 
    
    // Create a new instance of CosmosClient using a connection string
    using CosmosClient client = new(
        connectionString: Environment.GetEnvironmentVariable("AZURE_COSMOS_CONNECTIONSTRING")!
    );
  1. Add the following dependency in your pom.xml file:
    <dependency>
    	<groupId>com.azure</groupId>
    	<artifactId>azure-cosmos</artifactId>
    	<version>LATEST</version>
    </dependency>
  2. Get the connection string from the environment variable added by Service Connector.
    import com.azure.cosmos.CosmosClient;
    import com.azure.cosmos.CosmosClientBuilder;
    
    String connectionStr = System.getenv("AZURE_COSMOS_CONNECTIONSTRING");
    String[] connInfo = connectionStr.split(";");
    String endpoint = connInfo[0].split("=")[1];
    String accountKey = connInfo[1].split("=")[1];
    
    CosmosClient cosmosClient = new CosmosClientBuilder()
        .endpoint(endpoint)
        .key(accountKey)
        .buildClient();

Refer to Spring Data Azure Cosmos DB v3 examples and Build a Spring Data Azure Cosmos DB v3 app to manage Azure Cosmos DB for NoSQL data to set up your Spring application. The configuration properties are added to Spring Apps by Service Connector. Two sets of configuration properties are provided according to the version of Spring Cloud Azure (below 4.0 and above 4.0). For more information about library changes of Spring Cloud Azure, refer to Spring Cloud Azure Migration Guide. It is recommended to use Spring Cloud Azure version 4.0 and above. The configurations in the format of "azure.cosmos.*" from Spring Cloud Azure 3.x will no longer be supported after 1st July, 2024.

  1. Install dependency.
    pip install azure-cosmos
  2. Get the connection string from the environment variable added by Service Connector.
    import os
    from azure.cosmos import CosmosClient
    
    # Create a new instance of CosmosClient using a connection string
    CONN_STR = os.environ["AZURE_COSMOS_CONNECTIONSTRING"]
    client = CosmosClient.from_connection_string(conn_str=CONN_STR) 
  1. Install dependency.
    go get github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos
  2. Get the connection string from the environment variable added by Service Connector.
    import {
        "github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
    }
    connectionString := os.Getenv("AZURE_COSMOS_CONNECTIONSTRING")
    client, err := azcosmos.NewClientFromConnectionString(connectionString, nil)
  1. Install dependency.
    npm install @azure/cosmos
  2. Get the connection string from the environment variable added by Service Connector.
    import { CosmosClient } from "@azure/cosmos";
    
    // Create a new instance of CosmosClient using a connection string
    const cosmosClient = new CosmosClient(process.env.AZURE_COSMOS_CONNECTIONSTRING);

For other languages, you can use the endpoint URL and other properties that Service Connector sets to the environment variables to connect to Azure Cosmos DB for NoSQL. For environment variable details, see Integrate Azure Cosmos DB for NoSQL with Service Connector.