Azure Cosmos DB Part 3— Gremlin API

photo credit: Graph database vs Relational database | by Tarun Manrai | Dev Genius | Medium
  • Built-in Data Explorer
  • Gremlin Console
  • Application in Python
  • Create Database
  • Update Database
  • Create Graph
  • Update Graph
  • Insert Data into Graph
  • Query Data from Graph

Built-in Data Explorer

Create Database and Create Graph

  • Head over to “Data Explorer” on the left of the blade
  • Click on “New Graph” → “New Graph”
  • Put down the desired name for new database and graph. The same as MongoDB and SQL API, you would also need to select whether to auto scale/manual scale the request units (RU).
  • vertices
g.addV('person').property('firstName', 'Thomas').property('lastName', 'Andersen').property('age', 44).property('userid', 1).property('pk', 'pk')
  • edges
g.V().hasLabel('person').has('firstName', 'Thomas').addE('knows').to(g.V().hasLabel('person').has('firstName', 'Mary Kay'))
g.V().hasLabel('person').has('firstName', 'Thomas').property('age', 45)
g.V().hasLabel('person').has('firstName', 'Thomas').outE('knows').inV().hasLabel('person')
g.V()

Gremlin Console

Installation

  • Download and install Java 8 from here
  • Download and install Gremlin Console from here
  • Head over to the path “<file path>\apache-tinkerpop-gremlin-console-3.4.10-bin\apache-tinkerpop-gremlin-console-3.4.10\conf”
  • There should be a file named “remote-secure.yaml”. Create a copy of the file and named “remote-secure.yaml.bak”
  • Go back to the original file “remote-secure.yaml”. Within the file, replace the last few lines with below
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
##############################################################
# This configuration is meant to have Gremlin Server return
# text serialized objects. The server will toString()
# results giving a view into how scripts are executing.
#
# This file will work with:
# - gremlin-server-secure.yaml
##############################################################
hosts: [<azure cosmos db name>.gremlin.cosmos.azure.com]
port: 443
username: /dbs/<database name>/colls/<graph name>
password: your_primary_key
connectionPool: {
enableSsl: true
}
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV2d0, config: { serializeResultToString: true }}
  • Head over to the path “<file path>\apache-tinkerpop-gremlin-console-3.4.10-bin\apache-tinkerpop-gremlin-console-3.4.10\bin”
  • For Windows OS, launch the console with “gremlin.bat”
  • Execute the following in the console
#To read the remote server configuration
- :remote connect tinkerpop.server conf/remote-secure.yaml
#To use the remote server console
- :remote console
  • Create Database and Update Database
  • Create Graph and Update Graph
  • Insert Data into Graph
  • Query Data from Graph

Application in Python

Prerequisite Actions:

  • Install Python 3.6+ from here
  • Install PIP with “python3 -m pip3 — version”
#Check the version with
- python3 --version
- pip3 --version
#Change to the right directory
- cd "azure-cosmos-..."
#Edit the file
- nano connect.py
#Search for the string "client.Client"
- ctrl + w
- type in "client.Client"
- "alt + w" to keep on finding the next match
#Update the information within the file
...
client = client.Client('wss://<azure cosmos DB account name>.gremlin.cosmosdb.azure.com:443/','g',
username="/dbs/<database name>/colls/<graph name>",
password="<primary key>")
...
#Install the requirements
- pip3 install -r requirements.txt
#Execute the code
python3 connect.py

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jonathan

Jonathan

Learning new things about Kubernetes every day. Hopefully, the learning notes could help people on the same journey!