【问题标题】:Mapping database with D2RQ使用 D2RQ 映射数据库
【发布时间】:2014-07-03 10:02:30
【问题描述】:

我正在尝试使用我的数据库进行映射。我正在使用 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 文件中做错了什么,但我不知道是什么。 有什么帮助吗?

谢谢。

【问题讨论】:

  • “我认为我在 ttl 文件中做错了,但我不知道是什么。”错误消息显示:“第 11 行,第 7 列:未解析的前缀名称:d2rq:D2RQModel”,因此位置非常明确。您可以看到您在该行上使用了 d2rq:D2RQModel。如果您不知道“未解析的前缀名称”是什么意思,最好专门询问一下。
  • 我投票决定将此问题作为离题结束,因为错误消息中清楚地解释了该问题。这个问题的“答案”只不过是对错误消息的重述。

标签: java rdf jena d2rq


【解决方案1】:

就像错误消息所说:

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 命名空间前缀。

【讨论】:

    【解决方案2】:

    //关系型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();
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多