Querying using Gremlin

Connecting to PuppyGraph and making graph queries in Gremlin

PuppyGraph supports Gremlin, the graph query language developed by Apache TinkerPop.

Interactive Query UI

The Gremlin query tool offers a notebook-like user experience, complemented by the ability to visualize query results.

To access it, click the Gremlin Query tab on the Query page.

Gremlin Console

PuppyGraph provides an interactive CLI for Gremlin queries.

To access it, click the Gremlin Console tab on the Query page.

Graph Notebook

Graph Notebook is an open-source tool that enables users to interact with and visualize graph databases directly within a Jupyter Notebook environment.

PuppyGraph comes with a bundled Graph Notebook. To access it, click the Graph Notebook tab on the Query page. A new page will open with the Graph Notebook in it.

It comes with an example.ipynb file with example queries.

Client Drivers

PuppyGraph supports connecting and executing queries through client drivers of the following programming languages:

Python

Install the Gremlin Python Driver:

pip install gremlinpython

The following is an example of connecting to PuppyGraph and executing a query.

from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

# Establish a Gremlin Server connection to PuppyGraph.
g = traversal().with_remote(
    DriverRemoteConnection('ws://localhost:8182/gremlin', 'g', 
                           username='puppygraph', password='puppygraph123'))
                           
# Get all vertices in the graph.
vertices = g.V().value_map().to_list()
print("All vertices in the graph:")
print(vertices)

Go

Install the Gremlin Go Driver:

go get "github.com/apache/tinkerpop/gremlin-go/v3/driver"

The following is an example of connecting to PuppyGraph with the driver and executing a query.

package main

import (
	"fmt"
	"github.com/apache/tinkerpop/gremlin-go/v3/driver"
)

func main() {
	// Establish a connection to the PuppyGraph database.
	driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
	if err != nil {
		fmt.Println("Error creating connection:", err)
		return
	}
	defer driverRemoteConnection.Close() // Ensure closure of connection after execution

	// Create a graph traversal source.
	g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)

	// Get all vertices from the graph.
	fmt.Println("All vertices in the graph:")
	results, err := g.V().ValueMap().ToList()
	if err != nil {
		fmt.Println("Error retrieving vertices:", err)
		return
	}
	for _, r := range results {
		fmt.Println(r.GetString())
	}
}

Java

Install the Gremlin Java Driver:

The example uses Maven. You need to add the following dependencies to the pom.xml file of your project.

<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>gremlin-core</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>gremlin-driver</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>gremlin-server</artifactId>
    <version>3.6.0</version>
</dependency>

The following is an example of connecting to PuppyGraph and executing a query.

import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;

public class Example {
    public static void main(String[] args) throws Exception {
        // Connect to PuppyGraph database using a Gremlin Server.
        Cluster cluster = Cluster.build().addContactPoint("localhost").create();
        Client client = cluster.connect();

        // Create a graph traversal source for executing queries.
        GraphTraversalSource g = AnonymousTraversalSource.traversal()
                .withRemote(DriverRemoteConnection.using(client, "g"));

        // Get all vertices in the graph.
        System.out.println("All vertices in the graph:");
        System.out.println(g.V().valueMap().toList());
        System.out.println();

        // Close all opened resources to free up system resources.
        g.close();
        client.close();
        cluster.close();
    }
}

Javascript

Install the Gremlin Javascript Driver:

npm install gremlin

The following is an example of connecting to PuppyGraph and executing a query.

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

// Establish a connection to the graph database using Gremlin Server.
const endpoint = 'ws://localhost:8182/gremlin';
const graph = new Graph();
const connection = new DriverRemoteConnection(endpoint);
const g = graph.traversal().withRemote(connection);

// Get all vertices from the graph.
g.V().toList()
    .then(data => {
        console.log(data);
        return connection.close();
    })
    .then(() => {
        console.log('Connection closed');
    })
    .catch(error => {
        console.error('Error:', error);
        connection.close();
    });

The following link is a code repository where you can learn more examples of gremlin queries.

Client Applications

Gremlin Console (Apache TinkerPop)

PuppyGraph also supports the Gremlin Console provided by Apache TinkerPop.

You need to modify the remote.yaml file in the conf directory to use the IP and port (by default 8182) of the PuppyGraph server.

In the Gremlin Console, run the following commands to connect to PuppyGraph:

:remote connect tinkerpop.server conf/remote.yaml
:remote console

Last updated