Coder Social home page Coder Social logo

azure-cosmos-db-mongodb-java-getting-started's Introduction

page_type languages products description urlFragment
sample
java
azure
Azure Cosmos DB is a fully managed globally distributed, multi-model database service, transparently replicating your data across any number of Azure regions.
azure-cosmos-db-mongodb-java-getting-started

Developing a Java app using Azure Cosmos DB for MongoDB API

Azure Cosmos DB is a fully managed globally distributed, multi-model database service, transparently replicating your data across any number of Azure regions. You can elastically scale throughput and storage, and take advantage of fast, single-digit-millisecond data access using the API of your choice backed by 99.999 SLA. This sample shows you how to use the Azure Cosmos DB for MongoDB API to store and access data from a Java application.

Running this sample

  • Before you can run this sample, you must have the following prerequisites:

    • An active Azure account. If you don't have one, you can sign up for a free account. Alternatively, you can use the Azure Cosmos DB Emulator for this tutorial.
    • JDK 1.7+ (Run apt-get install default-jdk if you don't have JDK)
    • Maven (Run apt-get install maven if you don't have Maven)
  • Then, clone this repository using git clone [email protected]:azure-samples/azure-cosmosdb-for-mongodb-api-java-getting-started.git

  • Next, substitute the endpoint and authorization key in Program.java with your Cosmos DB account's values.

  • From a command prompt or shell, run mvn package to compile and resolve dependencies.

  • From a command prompt or shell, run mvn exec:java -D exec.mainClass=GetStarted.Program to run the application.

About the code

The code included in this sample is intended to get you quickly started with a Java application that connects to Azure Cosmos DB for MongoDB API.

More information

azure-cosmos-db-mongodb-java-getting-started's People

Contributors

arramac avatar microsoftopensource avatar msftgits avatar sivethe avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

azure-cosmos-db-mongodb-java-getting-started's Issues

Sample not work

  • Windows 10 Pro (17134.81)
  • CosmosDB emulator (1.22.0.0)
  • Java JDK 10 (jdk-10.0.1 & jre-10.0.1)

I tried it as Sample, but it got an error
MongoClientURI put the emulator's MongoAPI connector
I am in a state like the following, but will not the sample move if I do not change anything?
However, Azure's regular CosmosDB to MongoApi works, but I do not want to skip local tests

C:\work\azure-cosmos-db-mongodb-java-getting-started>mvn exec:java -D exec.mainClass=GetStarted.Program
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< GetStarted:GetStarted >------------------------
[INFO] Building Program 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ GetStarted ---
6月 12, 2018 9:30:09 午後 com.mongodb.diagnostics.logging.JULLogger log
情報: Cluster created with settings {hosts=[localhost:10255], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
6月 12, 2018 9:30:09 午後 com.mongodb.diagnostics.logging.JULLogger log
情報: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:10255, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
6月 12, 2018 9:30:09 午後 com.mongodb.diagnostics.logging.JULLogger log
情報: Exception in monitor thread while connecting to server localhost:10255
com.mongodb.MongoSocketWriteException: Exception sending message
        at com.mongodb.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:465)
        at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:208)
        at com.mongodb.connection.CommandHelper.sendMessage(CommandHelper.java:89)
        at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
        at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85)
        at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45)
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
        at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at java.base/sun.security.ssl.Alerts.getSSLException(Alerts.java:198)
        at java.base/sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1974)
        at java.base/sun.security.ssl.Handshaker.fatalSE(Handshaker.java:345)
        at java.base/sun.security.ssl.Handshaker.fatalSE(Handshaker.java:339)
        at java.base/sun.security.ssl.ClientHandshaker.checkServerCerts(ClientHandshaker.java:1968)
        at java.base/sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1777)
        at java.base/sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:264)
        at java.base/sun.security.ssl.Handshaker.processLoop(Handshaker.java:1098)
        at java.base/sun.security.ssl.Handshaker.processRecord(Handshaker.java:1026)
        at java.base/sun.security.ssl.SSLSocketImpl.processInputRecord(SSLSocketImpl.java:1137)
        at java.base/sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1074)
        at java.base/sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
        at java.base/sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1402)
        at java.base/sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:733)
        at java.base/sun.security.ssl.AppOutputStream.write(AppOutputStream.java:67)
        at com.mongodb.connection.SocketStream.write(SocketStream.java:75)
        at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:204)
        ... 7 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
        at java.base/sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:290)
        at java.base/sun.security.validator.Validator.validate(Validator.java:264)
        at java.base/sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:343)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:226)
        at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:133)
        at java.base/sun.security.ssl.ClientHandshaker.checkServerCerts(ClientHandshaker.java:1947)
        ... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
        at java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
        at java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)
        at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
        ... 25 more

