Create and Save Views

AnzoGraph DB 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 DB stores only the view definition, or materialized views, where AnzoGraph DB 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.

AnzoGraph DB also enables you to create named query definitions. See Save Queries for Reuse 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:

Create and Save a View for Reuse

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

CREATE [ OR REPLACE ] [ MATERIALIZED ] VIEW <view_uri> AS
CONSTRUCT { 
    query
}

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 DB to store a copy of the data that the view constructs. If you exclude MATERIALIZED, AnzoGraph DB stores only the view definition.

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

SELECT *
FROM <view_uri>
WHERE { ?s ?p ?o . }

Or

SELECT *
FROM <tickit>
FROM NAMED <view_uri>
WHERE { 
  ?person <birthday> ?bday .
  GRAPH <view_uri> { ?person <age> ?age. }
}

Create a 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.

AnzoGraph DB 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 <view_uri> 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:

WITH 
( VIEW <friends> AS 
CONSTRUCT { ?s <friend> ?friend }
WHERE { GRAPH <tickit> {
  ?s <friend> ?friend .
  filter (?s = <person1> || ?s = <person2>)
  }
 }
)
SELECT *
FROM <friends>
WHERE { ?s ?p ?o }
ORDER BY ?s
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

Examples

The example queries in this section run against the AnzoGraph DB 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 SPARQL and the Tickit Data.

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

SELECT ?p
FROM <tickit>
WHERE { 
  <sales1> ?p ?o .
}
p
-------------------------------------------------
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
dateid
sellerid
eventid
commission
saletime
listid
pricepaid
qtysold
buyerid
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
http://www.w3.org/1999/02/22-rdf-syntax-ns#type |                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.

CREATE MATERIALIZED VIEW <ages> AS
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.

SELECT *
FROM <ages>
WHERE { ?s ?p ?o . }
LIMIT 10
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