Azure Cosmos DB: Create a graph 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 simple graph database using the Azure portal tools for Azure Cosmos DB. This quickstart also shows you how to quickly create a Java console app using a graph database using the OSS Gremlin Java driver. The instructions in this quickstart can be followed on any operating system that is capable of running Java. This quickstart familiarizes you with creating and modifying graphs in either the UI or programmatically, whichever is your preference.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

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 graph database, you need to create a Gremlin (Graph) database account with Azure Cosmos DB.

  1. In a new browser window, sign in to the Azure portal.

  2. Click New > Databases > Azure Cosmos DB.

    Azure portal "Databases" pane

  3. In the New account page, enter the settings for the new Azure Cosmos DB account.

    Setting Suggested value Description
    ID Enter a unique name Enter a unique name to identify this Azure Cosmos DB account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique but identifiable ID.

    The ID can contain only lowercase letters, numbers, and the hyphen (-) character, and it must contain 3 to 50 characters.
    API Gremlin (graph) The API determines the type of account to create. Azure Cosmos DB provides five APIs to suits the needs of your application: SQL (document database), Gremlin (graph database), MongoDB (document database), Azure Table, and Cassandra, each which currently require a separate account.

    Select Gremlin (graph) because in this quickstart you are creating a graph that is queryable using Gremlin syntax.

    Learn more about the Graph API
    Subscription Your subscription Select Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource group Enter the same unique name as provided above in ID Enter a new resource-group name for your account. For simplicity, you can use the same name as your ID.
    Location Select the region closest to your users Select geographic location in which to host your Azure Cosmos DB account. Use the location that's closest to your users to give them the fastest access to the data.
    Enable geo-redundancy Leave blank This creates a replicated version of your database in a second (paired) region. Leave this blank.
    Pin to dashboard Select Select this box so that your new database account is added to your portal dashboard for easy access.

    Then click Create.

    The new account blade for Azure Cosmos DB

  4. The account creation takes a few minutes. During account creation the portal displays the Deploying Azure Cosmos DB tile.

    The Azure portal Notifications pane

    Once the account is created, the Congratulations! Your Azure Cosmos DB account was created page is displayed.

Add a graph

You can now use the Data Explorer tool in the Azure portal to create a graph database.

  1. Click Data Explorer > New Graph.

    The Add Graph area is displayed on the far right, you may need to scroll right to see it.

    The Azure portal Data Explorer, Add Graph page

  2. In the Add graph page, enter the settings for the new graph.

    Setting Suggested value Description
    Database ID sample-database Enter sample-database as the name for the new database. Database names must be between 1 and 255 characters, and cannot contain / \ # ? or a trailing space.
    Graph ID sample-graph Enter sample-graph as the name for your new collection. Graph names have the same character requirements as database IDs.
    Storage Capacity Fixed (10 GB) Change the value to Fixed (10 GB). This value is the storage capacity of the database.
    Throughput 400 RUs Change the throughput to 400 request units per second (RU/s). If you want to reduce latency, you can scale up the throughput later.
    Partition key Leave blank For the purpose of this quickstart, leave the partition key blank.
  3. Once the form is filled out, click OK.

Clone the sample application

Now let's switch to working with code. Let's clone a Graph 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-graph-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 in the C:\git-samples\azure-cosmos-db-graph-java-getting-started\src\GetStarted folder. Otherwise, you can skip ahead to Update your connection string.

  • The Gremlin Client is initialized from the configuration in src/remote.yaml.

    cluster = Cluster.build(new File("src/remote.yaml")).create();
    ...
    client = cluster.connect();
    
  • A series of Gremlin steps are executed using the client.submit method.

    ResultSet results = client.submit(gremlin);
    
    CompletableFuture<List<Result>> completableFutureResults = results.all();
    List<Result> resultList = completableFutureResults.get();
    
    for (Result result : resultList) {
        System.out.println(result.toString());
    }
    

Update your connection information

