question

Ey-8423 avatar image
0 Votes"
Ey-8423 asked OuryBa-MSFT commented

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

OuryBa-MSFT avatar image
0 Votes"
OuryBa-MSFT answered OuryBa-MSFT commented

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.

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

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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)


0 Votes 0 ·

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

0 Votes 0 ·

Hi @Ey-8423 Checking if you are still facing the same issue.?

Regards,
Oury

0 Votes 0 ·
Show more comments