Quickstart: Create a graph database in Azure Cosmos DB using PHP and the Azure portal
This quickstart shows how to use PHP and the Azure Cosmos DB Gremlin API to build a console app by cloning an example from GitHub. This quickstart also walks you through the creation of an Azure Cosmos DB account by using the web-based Azure portal.
Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can quickly create and query document, table, key-value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.
Create a database account
Before you can create a graph database, you need to create a Gremlin (Graph) database account with Azure Cosmos DB.
In a new browser window, sign in to the Azure portal.
Click Create a resource > Databases > Azure Cosmos DB.
In the Create Azure Cosmos DB Account page, enter the settings for the new Azure Cosmos DB account.
Setting Value Description Subscription Your subscription Select the Azure subscription that you want to use for this Azure Cosmos DB account. Resource Group Create new
Then enter the same unique name as provided in ID
Select Create new. Then enter a new resource-group name for your account. For simplicity, use the same name as your ID. Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique ID.
The ID can use only lowercase letters, numbers, and the hyphen (-) character. It must be between 3 and 31 characters in length.
API Gremlin (graph) The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core(SQL) for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. Currently, you must create a separate account for each API.
Select Gremlin (graph) because in this quickstart you are creating a table that works with the Gremlin API.
Learn more about the Graph API.
Location Select the region closest to your users Select a geographic location 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.
Select Review+Create. You can skip the Network and Tags section.
The account creation takes a few minutes. Wait for the portal to display the Congratulations! Your Azure Cosmos DB account was created page.
Add a graph
You can now use the Data Explorer tool in the Azure portal to create a graph database.
Click Data Explorer > New Graph.
The Add Graph area is displayed on the far right, you may need to scroll right to see it.
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) Leave the default value of 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.
Once the form is filled out, click OK.
Clone the sample application
Now let's switch to working with code. Let's clone a Gremlin API app from GitHub, set the connection string, and run it. You'll see how easy it is to work with data programmatically.
Open a command prompt, create a new folder named git-samples, then close the command prompt.
Open a git terminal window, such as git bash, and use the
cdcommand to change to a folder to install the sample app.
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-php-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 connect.php file in the C:\git-samples\azure-cosmos-db-graph-php-getting-started\ folder. Otherwise, you can skip ahead to Update your connection string.
connectionis initialized in the beginning of the
connect.phpfile using the
$db = new Connection([ 'host' => '<your_server_address>.graphs.azure.com', 'username' => '/dbs/<db>/colls/<coll>', 'password' => 'your_primary_key' ,'port' => '443' // Required parameter ,'ssl' => TRUE ]);
A series of Gremlin steps are executed using the
$query = "g.V().drop()"; ... $result = $db->send($query); $errors = array_filter($result); }
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.
In the Azure portal, click Keys.
Copy the first portion of the URI value.
connect.phpfile and in line 8 paste the URI value over
The connection object initialization should now look similar to the following code:
$db = new Connection([ 'host' => 'testgraphacct.graphs.azure.com', 'username' => '/dbs/<db>/colls/<coll>', 'password' => 'your_primary_key' ,'port' => '443' // Required parameter ,'ssl' => TRUE ]);
If your graph database account was created on or after December 20, 2017, change
graphs.azure.comin the host name to
usernameparameter in the Connection object with your database and graph name. If you used the recommended values of
sample-graph, it should look like the following code:
'username' => '/dbs/sample-database/colls/sample-graph'
The entire Connection object should look like the following code snippet at this time:
$db = new Connection([ 'host' => 'testgraphacct.graphs.azure.com', 'username' => '/dbs/sample-database/colls/sample-graph', 'password' => 'your_primary_key', 'port' => '443' // Required parameter ,'ssl' => TRUE ]);
In the Azure portal, use the copy button to copy the PRIMARY KEY and paste it over
your_primary_keyin the password parameter.
The Connection object initialization should now look like the following code:
$db = new Connection([ 'host' => 'testgraphacct.graphs.azure.com', 'username' => '/dbs/sample-database/colls/sample-graph', 'password' => '2Ggkr662ifxz2Mg==', 'port' => '443' // Required parameter ,'ssl' => TRUE ]);
Run the console app
In the git terminal window,
cdto the azure-cosmos-db-graph-php-getting-started folder.
In the git terminal window, use the following command to install the required PHP dependencies.
In the git terminal window, use the following command to start the PHP application.
The terminal window displays the vertices being added to the graph.
If you experience timeout errors, check that you updated the connection information correctly in Update your connection information, and also try running the last command again.
Once the program stops, press Enter, then 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.
Click Data Explorer, expand sample-graph, click Graph, and then click Apply Filter.
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.
Let's add a few new users. Click the New Vertex button to add data to your graph.
Enter a label of person.
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
In this quickstart you 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.
Click OK. You may need to expand your screen to see OK on the bottom of the screen.
Click New Vertex again and add an additional new user.
Enter a label of person.
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
Click the Apply Filter button 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.
Now you can connect rakesh and ashley. Ensure ashley is 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.
In the Target box type rakesh, and in the Edge label box type knows, and then click the check.
Now select rakesh from the results list and see that ashley and rakesh are connected.
That completes the resource creation part of this quickstart. 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.
Click Metrics in the navigation menu.
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.
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:
In the Azure portal, select Resource groups on the far left, and then select the resource group you created.
If the left menu is collapsed, click to expand it.
In the new window select the resource group, and then click Delete resource group.
In the new window, type the name of the resource group to delete, and then click Delete.
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.
We’d love to hear your thoughts. Choose the type you’d like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.