Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create and Query Labeled Property Graphs in AnzoGraph

DZone 's Guide to

How to Create and Query Labeled Property Graphs in AnzoGraph

This article will explain how to create property graphs in AnzoGraph, including loading and inserting properties, and how to query your property graphs.

· Database Zone ·
Free Resource

AnzoGraph is a native, massively parallel processing (MPP) graph OLAP database purpose-built for interactive data warehousing analytics and graph analytics. AnzoGraph supports not only semantic graphs, but also labeled property graphs using the RDF* proposed W3C standard. This article will explain how to create property graphs in AnzoGraph, including loading and inserting properties, and how to query your property graphs.

Create a Labeled Property Graph

AnzoGraph users can include triple properties to further define the relationships in graphs. For example, you can define properties that describe start and end dates, the provenance/lineage of the data, the weight, score, or veracity of the data, and more.

1. Define Properties in Turtle Files

In AnzoGraph you define properties for graphs in Turtle load files or via SPARQL INSERT queries. This section provides instructions and examples for creating labeled property graphs. To define a property for a triple in a Turtle load file, wrap the triplet in double arrow heads ( << >> ), and then specify the property name and value for that triple at the end of the triplet:

<< subject predicate object >> property_URI property_value .

The example TTL file contents below define triples for a person, person3. The graph that will contain this person data captures sales activity for a fictional e-commerce website, we’ll call “Tickit,” where people buy and sell tickets for sporting events, shows, and concerts.

The data consists of person, venue, category, date, event, listing, and sales classes. The sample TTL file includes properties that further define the like, dislike, and friend relationships in the person triples. The file adds a weight property to define how much person3 likes or dislikes certain types of events, and the file adds startDate and endDate properties to friend predicates to define the start and end dates of friendships:

<person3>
  rdf:type <person>;
  <card> "4984932249480735"^^xsd:long;
  <birthday> "1963-07-02"^^xsd:date;
  <ssn> 503703220;
  <firstname> "Lars";
  <lastname> "Ratliff";
  <city> "High Point";
  <state> "NY";
  <email> "amet.faucibus.ut@condimentumegetvolutpat.ca";
  <phone> "(624) 767-2465".
<<<person3> <like> "sports">> <weight> 8.
<<<person3> <like> "rock">> <weight> 9.
<<<person3> <like> "musicals">> <weight> 4.
<<<person3> <dislike> "theatre">> <weight> 5.
<<<person3> <dislike> "jazz">> <weight> 9.
<<<person3> <dislike> "opera">> <weight> 10.
<<<person3> <friend> <person8563>>> <startDate> "1990-01-04"^^xsd:date.
<<<person3> <friend> <person38436>>> <startDate> "2000-04-27"^^xsd:date.
<<<person3> <friend> <person11979>>> <startDate> "2004-11-09"^^xsd:date.
<<<person3> <friend> <person11979>>> <endDate> "2012-07-17"^^xsd:date.
<person3> <friend> <person8639>,<person18536>,<person42975>,<person47376>,
<person1692>,<person2556>,<person11979>,<person20860>,<person21259>,<person26586>,<person27529>,<person31735>,<person36264>,<person38436>,<person42306>,<person42975>.

The above example contains both compact and long Turtle notation. When defining properties in files, tuples that contain properties must include the complete reference triple (subject, predicate, and object). Properties cannot be added to triples specified in compact notation. In addition, specify one property per triplet. To define multiple properties for the same triplet, list the triplet multiple times. For example, the following lines in the example above define two properties (startDate and endDate) for the person3 friend person11979 triple:

<<<person3> <friend> <person11979>>> <startDate> "2004-11-09"^^xsd:date .
<<<person3> <friend> <person11979>>> <endDate> "2012-07-17"^^xsd:date .

2. Define Properties in INSERT Queries

AnzoGraph users can create property graphs using INSERT and INSERT DATA SPARQL update operations to insert triples and properties or add properties to existing triples. To define properties in INSERT statements, use the same syntax as Turtle files: wrap triplets in double arrow heads ( << >>), and then specify the property name and value for that triple at the end of the triplet.

<< subject predicate object >> property_URI property_value .

For example, the INSERT DATA statement below adds weight properties to the like and dislike predicates for person3:

INSERT DATA { GRAPH <tickit> {
  <<<person3> <dislike> "jazz">> <weight> 9 .
  <<<person3> <dislike> "theatre">> <weight> 5 .
  <<<person3> <dislike> "opera">> <weight> 10 .
  <<<person3> <like> "sports">> <weight> 8 .
  <<<person3> <like> "rock">> <weight> 9 .
  <<<person3> <like> "musicals">> <weight> 4 .
 }
}

Query Labeled Property Graphs

Now, to return properties and their values when analyzing data sets, include the following property graph syntax in graph and triple patterns:

<< subject predicate object >> property_URI property_value .

For example, the following query returns the properties that were defined in the INSERT DATA query above:

SELECT *
FROM <tickit>
WHERE {
  <<?person ?p ?likes_or_dislikes>> ?property ?value.
  FILTER(?p=<like> || ?p=<dislike>)
}
ORDER BY ?p

person  | p       | likes_or_dislikes | property | value
--------+---------+-------------------+----------+-------
person3 | dislike | jazz              | weight   |     9
person3 | dislike | opera             | weight   |    10
person3 | dislike | theatre           | weight   |     5
person3 | like    | rock              | weight   |     9
person3 | like    | musicals          | weight   |     4
person3 | like    | sports            | weight   |     8
6 rows

The example below returns a list of the properties in the Tickit graph and lists the number of times each property is referenced in the graph:

SELECT ?property (COUNT(?property) AS ?times_used)
FROM <tickit>
WHERE {
  <<?s ?p ?o>> ?property ?value
}
GROUP BY ?property
ORDER BY desc(?times_used)

property  | times_used
----------+------------
startDate |    1729764
endDate   |     173036
weight    |          6
3 rows
Topics:
graph databases ,property graphs ,anzograph ,data warehousing analytics ,graph analytics ,sparsql ,tutorial ,property graph tutorial ,how to create a property graph ,insert queries

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}