Skip to content

Latest commit

 

History

History
141 lines (89 loc) · 5.16 KB

create-mongodb-java.md

File metadata and controls

141 lines (89 loc) · 5.16 KB
title description services author ms.service ms.component ms.custom ms.devlang ms.topic ms.date ms.author
Azure Cosmos DB: Build a console app with Java and the MongoDB API
Presents a Java code sample you can use to connect to and query the Azure Cosmos DB MongoDB API
cosmos-db
slyons
cosmos-db
cosmosdb-mongo
quick start connect, mvc
java
quickstart
05/10/2017
sclyon

Azure Cosmos DB: Build a MongoDB API console app with Java and the Azure portal

[!div class="op_single_selector"]

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

This quick start demonstrates how to create an Azure Cosmos DB MongoDB API account, document database, and collection using the Azure portal. You'll then build and deploy a console app built on the MongoDB Java driver.

Prerequisites

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

  • 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)

[!INCLUDE quickstarts-free-trial-note] [!INCLUDE cosmos-db-emulator-mongodb]

Create a database account

[!INCLUDE mongodb-create-dbaccount]

Add a collection

Name your new database, db, and your new collection, coll.

[!INCLUDE cosmos-db-create-collection]

Clone the sample application

Now let's clone a MongoDB API app from github, set the connection string, and run it. You'll see how easy it is to work with data programmatically.

  1. Open a command prompt, create a new folder named git-samples, then close the command prompt.

    md "C:\git-samples"
  2. Open a git terminal window, such as git bash, and use the cd command to change to the new folder to install the sample app.

    cd "C:\git-samples"
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-mongodb-java-getting-started.git
  4. Then open the code in your favorite editor.

Review the code

This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to Update your connection string.

The following snippets are all taken from the Program.java file.

  • The DocumentClient is initialized.

    MongoClientURI uri = new MongoClientURI("FILLME");`
    
    MongoClient mongoClient = new MongoClient(uri);            
  • A new database and collection are created.

    MongoDatabase database = mongoClient.getDatabase("db");
    
    MongoCollection<Document> collection = database.getCollection("coll");
  • Some documents are inserted using MongoCollection.insertOne

    Document document = new Document("fruit", "apple")
    collection.insertOne(document);
  • Some queries are performed using MongoCollection.find

    Document queryResult = collection.find(Filters.eq("fruit", "apple")).first();
    System.out.println(queryResult.toJson());    	

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app.

  1. From the Account, select Quick Start, select Java, then copy the connection string to your clipboard

  2. Open the Program.java file, replace the argument to the MongoClientURI constructor with the connection string. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

Run the console app

  1. Run mvn package in a terminal to install required npm modules

  2. Run mvn exec:java -D exec.mainClass=GetStarted.Program in a terminal to start your Java application.

You can now use Robomongo / Studio 3T to query, modify, and work with this new data.

Review SLAs in the Azure portal

[!INCLUDE cosmosdb-tutorial-review-slas]

Clean up resources

[!INCLUDE cosmosdb-delete-resource-group]

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a collection using the Data Explorer, and run a console app. You can now import additional data to your Cosmos DB account.

[!div class="nextstepaction"] Import MongoDB data into Azure Cosmos DB