Skip to content

Using the Python API

This section covers an overview of the Python API extension for Gaffer. Getting this extension up and running allows users to perform queries using Python code on existing graphs.

Tip

Please see the handy introduction to Python if you are new to the language.

What is the Python Extension?

Commonly referred to as gafferpy, this API provides similar querying capabilities to the REST API using Python. Fundamentally, it wraps the REST API allowing users to access almost all the features or end points available in the main REST API using Python rather than JSON.

Installation

Currently there isn't a release of gafferpy on pypi or other pip repository; however, the source code can still be cloned from the git repository and installed via pip. Please see the README in the gafferpy repository for full instructions.

How to Query a Graph

To get started with gafferpy you will need to import the module and connect to an existing graph. The connection should be the same address as where the REST API is running.

from gafferpy import gaffer as g
from gafferpy import gaffer_connector
gc = gaffer_connector.GafferConnector("http://localhost:8080/rest/latest")

Once connected you can access and run the same endpoints and operations as you would using the usual REST API but via their Python classes. The endpoints are accessed via the GafferConnector where users can then query graphs by executing Operation Chains.

Note

Some of the features of the full REST API may not be present in gafferpy so always check the reference guide first.

A simple Operation can be called via the execute_operation() function. As an example, the following will get all the elements in a graph:

elements = gc.execute_operation(
    operation =  g.GetAllElements()
)

An Operation Chain can be run using the execute_operation_chain() function. As an example, the following will get all the elements in a graph then count them.

count = gc.execute_operation_chain(
    operation_chain = g.OperationChain(
        operations=[
            g.GetAllElements(),
            g.Count()
        ]
    )
)

Along with running Operations you can of course optionally apply some filtering using a View. There is more detail on both filtering and operations on their respective pages.


Last update: December 8, 2023
Created: September 21, 2023