Working with RDBMS SQL - patzomir/xsparql GitHub Wiki

XSPARQL supports a few types of transformations from a SQL database:

  • RDB2RDF direct mapping
  • R2RML
  • Nesting SQL queries in XSPARQL ones

Currently it supports mysql, psql (Postgres) and sqlserver (MS SQL Server). If you're using mysql, then you need to add the following command in the properties file sql-mode="PIPES_AS_CONCAT,ANSI_QUOTES". Otherwise errors will be thrown.

It is preferable to use properties file for the connection. Examples:

The syntax for running the jar with these properties is:

java -jar xsparql-cli-jar-with-dependencies.jar --dbConfig [properties file] [XSPARQL query]

References:

RDB2RDF direct mapping

The direct mapping can be called as follow:

java -jar xsparql-cli-jar-with-dependencies.jar --dbConfig mysql.properties --dm test

Where test is the database name. The output returns row for each table by row by column:

@prefix rr: <http://www.w3.org/ns/r2rml#> . 
@prefix ex: <http://example.com/> . 
@prefix test: <http://www.w3.org/2006/03/test-description#> . 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . 
@prefix foaf: <http://xmlns.com/foaf/0.1/> . 

_:EMP1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <testEMP> .
_:EMP1 <testEMP#EMPNO> "7369"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:EMP1 <testEMP#ENAME> "SMITH" .
_:EMP1 <testEMP#JOB> "CLERK" .
_:EMP1 <testEMP#DEPTNO> "10"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:test11 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <testtest1> .
_:test11 <testtest1#id> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:test11 <testtest1#name> "patzo" .

R2RML

R2RML is implemented as per w3c recommendation. In XSPARQL the first line of a triplesMap should be <#TriplesMap1> a rr:TriplesMap; as opposed to only <#TriplesMap1> as per the recommendation.

This is an example r2rml file.

@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix ex: <http://example.com/ns#>.

<#TriplesMap1> a rr:TriplesMap;
    rr:logicalTable [ rr:tableName "test.EMP" ];
        rr:subjectMap [
            rr:template "http://data.example.com/employee/{EMPNO}";
            rr:class ex:Employee;
        ];
        rr:predicateObjectMap [
        rr:predicate ex:name;
        rr:objectMap [ rr:column "ENAME" ];
    ].

Nesting SQL queries in XSPARQL ones

Examples for SQL-for-clause Example for constructing a rdf triple directly from RDBMS data.

for EMPNO from EMP
    construct { <ab> <cd> <{$EMPNO}> }

This query returns a XML sequence of all the tables in the current DB.

let $tables := xsparql:getRDBTables()
for $tableName in $tables//relation
return $tableName

Result:

<relation>EMP</relation>
<relation>test1</relation>

Other functions

Example: dm.xsparql). SQLQuery(driver,dbServer,dbPort,database,instance,username,password)

  • xsparql:getRDBTables(): return all tables from the active RDBMS. relations/relation
  • xsparql:getRDBTableAttributes($tableName) - returns all the information about a specified table
let $table := xsparql:getRDBTableAttributes("EMP")
return $table

result:

<metadata>
   <foreignKeys/>
   <columns>
      <column name="&#34;EMPNO&#34;" type="INT"/>
      <column name="&#34;ENAME&#34;" type="VARCHAR"/>
      <column name="&#34;JOB&#34;" type="VARCHAR"/>
      <column name="&#34;DEPTNO&#34;" type="INT"/>
   </columns>
</metadata>
  • getting RDBMS data These are different allowed syntaxes to query SQL data
for row $row from $tableName return $row
for distinct row $row from $tableName return $row
for distinct row $row from sqlQuery($sql) return  $row
  • xsparql:value($result,$column): get the value of a column from a SQL Result (single result, not a whole resultset)
⚠️ **GitHub.com Fallback** ⚠️