Named Views

AnzoGraph includes support for creating views. Views enable users to simplify logic by hiding the underlying complexity of the data or SPARQL operations, combine data from one or more graphs or other views, or mask sensitive information from some users. You can create virtual views, where AnzoGraph stores only the view definition, or materialized views, where AnzoGraph stores a copy of the data that the view creates as well as the view definition. This topic provides information about creating and using views.

Note: AnzoGraph also enables you to create named query definitions. See Named Queries for more information.

There are two ways to create a named view, depending on whether you want to create a view definition that is saved in the triplestore and can be referenced in various queries or whether you want to write a view inline to be referenced once in the query that immediately follows the inline view definition. This topic provides instructions for creating each type of named view:

Creating and Saving a Named View for Reuse

Use the following syntax to create a view and save the view definition for future use:


Include the OR REPLACE keywords when you want to replace a previously defined view with the same name. Include the MATERIALIZED keyword if you want AnzoGraph to store a copy of the data that the view constructs. If you exclude MATERIALIZED, AnzoGraph stores only the view definition.

To reference a view in subsequent queries, use view_name as a graph URI in FROM clauses or GRAPH patterns. For example:

FROM <view_name>
WHERE { ?s ?p ?o . }


FROM <tickit>
FROM NAMED <view_name>
  ?person <birthday> ?bday .
  GRAPH <view_name> { ?person <age> ?age. }

Creating a Named View Inline for One-Time Use

If you want to create a named view on-the-fly to reference in a query that you are writing, you can include a WITH clause to define a named view at the beginning of that query.

Note: AnzoGraph does not save the view definition for named views that are defined in a WITH clause. The named view can only be referenced in the query that immediately follows the WITH clause; it is not available to use in subsequent queries. To create a named view whose definition is persisted and can be referenced in future queries, use the CREATE OR REPLACE syntax.

WITH Syntax

WITH ( VIEW <name> AS construct_query )
     [ ( ... ) ]

Where construct_query is the query that constructs the view that you want to name and reference as a graph URI in the main query. You can define multiple named views in one WITH clause. For example, the WITH clause below defines a view named friends. The friends view is listed as a graph in the FROM clause in the main query:

( VIEW <friends> AS 
CONSTRUCT { ?s <friend> ?friend }
WHERE { GRAPH <tickit> {
  ?s <friend> ?friend .
  filter (?s = <person1> || ?s = <person2>)
FROM <friends>
WHERE { ?s ?p ?o }
s       | p      | o
person1 | friend | person20018
person1 | friend | person11678
person1 | friend | person12081
person1 | friend | person12316
person1 | friend | person11549
person1 | friend | person13826
person1 | friend | person26733
person1 | friend | person3005
person1 | friend | person27710
person1 | friend | person29554
person1 | friend | person14472
73 rows


The example queries in this section run against the AnzoGraph sample Tickit data set, which captures sales activity for a fictional Tickit website where people buy and sell tickets for sporting events, shows, and concerts. You can load and explore this data set. For more information, see Working with the Tickit Data.

In the sample Tickit data set, the sales1 data includes values for the following properties or predicates:

FROM <tickit>
  <sales1> ?p ?o .
10 rows

A sales manager might want to create a view so that the sales team can review ticket sales data without viewing the commission paid to their team members. This query creates a view that suppresses the commission values for sales1:

CREATE VIEW <no-commission> AS
CONSTRUCT {?s ?p ?o}
WHERE { GRAPH <tickit> {
  ?s ?p ?o .
  FILTER(?p != <commission>)
  FILTER(?s = <sales1>)

Querying the sales1 data using the new view shows the following results:

SELECT ?p ?o
FROM <no-commission>
WHERE { ?s ?p ?o . }
p                                               | o
buyerid                                         |          person21191
listid                                          |             listing1
pricepaid                                       |           728.000000
sellerid                                        |          person36861
eventid                                         |            event7872
qtysold                                         |                    4 |                sales
saletime                                        | 2008-02-18T02:36:48Z
dateid                                          |             date1875
9 rows

The example below creates a materialized view called "ages." The view constructs a new age predicate and calculates the approximate age value for each person in the sample Tickit data set.

CONSTRUCT { ?person <age> ?age . }
WHERE { GRAPH <tickit> {
  { SELECT ?person ((YEAR(?date))-(YEAR(xsd:dateTime(?birthdate))) AS ?age)
    WHERE {
      ?person <birthday> ?birthdate .
      BIND(xsd:dateTime(NOW()) AS ?date)

Running the following query against the view, shows the approximate age of each person.

FROM <ages>
WHERE { ?s ?p ?o . }
s           | p   | o
person40149 | age | 39
person30658 | age | 23
person6893  | age | 30
person12131 | age | 22
person33027 | age | 69
person24690 | age | 55
person9306  | age | 76
person4808  | age | 54
person45368 | age | 25
person34994 | age | 59
10 rows
Related Topics