[WARNING]
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:10255, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException (BaseCluster.java:377)
    at com.mongodb.connection.BaseCluster.selectServer (BaseCluster.java:104)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource. (ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource. (ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getWriteConnectionSource (ClusterBinding.java:68)
    at com.mongodb.operation.OperationHelper.withConnection (OperationHelper.java:411)
    at com.mongodb.operation.MixedBulkWriteOperation.execute (MixedBulkWriteOperation.java:168)
    at com.mongodb.operation.MixedBulkWriteOperation.execute (MixedBulkWriteOperation.java:74)
    at com.mongodb.Mongo.execute (Mongo.java:845)
    at com.mongodb.Mongo$2.execute (Mongo.java:828)
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest (MongoCollectionImpl.java:550)
    at com.mongodb.MongoCollectionImpl.insertOne (MongoCollectionImpl.java:317)
    at com.mongodb.MongoCollectionImpl.insertOne (MongoCollectionImpl.java:307)
    at GetStarted.Program.main (Program.java:36)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:844)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.114 s
[INFO] Finished at: 2018-06-12T21:30:40+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project GetStarted: An exception occured while executing the Java class. Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:10255, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

CosmosDB not connection but MongoDB connection working

Hello,

I am trying with Azure Cosmo DB emulator, when i run the sample code with the connection string changes it Cosmos DB is not connecting, but MongoDB working fine.

Note: Same cosmos DB emulator working in other sample languages like Python and nodejs

I tried with the driver 3.4.2 and 3.7.1 and 3.12.10 - but no luck. Below are the error message.

[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ GetStarted ---
Jan 16, 2022 8:41:45 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:10255], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jan 16, 2022 8:41:45 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Jan 16, 2022 8:42:05 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server localhost:10255
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:530)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:421)
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.base/java.net.SocketInputStream.socketRead0(Native Method)
at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
at com.mongodb.connection.SocketStream.read(SocketStream.java:84)
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
... 9 more

[WARNING]
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:10255, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {java.net.SocketTimeoutException: Read timed out}}]
at com.mongodb.connection.BaseCluster.getDescription (BaseCluster.java:167)
at com.mongodb.Mongo.getConnectedClusterDescription (Mongo.java:885)
at com.mongodb.Mongo.createClientSession (Mongo.java:877)
at com.mongodb.Mongo$3.getClientSession (Mongo.java:866)
at com.mongodb.Mongo$3.execute (Mongo.java:834)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest (MongoCollectionImpl.java:1025)
at com.mongodb.MongoCollectionImpl.executeInsertOne (MongoCollectionImpl.java:513)
at com.mongodb.MongoCollectionImpl.insertOne (MongoCollectionImpl.java:493)
at com.mongodb.MongoCollectionImpl.insertOne (MongoCollectionImpl.java:487)
at GetStarted.Program.main (Program.java:37)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:834)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 47.438 s
[INFO] Finished at: 2022-01-16T20:42:31+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project GetStarted: An exception occured while executing the Java class. Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:10255, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {java.net.SocketTimeoutException: Read timed out}}] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.