使用D2RQ映射数据库



我正在尝试与我的数据库之间的映射。我正在使用Jena和D2RQ引擎。

我不知道如何配置文件。ttl与数据库连接的信息。

下面是我的代码:

public class CreateOntModel5 {
    public static void main(String[] args) throws OWLOntologyStorageException,
    OWLOntologyCreationException, IOException, SQLException  {

        // Set up the ModelD2RQ using a mapping file
        Model m = new ModelD2RQ("C:/Users/Tiziano/workspace/rules/prova0-mappings.ttl");
        // Find anything with an rdf:type of iswc:InProceedings
        StmtIterator paperIt = m.listStatements(null, RDF.type, ISWC.InProceedings);
        // List found papers and print their titles
        while (paperIt.hasNext()) {
            Resource paper = paperIt.nextStatement().getSubject();
            System.out.println("Paper: " + paper.getProperty(DC.title).getString());
            // List authors of the paper and print their names
            StmtIterator authorIt = paper.listProperties(DC.creator);
            while (authorIt.hasNext()) {
                Resource author = authorIt.nextStatement().getResource();
                System.out.println("Author: " + author.getProperty(FOAF.name).getString());
            }
            System.out.println();
        }
        m.close();
    }
}

在这里你可以找到映射文件代码:

@prefix sdb:      <http://jena.hpl.hp.com/2007/sdb#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352#> .
@base <http://example.com/base/> .
     :myModel
    a d2rq:D2RQModel;
    d2rq:mappingFile <prova0-mappings.ttl>;
    d2rq:resourceBaseURI <http://localhost:3306/prova_rules>;
    .
<MAPPING-ID1>
    a rr:TriplesMap;
    rr:logicalTable [ rr:tableName "esame"] ;
        rr:subjectMap [ rr:template "http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352/scrittura/campowrite1={"campowrite1"}";
                     rr:class <http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352/scrittura>
   ];
    rr:predicateObjectMap [
         rr:predicate   <http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352/scrittura#campowrite1> ;
         rr:objectMap   [ rr:column ""campowrite1""; rr:datatype <http://www.w3.org/2001/XMLSchema#integer> ]
    ];
    rr:predicateObjectMap [
         rr:predicate   <http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352/scrittura#campowrite2> ;
         rr:objectMap [ rr:column ""campowrite2"" ]
    ].

我认为错误在ttl文件上。

错误是:

Exception in thread "main" com.hp.hpl.jena.n3.turtle.TurtleParseException: Line 11, column 7: Unresolved prefixed name: d2rq:D2RQModel
    at com.hp.hpl.jena.n3.turtle.ParserBase.throwParseException(ParserBase.java:274)
    at com.hp.hpl.jena.n3.turtle.ParserBase.resolvePName(ParserBase.java:195)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.PrefixedName(TurtleParser.java:680)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.IRIref(TurtleParser.java:664)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.GraphTerm(TurtleParser.java:475)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.VarOrTerm(TurtleParser.java:399)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.GraphNode(TurtleParser.java:362)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.Object(TurtleParser.java:215)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.ObjectList(TurtleParser.java:197)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.PropertyListNotEmpty(TurtleParser.java:165)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.TriplesSameSubject(TurtleParser.java:130)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.Statement(TurtleParser.java:76)
    at com.hp.hpl.jena.n3.turtle.parser.TurtleParser.parse(TurtleParser.java:46)
    at com.hp.hpl.jena.n3.turtle.ParserTurtle.parse(ParserTurtle.java:37)
    at com.hp.hpl.jena.n3.turtle.TurtleReader.readWorker(TurtleReader.java:21)
    at com.hp.hpl.jena.n3.JenaReaderBase.readImpl(JenaReaderBase.java:101)
    at com.hp.hpl.jena.n3.JenaReaderBase.read(JenaReaderBase.java:68)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:226)
    at com.hp.hpl.jena.util.FileManager.readModelWorker(FileManager.java:395)
    at com.hp.hpl.jena.util.FileManager.loadModelWorker(FileManager.java:299)
    at com.hp.hpl.jena.util.FileManager.loadModel(FileManager.java:250)
    at de.fuberlin.wiwiss.d2rq.jena.ModelD2RQ.<init>(ModelD2RQ.java:35)
    at src.jenalib.CreateOntModel5.main(CreateOntModel5.java:28)

我想我在ttl文件中做错了什么,但我不知道是什么。任何帮助吗?

谢谢。

就像错误信息所说的:

Line 11, column 7: Unresolved prefixed name: d2rq:D2RQModel

您的数据以

开头
@prefix sdb:      <http://jena.hpl.hp.com/2007/sdb#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://www.semanticweb.org/aalisabeth/ontologies/2014/0/untitled-ontology-352#> .
@base <http://example.com/base/> .
     :myModel
    a d2rq:D2RQModel;

您正在使用d2rq:D2RQModel而没有声明d2rq命名空间前缀。

//关联DB2RDF生成

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class DB2RDF {
    /*
     * Input Arguments host:port, dbname, username, password
     */
    public static void main(String args[]) {
        // Data source details
        String databaseName = "testdb";
        String userName = "testuser";
        String password = "";
        String mySQLPort = "3306";
        String hostUrl = "localhost";
        Txt2XMLConversion xml=new Txt2XMLConversion();
        // Setup the connection with the DB
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://"
                    + hostUrl + ":" + mySQLPort + "/" + databaseName, userName,
                    password);
            // --- LISTING DATABASE SCHEMA NAMES ---
            ResultSet resultSet = conn.getMetaData().getCatalogs();
            while (resultSet.next()) {
                //System.out.println("Schema Name = "+ resultSet.getString("TABLE_CAT"));
            }
            resultSet.close();
            // --- LISTING DATABASE TABLE NAMES ---
            String[] types = { "TABLE" };
            resultSet = conn.getMetaData().getTables(databaseName, null, "%",types);
            String tableName = "";
            StringBuilder sb = new StringBuilder();
            while (resultSet.next()) {
                xml.initXML();
                tableName = resultSet.getString(3);
                sb.append(tableName + "n");
                // --- LISTING DATABASE COLUMN NAMES ---
                DatabaseMetaData meta = conn.getMetaData();
                ResultSet resultTable = meta.getColumns(databaseName, null, tableName, "%");
                while (resultTable.next()) {
                    //System.out.println("Column Name of table " + tableName+ " = " + resultTable.getString(4));
                    sb.append(resultTable.getString(4) + "n");
                }
                String data=sb.toString();
                String[] dd=data.split("n");
                for(int i=0;i<dd.length;i++){
                    System.out.println(dd[i]);
                    xml.addNode(dd[i], i);  
                    xml.writeXML(tableName);
                }
                System.out.println("*****************************");
                sb=sb.delete(0, sb.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新