1.5. Capabilities of the Client¶
Any Python script that interacts with the xGT server, whether it is run within a Jupyter Notebook or is run as a stand-alone program, usually begins by establishing a connection to the server. This connection carries user identity information that is used by the server for access control. Once connected, there are a variety of interactions that can be done:
Request the state of the server:
Request some form of data movement:
bringing data into some data structure
sending data out to a file or a Python client
dropping a data structure, thereby freeing the memory held by it
Request some computation be carried out:
Many computations are read-only in that they search through existing data for patterns without altering the existing data. In this scenario, a new table is created to hold the “answers” discovered in the data.
Some computations are read-write in that they scan over the existing data and (optionally) request updates to the existing data in the form of:
modifications to graph component (vertex or edge) properties
deletion of some graph components
creation and insertion of some graph components
1.5.1. Mixed-language Programming¶
In order to request certain computations be carried out by the server, it is necessary to mix both TQL and Python into your script. This is done by writing Python code that creates strings holding TQL queries, which are then passed to the server.
Suppose this TQL query is desired in a more generalized manner, based on some Python variables holding the value 13
and the column property1
:
MATCH (a)-[e:EdgeFrame]->(b)
WHERE a <> b AND e.property1 < 13
RETURN COUNT(*)
One could build up the TQL query and ask the server to run it using a Python function:
def count_edges_with_small_property_values(server, property_name, threshold):
q = """
MATCH (a)-[e:EdgeFrame]->(b)
WHERE a <> b AND e.{prop} < $threshold
RETURN COUNT(*)""".format(prop = property_name)
job = server.run_job(q, parameters = { "threshold" : threshold })
return int(job.get_data(0,1)[0][0])
In this case, the property name and threshold to use are arguments to the Python function that are substituted into the TQL query with their actual values. Note that the property name is substituted using Python string substitution, while the threshold is substituted using a TQL feature for parametric queries (see Cypher Parameters).