3.3. Using the Python LibraryΒΆ

The xGT client is a python library, xgt, that connects to a running xGT server. This section gives a brief overview of using xgt to perform graph analytics with xGT. Section Graph Thinking provides a tutorial on graph analysis with xGT, including how to think of and model your data as a graph.

The first step is to connect to the server which returns a Connection object. This object is used to drive the server to perform graph analysis. To learn more about connecting to a server, see Connecting to a Server:

import xgt
server = xgt.Connection(host = 'localhost')

The second step is to create a new graph by creating one or more vertex and edge frames. The example below defines an empty graph on the server:

vertex_frame = server.create_vertex_frame(name = 'graph__VertexFrame',
                                          schema = [['id', xgt.INT]],
                                          key = 'id')

edge_frame = server.create_edge_frame(name = 'graph__EdgeFrame',
                                      source = 'graph__VertexFrame',
                                      target = 'graph__VertexFrame',
                                      schema = [['source_id', xgt.INT],
                                                ['target_id', xgt.INT],
                                                ['value', xgt.TEXT]],
                                      source_key = 'source_id',
                                      target_key = 'target_id')

In this example, vertex_frame and edge_frame are frame proxy objects that can be used to load data into the graph and obtain basic information about it. To read more about frame types in xGT as well as the API for creating and modifying frames, see Graph Data Model: Frames and Namespaces.

The third step is to load data into the graph. Data Movement discusses loading data in detail.

The fourth step is to run queries to search for graph patterns. Queries are Python strings following the Trovares Query Language (TQL) syntax:

query = """
MATCH (a)-[e1:graph__EdgeFrame]->(b)<-[e2:graph__EdgeFrame]-(c)
WHERE e1.value = e2.value
RETURN COUNT(*)
"""
job = server.run_job(query)

For more information on running queries, see Job Management and History. Sections Introduction to the Trovares Query Language (TQL) and TQL for Cypher Users discuss TQL.