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!
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!
Hi @Ey-8423 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://docs.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.
Regards,
Oury
Got:
java.lang.RuntimeException: Client initialization failed. Check if the endpoint is reachable and if your auth token is valid
at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:372)
at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:393)
at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:225)
at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:104)
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:729)
at com.azure.cosmos.CosmosClient.<init>(CosmosClient.java:30)
at com.azure.cosmos.CosmosClientBuilder.buildClient(CosmosClientBuilder.java:741)
at CosmosDb.queriesDemo(CosmosDb.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Hi @Ey-8423 Can you please make sure you are passing the correct credentials and check if your cosmosdb account is reachable. The cluster may have a firewall issue which can block the connection to the cosmos endpoint.
Regards,
Oury
Hi @Ey-8423 Checking if you are still facing the same issue.?
Regards,
Oury
5 people are following this question.