16.8. Examples of Linked Data Views
16.8.1. Simple Mapping Example -- Northwind Linked Data View
Here is example of the basic Northwind Linked Data Views deployment. The sequence of operations is very common for adding SPARQL access to existing application.
There exist few important questions to answer. Who should have access to data behind Linked Data View? Should someone have access to other sorts of RDF data but not to the new View? What are applications that should be interoperable with the new RDF data source? Are there any applications that produce similar data but that data sould be kept apart from data made by view? How to ensure that deployment the view will not cause problems for other applications?
First of all, we decide whether the default web service endpoint should have access to the data in question. If it should then we have to grant SELECT privileges to the account "SPARQL" that is used for the default endpoint; if it should not but some custom edpoint should then grant to the owner account of that account. Granting access is less trivial that it is usual. On one hand, those who can make SQL SELECT statements on application's tables can also make SPARQL queries on Linked Data View over that tables, because it makes SQL inside. On the other hand, those who do not intend to query that data at all may get unexpected "permission denied" errors on queries that worked fine before adding an Linked Data View. If SPARQL compiler can not prove that the query can not access data from the view then it will generate SQL code that will access tables behind the view. In some cases permission problems should be resolved by creating Linked Data View in a separate RDF storage . In this example, data are public:
use DB; GRANT SELECT ON "Demo"."demo"."Products" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Suppliers" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Shippers" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Categories" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Customers" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Employees" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Orders" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Order_Details" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Countries" TO "SPARQL"; GRANT SELECT ON "Demo"."demo"."Provinces" TO "SPARQL";
Interoperability is the next question. The example is not interoperable with anything so in can provide data of any form, a real application will probably use some ontology from external source. Sometimes data should be converted from internal application's representation to something different (such as metric to imperial or ATT country code to two-character country id); sometimes composed IRIs should follow special rules; function-based IRI classes may help in that cases. As this is the first example, only plain format-string-based IRI classes are used.
We should also ensure that data generated by the new view will not be accidentally mixed with other data of the database. For that purpose the example will use a unique graph name that includes both application name and host name. In addition, the script will drop declarations that might remain from a previous run of the same script. The script is executed many times during the development so erasing previous version is worth writing. It will report an error if there's nothing to erase but it's better than unpredictable errors due to writing new declarations over existing ones.
Note | |
---|---|
Making graph name unique for every host is not needed if the application is supposed to be "local" and nobody will access more than one installation of the application. If this is the case, use some fixed graph IRI, not necessarily starting with hostname at all; this is much more convenient for querying because you don't have to calculate the graph name in each query. With fixed graph in use, it is still possible to clone the Linked Data View to map to a unique graph as soon as the application become "public" and requires merging data from many installations. |
SPARQL drop quad map graph iri("http://^{URIQADefaultHost}^/Northwind") ; SPARQL drop quad map virtrdf:NorthwindDemo ;
The {URIQADefaultHost} macro is replaced with the value of DefaultHost parameter of [URIQA] section of configuration file. The IRI is written as iri("http://^{URIQADefaultHost}^/Northwind") , not as <http://^{URIQADefaultHost}^/Northwind> because macro of this sort works only inside SPARQL string values.
Now it's safe to create IRI classes needed for the view. If these classes are used only in the view we define then it is safe to
create all of them in a single statement. If some of them are used across multiple declarations then errors may occur. it is impossible
to redefine an IRI class that is in use; the compiler will try to avoid reporting errors by checking whether the new declaration is
identical to the existing one and by trying garbage collection in hope that the IRI class is used only in garbage, but errors may occur
anyway. Thus it is better to declare "shared" IRI classes by individual statements and group together only "private" IRI
classes of a view. If a "class redefinition" error occurs in the middle of a group then "undefined class" errors may
occur after because the processing of the group was interrupted before rest of group was not executed. When in trouble, try
DB.DBA.RDF_AUDIT_METADATA
procedure.
SPARQL create iri class northwind:Category "http://^{URIQADefaultHost}^/Northwind/Category/%d#this" (in category_id integer not null) . create iri class northwind:Shipper "http://^{URIQADefaultHost}^/Northwind/Shipper/%d#this" (in shipper_id integer not null) . create iri class northwind:Supplier "http://^{URIQADefaultHost}^/Northwind/Supplier/%d#this" (in supplier_id integer not null) . create iri class northwind:Product "http://^{URIQADefaultHost}^/Northwind/Product/%d#this" (in product_id integer not null) . create iri class northwind:Customer "http://^{URIQADefaultHost}^/Northwind/Customer/%U#this" (in customer_id varchar not null) . create iri class northwind:Employee "http://^{URIQADefaultHost}^/Northwind/Employee/%U%U%d#this" (in employee_firstname varchar not null, in employee_lastname varchar not null, in employee_id integer not null) . create iri class northwind:Order "http://^{URIQADefaultHost}^/Northwind/Order/%d#this" (in order_id integer not null) . create iri class northwind:CustomerContact "http://^{URIQADefaultHost}^/Northwind/CustomerContact/%U#this" (in customer_id varchar not null) . create iri class northwind:OrderLine "http://^{URIQADefaultHost}^/Northwind/OrderLine/%d/%d#this" (in order_id integer not null, in product_id integer not null) . create iri class northwind:Province "http://^{URIQADefaultHost}^/Northwind/Province/%U/%U#this" (in country_name varchar not null, in province_name varchar not null) . create iri class northwind:Country "http://^{URIQADefaultHost}^/Northwind/Country/%U#this" (in country_name varchar not null) . create iri class northwind:Flag "http://^{URIQADefaultHost}^%U#this" (in flag_path varchar not null) . create iri class northwind:dbpedia_iri "http://dbpedia.org/resource/%U" (in uname varchar not null) . create iri class northwind:EmployeePhoto "http://^{URIQADefaultHost}^/DAV/VAD/demo/sql/EMP%d#this" (in emp_id varchar not null) . create iri class northwind:CategoryPhoto "http://^{URIQADefaultHost}^/DAV/VAD/demo/sql/CAT%d#this" (in category_id varchar not null) . ;
One IRI class per subject type; format strings begin with same host but different directory names so this will let the
compiler to guess the type of subject by the text of IRI. Most of declarations are
bijections
and may get option (bijection)
hint but these
format strings are so simple that the compiler may understand it by itself. (northwind:Employee
is not a
bijection because sprintf_inverse
will be unable to split the
tail of IRI string and find the boundary between first and last name.)
The final operation is extending the default quad storage with new tree of quad map patterns.
SPARQL prefix northwind: <http://demo.openlinksw.com/schemas/northwind#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix sioc: <http://rdfs.org/sioc/ns#> prefix foaf: <http://xmlns.com/foaf/0.1/> prefix owl: <http://www.w3.org/2002/07/owl#> prefix wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#> alter quad storage virtrdf:DefaultQuadStorage from Demo.demo.Products as products from Demo.demo.Suppliers as suppliers from Demo.demo.Shippers as shippers from Demo.demo.Categories as categories from Demo.demo.Customers as customers from Demo.demo.Employees as employees from Demo.demo.Orders as orders from Demo.demo.Order_Details as order_lines from Demo.demo.Countries as countries from Demo.demo.Provinces as provinces where (^{suppliers.}^.Country = ^{countries.}^.Name) where (^{customers.}^.Country = ^{countries.}^.Name) where (^{employees.}^.Country = ^{countries.}^.Name) where (^{orders.}^.ShipCountry = ^{countries.}^.Name) { create virtrdf:NorthwindDemo as graph iri ("http://^{URIQADefaultHost}^/Northwind") option (exclusive) { northwind:CustomerContact (customers.CustomerID) a foaf:Person as virtrdf:CustomerContact-foaf_Person . northwind:CustomerContact (customers.CustomerID) a northwind:CustomerContact as virtrdf:CustomerContact-CustomerContact; foaf:name customers.ContactName as virtrdf:CustomerContact-contact_name ; foaf:phone customers.Phone as virtrdf:CustomerContact-foaf_phone ; northwind:is_contact_at northwind:Customer (customers.CustomerID) as virtrdf:CustomerContact-is_contact_at ; northwind:country northwind:Country (customers.Country) as virtrdf:CustomerContact-country ; rdfs:isDefinedBy northwind:customercontact_iri (customers.CustomerID) ; rdfs:isDefinedBy northwind:CustomerContact (customers.CustomerID) . northwind:Country (customers.Country) northwind:is_country_of northwind:CustomerContact (customers.CustomerID) as virtrdf:CustomerContact-is_country_of . northwind:Product (products.ProductID) a northwind:Product as virtrdf:Product-ProductID ; northwind:has_category northwind:Category (products.CategoryID) as virtrdf:Product-product_has_category ; northwind:has_supplier northwind:Supplier (products.SupplierID) as virtrdf:Product-product_has_supplier ; northwind:productName products.ProductName as virtrdf:Product-name_of_product ; northwind:quantityPerUnit products.QuantityPerUnit as virtrdf:Product-quantity_per_unit ; northwind:unitPrice products.UnitPrice as virtrdf:Product-unit_price ; northwind:unitsInStock products.UnitsInStock as virtrdf:Product-units_in_stock ; northwind:unitsOnOrder products.UnitsOnOrder as virtrdf:Product-units_on_order ; northwind:reorderLevel products.ReorderLevel as virtrdf:Product-reorder_level ; northwind:discontinued products.Discontinued as virtrdf:Product-discontinued ; rdfs:isDefinedBy northwind:product_iri (products.ProductID) ; rdfs:isDefinedBy northwind:Product (products.ProductID) . northwind:Category (products.CategoryID) northwind:category_of northwind:Product (products.ProductID) as virtrdf:Product-category_of . northwind:Supplier (products.SupplierID) northwind:supplier_of northwind:Product (products.ProductID) as virtrdf:Product-supplier_of . northwind:Supplier (suppliers.SupplierID) a northwind:Supplier as virtrdf:Supplier-SupplierID ; northwind:companyName suppliers.CompanyName as virtrdf:Supplier-company_name ; northwind:contactName suppliers.ContactName as virtrdf:Supplier-contact_name ; northwind:contactTitle suppliers.ContactTitle as virtrdf:Supplier-contact_title ; northwind:address suppliers.Address as virtrdf:Supplier-address ; northwind:city suppliers.City as virtrdf:Supplier-city ; northwind:dbpedia_city northwind:dbpedia_iri(suppliers.City) as virtrdf:Supplier-dbpediacity ; northwind:region suppliers.Region as virtrdf:Supplier-region ; northwind:postalCode suppliers.PostalCode as virtrdf:Supplier-postal_code ; northwind:country northwind:Country(suppliers.Country) as virtrdf:Supplier-country ; northwind:phone suppliers.Phone as virtrdf:Supplier-phone ; northwind:fax suppliers.Fax as virtrdf:Supplier-fax ; northwind:homePage suppliers.HomePage as virtrdf:Supplier-home_page ; rdfs:isDefinedBy northwind:supplier_iri (suppliers.SupplierID) ; rdfs:isDefinedBy northwind:Supplier (suppliers.SupplierID) . northwind:Country (suppliers.Country) northwind:is_country_of northwind:Supplier (suppliers.SupplierID) as virtrdf:Supplier-is_country_of . northwind:Category (categories.CategoryID) a northwind:Category as virtrdf:Category-CategoryID ; northwind:categoryName categories.CategoryName as virtrdf:Category-home_page ; northwind:description categories.Description as virtrdf:Category-description ; foaf:img northwind:CategoryPhoto(categories.CategoryID) as virtrdf:Category-categories.CategoryPhoto ; rdfs:isDefinedBy northwind:category_iri (categories.CategoryID) ; rdfs:isDefinedBy northwind:Category (categories.CategoryID) . northwind:CategoryPhoto(categories.CategoryID) a northwind:CategoryPhoto as virtrdf:Category-categories.CategoryPhotoID ; rdfs:isDefinedBy northwind:categoryphoto_iri (categories.CategoryID) ; rdfs:isDefinedBy northwind:CategoryPhoto(categories.CategoryID) . northwind:Shipper (shippers.ShipperID) a northwind:Shipper as virtrdf:Shipper-ShipperID ; northwind:companyName shippers.CompanyName as virtrdf:Shipper-company_name ; northwind:phone shippers.Phone as virtrdf:Shipper-phone ; rdfs:isDefinedBy northwind:shipper_iri (shippers.ShipperID) ; rdfs:isDefinedBy northwind:Shipper (shippers.ShipperID) . northwind:Customer (customers.CustomerID) a northwind:Customer as virtrdf:Customer-CustomerID2 ; a foaf:Organization as virtrdf:Customer-CustomerID ; foaf:name customers.CompanyName as virtrdf:Customer-foaf_name ; northwind:companyName customers.CompanyName as virtrdf:Customer-company_name ; northwind:has_contact northwind:CustomerContact (customers.CustomerID) as virtrdf:Customer-contact ; northwind:country northwind:Country (customers.Country) as virtrdf:Customer-country ; northwind:contactName customers.ContactName as virtrdf:Customer-contact_name ; northwind:contactTitle customers.ContactTitle as virtrdf:Customer-contact_title ; northwind:address customers.Address as virtrdf:Customer-address ; northwind:city customers.City as virtrdf:Customer-city ; northwind:dbpedia_city northwind:dbpedia_iri(customers.City) as virtrdf:Customer-dbpediacity ; northwind:region customers.Region as virtrdf:Customer-region ; northwind:PostalCode customers.PostalCode as virtrdf:Customer-postal_code ; foaf:phone customers.Phone as virtrdf:Customer-foaf_phone ; northwind:phone customers.Phone as virtrdf:Customer-phone ; northwind:fax customers.Fax as virtrdf:Customer-fax ; rdfs:isDefinedBy northwind:customer_iri (customers.CustomerID) ; rdfs:isDefinedBy northwind:Customer (customers.CustomerID) . northwind:Country (customers.Country) northwind:is_country_of northwind:Customer (customers.CustomerID) as virtrdf:Customer-is_country_of . northwind:Employee (employees.FirstName, employees.LastName, employees.EmployeeID) a northwind:Employee as virtrdf:Employee-EmployeeID2 ; a foaf:Person as virtrdf:Employee-EmployeeID ; foaf:surname employees.LastName as virtrdf:Employee-foaf_last_name ; northwind:lastName employees.LastName as virtrdf:Employee-last_name ; foaf:firstName employees.FirstName as virtrdf:Employee-foaf_first_name ; northwind:firstName employees.FirstName as virtrdf:Employee-first_name ; foaf:title employees.Title as virtrdf:Employee-title ; northwind:titleOfCourtesy employees.TitleOfCourtesy as virtrdf:Employee-title_of_courtesy ; foaf:birthday employees.BirthDate as virtrdf:Employee-foaf_birth_date ; northwind:birthday employees.BirthDate as virtrdf:Employee-birth_date ; northwind:hireDate employees.HireDate as virtrdf:Employee-hire_date ; northwind:address employees.Address as virtrdf:Employee-address ; northwind:city employees.City as virtrdf:Employee-city ; northwind:dbpedia_city northwind:dbpedia_iri(employees.City) as virtrdf:Employee-dbpediacity ; northwind:region employees.Region as virtrdf:Employee-region ; northwind:postalCode employees.PostalCode as virtrdf:Employee-postal_code ; northwind:country northwind:Country(employees.Country) as virtrdf:Employee-country ; foaf:phone employees.HomePhone as virtrdf:Employee-home_phone ; northwind:extension employees.Extension as virtrdf:Employee-extension ; northwind:notes employees.Notes as virtrdf:Employee-notes ; northwind:reportsTo northwind:Employee(employees.FirstName, employees.LastName, employees.ReportsTo) where (^{employees.}^.ReportsTo = ^{employees.}^.EmployeeID) as virtrdf:Employee-reports_to ; foaf:img northwind:EmployeePhoto(employees.EmployeeID) as virtrdf:Employee-employees.EmployeePhoto ; rdfs:isDefinedBy northwind:employee_iri (employees.EmployeeID) ; rdfs:isDefinedBy northwind:Employee (employees.FirstName, employees.LastName, employees.EmployeeID) . northwind:EmployeePhoto(employees.EmployeeID) a northwind:EmployeePhoto as virtrdf:Employee-employees.EmployeePhotoId ; rdfs:isDefinedBy northwind:employeephoto_iri (employees.EmployeeID) ; rdfs:isDefinedBy northwind:EmployeePhoto (employees.EmployeeID) . northwind:Employee (employees.FirstName, employees.LastName, orders.EmployeeID) northwind:is_salesrep_of northwind:Order (orders.OrderID) where (^{orders.}^.EmployeeID = ^{employees.}^.EmployeeID) as virtrdf:Order-is_salesrep_of . northwind:Country (employees.Country) northwind:is_country_of northwind:Employee (employees.FirstName, employees.LastName, employees.EmployeeID) as virtrdf:Employee-is_country_of . northwind:Order (orders.OrderID) a northwind:Order as virtrdf:Order-Order ; northwind:has_customer northwind:Customer (orders.CustomerID) as virtrdf:Order-order_has_customer ; northwind:has_salesrep northwind:Employee (employees.FirstName, employees.LastName, orders.EmployeeID) where (^{orders.}^.EmployeeID = ^{employees.}^.EmployeeID) as virtrdf:Customer-has_salesrep ; northwind:has_employee northwind:Employee (employees.FirstName, employees.LastName, orders.EmployeeID) where (^{orders.}^.EmployeeID = ^{employees.}^.EmployeeID) as virtrdf:Order-order_has_employee ; northwind:orderDate orders.OrderDate as virtrdf:Order-order_date ; northwind:requiredDate orders.RequiredDate as virtrdf:Order-required_date ; northwind:shippedDate orders.ShippedDate as virtrdf:Order-shipped_date ; northwind:order_ship_via northwind:Shipper (orders.ShipVia) as virtrdf:Order-order_ship_via ; northwind:freight orders.Freight as virtrdf:Order-freight ; northwind:shipName orders.ShipName as virtrdf:Order-ship_name ; northwind:shipAddress orders.ShipAddress as virtrdf:Order-ship_address ; northwind:shipCity orders.ShipCity as virtrdf:Order-ship_city ; northwind:dbpedia_shipCity northwind:dbpedia_iri(orders.ShipCity) as virtrdf:Order-dbpediaship_city ; northwind:shipRegion orders.ShipRegion as virtrdf:Order-ship_region ; northwind:shipPostal_code orders.ShipPostalCode as virtrdf:Order-ship_postal_code ; northwind:shipCountry northwind:Country(orders.ShipCountry) as virtrdf:ship_country ; rdfs:isDefinedBy northwind:order_iri (orders.OrderID) ; rdfs:isDefinedBy northwind:Order (orders.OrderID) . northwind:Country (orders.ShipCountry) northwind:is_ship_country_of northwind:Order (orders.OrderID) as virtrdf:Order-is_country_of . northwind:Customer (orders.CustomerID) northwind:has_order northwind:Order (orders.OrderID) as virtrdf:Order-has_order . northwind:Shipper (orders.ShipVia) northwind:ship_order northwind:Order (orders.OrderID) as virtrdf:Order-ship_order . northwind:OrderLine (order_lines.OrderID, order_lines.ProductID) a northwind:OrderLine as virtrdf:OrderLine-OrderLines ; northwind:has_order_id northwind:Order (order_lines.OrderID) as virtrdf:order_lines_has_order_id ; northwind:has_product_id northwind:Product (order_lines.ProductID) as virtrdf:order_lines_has_product_id ; northwind:unitPrice order_lines.UnitPrice as virtrdf:OrderLine-unit_price ; northwind:quantity order_lines.Quantity as virtrdf:OrderLine-quantity ; northwind:discount order_lines.Discount as virtrdf:OrderLine-discount ; rdfs:isDefinedBy northwind:orderline_iri (order_lines.OrderID, order_lines.ProductID) ; rdfs:isDefinedBy northwind:OrderLine (order_lines.OrderID, order_lines.ProductID) . northwind:Order (orders.OrderID) northwind:is_order_of northwind:OrderLine (order_lines.OrderID, order_lines.ProductID) where (^{orders.}^.OrderID = ^{order_lines.}^.OrderID) as virtrdf:Order-is_order_of . northwind:Product (products.ProductID) northwind:is_product_of northwind:OrderLine (order_lines.OrderID, order_lines.ProductID) where (^{products.}^.ProductID = ^{order_lines.}^.ProductID) as virtrdf:Product-is_product_of . northwind:Country (countries.Name) a northwind:Country as virtrdf:Country-Type2 ; a wgs:SpatialThing as virtrdf:Country-Type ; owl:sameAs northwind:dbpedia_iri (countries.Name) ; northwind:name countries.Name as virtrdf:Country-Name ; northwind:code countries.Code as virtrdf:Country-Code ; northwind:smallFlagDAVResourceName countries.SmallFlagDAVResourceName as virtrdf:Country-SmallFlagDAVResourceName ; northwind:largeFlagDAVResourceName countries.LargeFlagDAVResourceName as virtrdf:Country-LargeFlagDAVResourceName ; northwind:smallFlagDAVResourceURI northwind:Flag(countries.SmallFlagDAVResourceURI) as virtrdf:Country-SmallFlagDAVResourceURI ; northwind:largeFlagDAVResourceURI northwind:Flag(countries.LargeFlagDAVResourceURI) as virtrdf:Country-LargeFlagDAVResourceURI ; wgs:lat countries.Lat as virtrdf:Country-Lat ; wgs:long countries.Lng as virtrdf:Country-Lng ; rdfs:isDefinedBy northwind:country_iri (countries.Name) ; rdfs:isDefinedBy northwind:Country (countries.Name) . northwind:Country (countries.Name) northwind:has_province northwind:Province (provinces.CountryCode, provinces.Province) where (^{provinces.}^.CountryCode = ^{countries.}^.Code) as virtrdf:Country-has_province . northwind:Province (provinces.CountryCode, provinces.Province) a northwind:Province as virtrdf:Province-Provinces ; northwind:has_country_code provinces.CountryCode as virtrdf:has_country_code ; northwind:provinceName provinces.Province as virtrdf:Province-ProvinceName ; rdfs:isDefinedBy northwind:province_iri (provinces.CountryCode, provinces.Province) ; rdfs:isDefinedBy northwind:Province (provinces.CountryCode, provinces.Province) . northwind:Province (provinces.CountryCode, provinces.Province) northwind:is_province_of northwind:Country (countries.Name) where (^{countries.}^.Code = ^{provinces.}^.CountryCode) as virtrdf:Province-country_of . }. }. ;
The created Linked Data View is sufficient for querying relational data via SPARQL but not for accessing data by dereferencing IRIs of subjects. Making IRIs dereferenceable requires configuring HTTP server; that is explained in second part of the example .