Cosmos db connect with java code --->>> How to ??

Ey 1 Reputation point
2021-08-12T15:21:05.237+00:00

I am trying to connect to cosmos db with java code (Uisng intellij)
in order to validate some field exist or not.

can someone gove me the simplest sample code for doing connection and quick sql query for pull some data ?

Thanks!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,688 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Oury Ba-MSFT 19,726 Reputation points Microsoft Employee
    2021-08-13T06:19:49.85+00:00

    Hi @Ey Thank you for posting your question.

    Please try the below samples using either an IDE (Eclipse, IntelliJ, or VSCODE) or from the command line using Maven.

    These environment variables must be set
    ACCOUNT_HOST=your account hostname;ACCOUNT_KEY=your account primary key (https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-java-sdk-samples)

    / Create sync client
    // <CreateSyncClient>
    client = new CosmosClientBuilder()
    .endpoint(AccountSettings.HOST)
    .key(AccountSettings.MASTER_KEY)
    .preferredRegions(preferredRegions)
    .consistencyLevel(ConsistencyLevel.EVENTUAL)
    .contentResponseOnWriteEnabled(true)
    .buildClient();

    Please refer to the below link for java Quickstart examples.

    https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/blob/main/src/main/java/com/azure/cosmos/examples/crudquickstart/sync/SampleCRUDQuickstart.java

    Regards,
    Oury


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.