Now go back to the Azure portal to get your connection information and copy it into the app. These settings enable your app to communicate with your hosted database.

  1. In the Azure portal, click Keys.

    Copy the first portion of the URI value.

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

  2. Open the src/remote.yaml file and paste the value over $name$ in hosts: [$name$.graphs.azure.com].

    Line 1 of remote.yaml should now look similar to

    hosts: [test-graph.graphs.azure.com]

  3. In the Azure portal, use the copy button to copy the PRIMARY KEY and paste it over $masterKey$ in password: $masterKey$.

    Line 4 of remote.yaml should now look similar to

    password: 2Ggkr662ifxz2Mg==

  4. Change line 3 of remote.yaml from

    username: /dbs/$database$/colls/$collection$

    to

    username: /dbs/sample-database/colls/sample-graph

  5. Save the remote.yaml file.

Run the console app

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

    cd "C:\git-samples\azure-cosmos-db-graph-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 your Java application.

    The terminal window displays the vertices being added to the graph. Once the program stops, switch back to the Azure portal in your internet browser.

Review and add sample data

You can now go back to Data Explorer and see the vertices added to the graph, and add additional data points.

  1. Click Data Explorer, expand sample-graph, click Graph, and then click Apply Filter.

    Create new documents in Data Explorer in the Azure portal

  2. In the Results list, notice the new users added to the graph. Select ben and notice that he's connected to robin. You can move the vertices around by dragging and dropping, zoom in and out by scrolling the wheel of your mouse, and expand the size of the graph with the double-arrow.

    New vertices in the graph in Data Explorer in the Azure portal

  3. Let's add a few new users. Click the New Vertex button to add data to your graph.

    Create new documents in Data Explorer in the Azure portal

  4. Enter a label of person.

  5. Click Add property to add each of the following properties. Notice that you can create unique properties for each person in your graph. Only the id key is required.

    key value Notes
    id ashley The unique identifier for the vertex. If you don't specify an id, one is generated for you.
    gender female
    tech java

    Note

    In this quickstart we create a non-partitioned collection. However, if you create a partitioned collection by specifying a partition key during the collection creation, then you need to include the partition key as a key in each new vertex.

  6. Click OK. You may need to expand your screen to see OK on the bottom of the screen.

  7. Click New Vertex again and add an additional new user.

  8. Enter a label of person.

  9. Click Add property to add each of the following properties:

    key value Notes
    id rakesh The unique identifier for the vertex. If you don't specify an id, one is generated for you.
    gender male
    school MIT
  10. Click OK.

  11. Click Apply Filter with the default g.V() filter to display all the values in the graph. All of the users now show in the Results list.

    As you add more data, you can use filters to limit your results. By default, Data Explorer uses g.V() to retrieve all vertices in a graph. You can change it to a different graph query, such as g.V().count(), to return a count of all the vertices in the graph in JSON format. If you changed the filter, change the filter back to g.V() and click Apply Filter to display all the results again.

  12. Now we can connect rakesh and ashley. Ensure ashley in selected in the Results list, then click the edit button next to Targets on lower right side. You may need to widen your window to see the Properties area.

    Change the target of a vertex in a graph

  13. In the Target box type rakesh, and in the Edge label box type knows, and then click the check.

    Add a connection between ashley and rakesh in Data Explorer

  14. Now select rakesh from the results list and see that ashley and rakesh are connected.

    Two vertices connected in Data Explorer

    That completes the resource creation part of this tutorial.You can continue to add vertexes to your graph, modify the existing vertexes, or change the queries. Now let's review the metrics Azure Cosmos DB provides, and then clean up the resources.

Review SLAs in the Azure portal

The throughput, storage, availability, latency, and consistency of the resources in your account are monitored in the Azure portal. Let's take a quick look at these metrics.

  1. Click Metrics in the navigation menu.

    Metrics in the Azure portal

  2. Click through each of the tabs so you're aware of the metrics Azure Cosmos DB provides.

    Each chart that's associated with the Azure Cosmos DB Service Level Agreements (SLAs) provides a line that shows if any of the SLAs have been violated. Azure Cosmos DB makes monitoring your SLAs transparent with this suite of metrics.

    Azure Cosmos DB metrics suite

Clean up resources

If you're not going to continue to use this app, delete all resources created by this quickstart with the following steps so you don't incur any charges:

  1. Select Resource groups and then click Delete resource group.

    Metrics in the Azure portal

  2. Type the name of the resource group to delete, and then click Delete.

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a graph using the Data Explorer, and run an app. You can now build more complex queries and implement powerful graph traversal logic using Gremlin.