Graph Studio Tutorial (6/6)

Labels and Properties

Pablo Sierra
4 min readAug 17, 2022

Previous chapter,

In previous chapter We used a very basic PGQL syntax with our first graph, but now we can create more complex graphs structures.

← PGQL — Basic Graph Patterns — |

Labels and Properties

Remember, labels are used to define the kind of node or the kind of edge in the graph and with properties we can model the characteristics.

For example, in a financial graph, we can have three kind of nodes as Persons, Accounts, and Companies, same way, We can model their relationships or kind of edges as owner, transactions and worksFor.

To demo this in with the python API, first, create a new notebook:

Open a new %python-gpx cell and add labels over nodes and edges using the python API with .add_label(label), something like:

%python-pgx# graph builder
builder = session.create_graph_builder()
# node - person
nikita = builder.add_vertex(1).add_label("Person")
# node - account
account_1000 = builder.add_vertex(2).add_label("Account")
# node - company
tesla = builder.add_vertex(3).add_label("Company")
# edge - owner
builder.add_edge(account_1000, nikita).set_label("owner")
# edge - worksFor
builder.add_edge(nikita, tesla).set_label("worksFor")
# optional
if session.get_graph("financial"):
financial.close()
# graph
financial = builder.build("financial")
financial

The only new are the label and the graph named now as financial, others parts are pretty familiar.

Later, you can chain properties in a similar way using python API with .set_property(key, val) , in this case, you must provide two arguments, It is, the key string to specify “the kind of” and their value with any valid python data type.

For example, you can add the amount for every transaction like:

builder.add_edge(account_8021, account_1001).set_label(“transaction”).set_property(“amount”, 1500)

To summarize and see the big picture of this, we can build a new financial_transactions graph:

And run a PGQL query in a new %pgql-pgx. cell:

A tip to analyze this the graph above could be visualize the labels name and keys.

To do this, you can look for the settings icon in the left upper side of the cell:

There move on to the Customization section and in the Labeling select the Name as Vertex Label and label in the Edge Label.

And wallah, now the graph is more self explanatory:

You can explore more customization options if you want!

Now with those new features, We can build more complex queries in PGQL too.

Use labels to filter PGQL Queries

Note, now We can specify in the PGQL notation the kind of node or edge we want with their label.

/* Who persons has accounts? */
SELECT p, a, o
FROM MATCH (p:Person) <-[o:owner]- (a:Account)
ON financial_transactions
/* Who companies has accounts? */
SELECT c, a, o
FROM MATCH (c:Company) <-[o:owner]- (a:Account)
ON financial_transactions

Writing a PGQL Query with SQL Functions (Aggregation)

We can explore a variety of hand SQL functions ready to use and answers analytics questions.

/* COUNT */
SELECT COUNT(t) AS total
FROM financial_transactions
MATCH ()-[t:transaction]->()
/* SUM */
SELECT SUM(t.amount) AS total_amount
FROM financial_transactions
MATCH ()-[t:transaction]->()
/* AVG * /
SELECT AVG(t.amount) AS avg_amount
FROM financial_transactions
MATCH ()-[t:transaction]->()

Analyze the structure of the graph

Finally we can explore queries to understand the graph by self, I mean, now we now exactly the graphs structure, but, near in the future with more complex graphs structures It is more difficult to now.

/* How many types of vertices are in the graph? */
SELECT labels(n) AS node_labels, COUNT(*) AS total
FROM MATCH (n)
ON financial_transactions
GROUP BY labels(n)
ORDER BY total DESC
/* How many types of edges are in the graph?*/
SELECT label(e) AS edge_labels, COUNT(*) AS total
FROM MATCH () -[e]-> ()
ON financial_transactions
GROUP BY label(e)
ORDER BY total DESC

What Next?

Well my friend,

This is the end of this tutorial and I hope you really enjoy this, if you want please add your like and subscribe for new content.

Coming soon,

I hope finish to write a new tutorial to Find Circular Payment Chains using graphs, It is plenty used for fraud prevention applications in financial services and graph modeling from relational data models.

In the meantime you can explore:

--

--

Pablo Sierra

Solution Architect with expertise in artificial intelligence (AI), analytics, and Oracle Cloud solutions.