Warning:
This wiki has been archived and is now read-only.

Example of SQL-based RDB2RDF Mapping

From RDB2RDF
Jump to: navigation, search

A sample relational schema:

DEPT table

 deptno  NUMBER       UNIQUE
 dname   VARCHAR2(30)
 loc     VARCHAR2(100)

EMP table

 empno  NUMBER        PRIMARY KEY
 ename  VARCHAR2(100)
 job    VARCHAR2(30)
 deptno NUMBER        REFERENCES DEPT(deptno)
 etype  VARCHAR2(30)

LIKES table -- attr-val table (sample row: "xyz.com/100" "likesPlaying" "Soccer")

 id       VARCHAR2(4000)
 likeType VARCHAR2(30)
 likedObj VARCHAR2(100)


A mapping specification (for mapping the above relational schema to RDF):

This specification maps the example relational schema to RDF using Turtle syntax. This example includes:

o bNode instance ID and IRI instance ID
o inverse expression
o rdf:type columns 
o computed properties
o destination graphs (either at class-level or at instance-level)
o foreign key constraint
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix emp: <http://example.com/emp#> .
@prefix dept: <http://example.com/dept#> .
@prefix xyz: <http://example.com/xyz#> .
<#classMap1>
    a rr:ClassMap;
    rr:class xyz:dept;
    rr:graph xyz:DeptGraph;
    rr:sqlDefString """
       Select 'dept' || deptno AS deptId
            , deptno
            , dname
            , loc
         from dept
    """;
   rr:instanceIdMap [ a rr:bNodeMap; rr:column "deptId";
                      rr:InverseExpr "substr({alias}.deptId,length('dept')+1)"];
   rr:propertyMap [ a rr:LiteralMap; rr:Datatype xsd:positiveInteger;
                    rr:property dept:deptno; rr:column "deptno" ];
   rr:propertyMap [ rr:property dept:name; rr:column "dname" ];
   rr:propertyMap [ rr:property dept:location; rr:column "loc" ];
   rr:propertyMap [ rr:property dept:COMPANY; rr:constantValue "XYZ Corporation" ];
   .
<#classMap2>
   a rr:ClassMap;
   rr:class xyz:emp;
   rr:sqlDefString """
       Select 'xyz.com/emp/' || empno AS empURI
            , empno
            , ename
            , 'xyz.com/emp/job/'|| job AS jobTypeURI
            , job
            , deptno
            , 'xyz.com/emp/etype/'|| etype AS empTypeURI
            , etype
            , 'xyz.com/graph/'|| job || '/' || etype AS graphURI
       from emp
    """;
   rr:instanceIdMap [ a rr:IRIMap; rr:column "empURI" ];

   rr:RDFTypeURIPropertyMap [ rr:property emp:jobtype; rr:column "jobTypeURI" ];
   rr:RDFTypeURIPropertyMap [ rr:property emp:emptype; rr:column "empTypeURI" ];

   rr:graphURIPropertyMap [ rr:property emp:destGraph; rr:column "graphURI" ];

   rr:propertyMap [ rr:property rdf:type; rr:column "jobTypeURI" ];
   rr:propertyMap [ rr:property rdf:type; rr:column "empTypeURI" ];
   rr:propertyMap [ rr:property emp:empno; rr:column "empno" ];
   rr:propertyMap [ rr:property emp:name; rr:column "ename" ];
   rr:propertyMap [ rr:property emp:job; rr:column "job" ];
   rr:propertyMap [ rr:property emp:deptNum; rr:column "deptno" ];
   rr:propertyMap [ rr:property emp:etype; rr:column "etype" ];
   rr:foreignKeyMap [
      rr:key emp:c_ref_deptno;
      rr:parentClass xyz:dept;
      rr:joinCondition "{child}.deptno = {parent}.deptno";
   ];
   .
<#classMap3>
   a rr:ClassMap;
   rr:class xyz:likes;
   rr:graph xyz:LikesGraph;
   rr:sqlDefString """
      Select id
           , likeType
           , likedObj
        from likes
   """;
  rr:instanceIdMap [ a rr:IRIMap; rr:column "id"];
  rr:ComputedPropertyMap [ rr:property likes:likeType; rr:column "likeType" ];
  rr:propertyMap [ rr:property likes:likeType; rr:column "likedObj" ];
  .