簡體   English   中英

使用D2RQ映射數據庫

[英]Mapping database with 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();
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM