Thursday, March 19, 2020

Connecting to Cosmos DB Gremlin API from Gremlin Console


     Graph Databases have been popular lately. You can use Azure Cosmos DB as your  Graph database source by selecting Gremlin API. Gremlin programing language is developed by Apache TinkerPop of the Apache Software Foundation. I will show you how to connect to Cosmos DB Gremlin API from TinkerPop Gremlin console.

     You can download the latest version of Gremlin console from here. The latest version was 3.4.6 when I wrote this post. I was able to connect to the Cosmos DB by using the versions 3.4.3 and 3.4.6.  You can run the console from Linux or Windows, I will focus on the Windows version here but Linux version should work the same way. You must have Java SDK 8 to run this console. Latest version of Java SDK does not work with this console.

    After you install Java runtime and TinkerPop Console, you need to add your database information to configuration file. You can find the configuration files in conf folder. I created a new file named remote.yaml in this folder



     You need the following information from Cosmos DB for the configuration file.
  • Graph Database Name
  • Graph Collection Name
  • Master Key
  • Url
     If you want to use Emulator as your source, keep enableSsl property false. If you are trying to connect to Azure Cosmos DB, enableSsl must be true. Here is my configuration file. Pay attention to the yellow text. You want to be sure that you are using the right serializer for Cosmos DB.

hosts: [x.gremlin.cosmosdb.azure.com] 
port: 443 
username: /dbs/ProductsDemo/colls/Products
password: masterKey 
connectionPool: { enableSsl: true} 
serializer: { 
className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0
config: { serializeResultToString: true }}

Hosts, password and port comes from the Keys section of your Cosmos DB


Username comes from your database name and collection name


     If you are trying to connect to the Emulator, your port number will be different. Also, for Emulator you need to change enableSsl to false. TinkerTop console's serializer class name can be different, you want to be sure that you are using GraphSonMessageSerializer class! If not, you will get error when you try to query the Cosmos DB Gremlin API.

     We should be ready to run TinkerPop console application now. Change your directory to Bin folder and type gremlin. You should see the following screen.


     TinkerPop is ready to receive commands from us. We are not connected to Cosmos DB yet in this point. To connect to the Cosmos DB, we need to load the configuration file by the following command.

:remote connect tinkerpop.server conf/remote.yaml

Here is the screenshot of the command and result.


   Following commond will force all scripts to run in Cosmos DB.

:remote console

     Now, I am ready to pass Gremlin Language to Cosmos DB, Let's count how many Nodes I have in my database.

    You should be able to run any Gremlin script in this point; I am listing all nodes in the following example.



No comments:

Post a Comment