Skip to content

Latest commit

 

History

History
237 lines (154 loc) · 10.9 KB

create-documentdb-java.md

File metadata and controls

237 lines (154 loc) · 10.9 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.custom ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Create an Azure Cosmos DB document database with Java | Microsoft Docs | Microsoft Docs'
Presents a Java code sample you can use to connect to and query the Azure Cosmos DB DocumentDB API
cosmos-db
mimig1
jhubbard
89ea62bb-c620-46d5-baa0-eefd9888557c
cosmos-db
quick start connect, mvc, devcenter
na
java
quickstart
10/30/2017
mimig

Azure Cosmos DB: Create a document database using Java and the Azure portal

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. Using Azure Cosmos DB, you can quickly create and query managed document, table, and graph databases.

This quickstart creates a document database using the Azure portal tools for Azure Cosmos DB. This quickstart also shows you how to quickly create a Java console app using the DocumentDB Java API. The instructions in this quickstart can be followed on any operating system that is capable of running Java. By completing this quickstart you'll be familiar with creating and modifying document database resources in either the UI or programmatically, whichever is your preference.

Prerequisites

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

In addition:

  • Java Development Kit (JDK) 1.7+
    • On Ubuntu, run apt-get install default-jdk to install the JDK.
    • Be sure to set the JAVA_HOME environment variable to point to the folder where the JDK is installed.
  • Download and install a Maven binary archive
    • On Ubuntu, you can run apt-get install maven to install Maven.
  • Git
    • On Ubuntu, you can run sudo apt-get install git to install Git.

Create a database account

Before you can create a document database, you need to create a SQL (DocumentDB) database account with Azure Cosmos DB.

[!INCLUDE cosmos-db-create-dbaccount]

Add a collection

[!INCLUDE cosmos-db-create-collection]

Add sample data

You can now add data to your new collection using Data Explorer.

  1. Expand the Items collection, click Documents > New Document.

    Create new documents in Data Explorer in the Azure portal

  2. Now add a document to the collection with the following structure and click Save.

    {
        "id": "1",
        "category": "personal",
        "name": "groceries",
        "description": "Pick up apples and strawberries.",
        "isComplete": false
    }

    Copy in json data and click Save in Data Explorer in the Azure portal

  3. Create and save one more document where you change id to 2, and change the other properties as you see fit. Your new documents can have any structure you want as Azure Cosmos DB doesn't impose any schema on your data.

Query your data

You can now use queries in Data Explorer to retrieve and filter your data.

  1. See that by default, the query is set to SELECT * FROM c. This default query retrieves and displays all documents in the collection.

    Default query in Data Explorer is SELECT * FROM c

  2. Change the query by clicking the Edit Filter button, adding ORDER BY c._ts DESC to the query predicate box, and then clicking Apply Filter.

    Change the default query by adding ORDER BY c._ts DESC and clicking Apply Filter

This modified query lists the documents in descending order based on their time stamp, so now your second document is listed first. If you're familiar with SQL syntax, you can enter any of the supported SQL queries in this box.

That completes our work in Data Explorer. Before we move on to working with code, note that you can also use Data Explorer to create stored procedures, UDFs, and triggers to perform server-side business logic as well as scale throughput. Data Explorer exposes all of the built-in programmatic data access available in the APIs, but provides easy access to your data in the Azure portal.

Clone the sample application

Now let's switch to working with code. Let's clone a DocumentDB 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 git terminal window, such as git bash, and use the cd command to change to a folder to install the sample app.

    cd "C:\git-samples"
  2. 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-documentdb-java-getting-started.git

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. The snippets are all taken from the Program.java file installed in the C:\git-samples\azure-cosmos-db-documentdb-java-getting-started\src\GetStarted folder. Otherwise, you can skip ahead to Update your connection string.

  • DocumentClient initialization. The DocumentClient provides client-side logical representation for the Azure Cosmos DB database service. This client is used to configure and execute requests against the service.

    this.client = new DocumentClient("https://FILLME.documents.azure.com",
            "FILLME", 
            new ConnectionPolicy(),
            ConsistencyLevel.Session);
  • Database creation.

    Database database = new Database();
    database.setId(databaseName);
    
    this.client.createDatabase(database, null);
  • DocumentCollection creation.

    DocumentCollection collectionInfo = new DocumentCollection();
    collectionInfo.setId(collectionName);
    
    ...
    
    this.client.createCollection(databaseLink, collectionInfo, requestOptions);
  • Document creation by using the createDocument method.

    // Any Java object within your code can be serialized into JSON and written to Azure Cosmos DB
    Family andersenFamily = new Family();
    andersenFamily.setId("Andersen.1");
    andersenFamily.setLastName("Andersen");
    // More properties
    
    String collectionLink = String.format("/dbs/%s/colls/%s", databaseName, collectionName);
    this.client.createDocument(collectionLink, family, new RequestOptions(), true);
  • SQL queries over JSON are performed using the queryDocuments method.

    FeedOptions queryOptions = new FeedOptions();
    queryOptions.setPageSize(-1);
    queryOptions.setEnableCrossPartitionQuery(true);
    
    String collectionLink = String.format("/dbs/%s/colls/%s", databaseName, collectionName);
    FeedResponse<Document> queryResults = this.client.queryDocuments(
        collectionLink,
        "SELECT * FROM Family WHERE Family.lastName = 'Andersen'", queryOptions);
    
    System.out.println("Running SQL query...");
    for (Document family : queryResults.getQueryIterable()) {
        System.out.println(String.format("\tRead %s", family));
    }

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. This enables your app to communicate with your hosted database.

  1. In the Azure portal, click Keys.

    Use the copy buttons on the right side of the screen to copy the top value, the URI.

    View and copy an access key in the Azure portal, Keys page

  2. Open the Program.java file from C:\git-samples\azure-cosmos-db-documentdb-java-getting-started\src\GetStarted folder.

  3. Paste the URI value from the portal over https://FILLME.documents.azure.com on line 45.

  4. Go back to portal and copy the PRIMARY KEY value as shown in the screenshot. Paste the PRIMARY KEY value from the portal over FILLME on line 46.

    The getStartedDemo method should now look similar to this:

    private void getStartedDemo() throws DocumentClientException, IOException {
        this.client = new DocumentClient("https://youraccountname.documents.azure.com:443/",
                "your-primary-key...RJhQrqQ5QQ==", 
                new ConnectionPolicy(),
                ConsistencyLevel.Session);
  5. Save the Program.java file.

Run the app

  1. In the git terminal window, cd to the azure-cosmos-db-documentdb-java-getting-started folder.

    cd "C:\git-samples\azure-cosmos-db-documentdb-java-getting-started"
    
  2. In the git terminal window, type mvn package to install the required Java packages.

  3. In the git terminal window, run mvn exec:java -D exec.mainClass=GetStarted.Program to start the Java application.

    The terminal window displays a notification that the FamilyDB database was created. Press a key to create the collection, then switch to the Data Explorer and you'll see that it now contains a FamilyDB database.

    Continue to press keys to create the documents and then perform a query.

    At the end of the program, all the resources from this app are deleted from your account so that you don't incur any charges.

    Console output

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, document database, and collection using the Data Explorer, and run an app to do the same thing programmatically. You can now import additional data into your Azure Cosmos DB collection.

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