在Jena中读取嵌套的RDF三元组

时间:2013-12-05 12:53:33

标签: java json rdf sparql jena

我有一个查询

CONSTRUCT { ?highValForeignTran ?hvFTPred ?hvFTObj . }
WHERE { ?highValForeignTran vocab:accounttransactions_transactionCurrency "USD" .
?highValForeignTran vocab:accounttransactions_transactionValue ?tranValue .
?highValForeignTran vocab:accounttransactions_transactionDate ?tranDate .
?highValForeignTran ?hvFTPred ?hvFTObj .
FILTER ( ?tranValue > 10000) .
FILTER (  ?tranDate >= "2013-11-23"^^xsd:date  && ?tranDate <= "2013-11-23"^^xsd:date) .
}

返回结果:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vocab="http://localhost:2020/resource/vocab/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:db="http://localhost:2020/resource/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:map="http://localhost:2020/resource/#">
<vocab:accounttransactions rdf:about="http://localhost:2020/resource/accounttransactions/1">
<vocab:accounttransactions_id rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"
>1</vocab:accounttransactions_id>
<vocab:accounttransactions_transactionCurrency>USD</vocab:accounttransactions_transactionCurrency>
<vocab:accounttransactions_originAccountNumber>DB48939239</vocab:accounttransactions_originAccountNumber>
<vocab:accounttransactions_transactionType>Cr</vocab:accounttransactions_transactionType>
    <vocab:accounttransactions_transactionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
>2013-11-23</vocab:accounttransactions_transactionDate>
<vocab:accounttransactions_destinationAccountId rdf:resource="http://localhost:2020/resource/bankaccounts/1"/>
<vocab:accounttransactions_transactionValue rdf:datatype=
"http://www.w3.org/2001/XMLSchema#decimal">12000</vocab:accounttransactions_transactionValue>
<rdfs:label>accounttransactions #1</rdfs:label>
<vocab:accounttransactions_destinationAccountNumber>47321896544567</vocab:accounttransactions_destinationAccountNumber>
</vocab:accounttransactions>
</rdf:RDF>

当我尝试使用Jena解析它时,我只得到一个代表外部accountTransactions三元组的三元组:

{"http://localhost:2020/resource/accounttransactions/1":
 {"subject":"http://localhost:2020/resource/accounttransactions/1",
  "predicate":"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
  "object":"http://localhost:2020/resource/vocab/accounttransactions"}
}

我不知道为什么其他三元组嵌套在里面,但我真的需要能够解析它们并将它们作为JSON发送。 这是我的代码:

try {
Model result = qexec.execConstruct();

    JSONObject jsonShell = new JSONObject();

StmtIterator stmtIter = model.listStatements();
    while ( stmtIter.hasNext() ) {
        Statement stmt = stmtIter.nextStatement();
        JSONObject innerJson = new JSONObject();
        innerJson.put("subject", stmt.getSubject().visitWith(rdfVisitor));
        innerJson.put("predicate", stmt.getPredicate().visitWith(rdfVisitor));
        innerJson.put("object", stmt.getObject().visitWith(rdfVisitor));

        jsonShell.put(String.valueOf(stmt.getSubject().visitWith(rdfVisitor)), innerJson);
    }
    System.out.println(resultJson.toString());
    }
    finally {
        qexec.close();
    }

RDFVisitor rdfVisitor = new RDFVisitor() {

    @Override
    public Object visitURI(Resource r, String uri) {
        return uri;
    }

    @Override
    public Object visitLiteral(Literal l) {
        return l.getLexicalForm();
    }

    @Override
    public Object visitBlank(Resource r, AnonId id) {
        return id.getLabelString();
    }
};

我很遗憾,如果Statement.getProperty()可以做到这一点,但无法找到创建Property实例的方法。

1 个答案:

答案 0 :(得分:5)

代码中的问题(以及生成的JSON)

您数据中的所有三元组(很好)具有相同的主题。这可能更容易在更易读的Turtle格式中看到,或者非常明显地以每行三行N-Triples格式显示。我在这个答案的最后列出了这些内容。由于所有三元组都有相同的主题,我怀疑正在发生的是

jsonShell.put(String.valueOf(stmt.getSubject().visitWith(rdfVisitor)), innerJson);
//            |-----------------------------------------------------|
//                         same every time

每次都会覆盖上一次迭代的结果,因为如上所述,密钥是每次迭代的相同。如果你在循环中添加一些打印语句,我希望你会发现你实际上正在迭代模型中的每一个三元组。

我无法告诉你应该用于那里的密钥,因为我不清楚该密钥将如何有用,因为三元组的主题已经编码在输出。看起来你想要某种语句ID,所以也许你可以使用字符串表示语句或其他东西。

使用Jena和RDF / JSON

的替代方案

我指出Jena可以在RDF / JSON中序列化模型,如果这是你需要的,那么这可能是一种更容易获得JSON的方法。结构与当然产生的结构不同,但这可能不是一个大问题。例如,/jsonoutput.ttl是我的数据的本地副本,以下代码编写JSON。

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class JSONObjectTest {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        model.read( JSONObjectTest.class.getResourceAsStream( "/jsonoutput.ttl"), null, "N3" );
        model.write( System.out, "RDF/JSON" );
    }
}

结果JSON是:

{ 
  "http://localhost:2020/resource/accounttransactions/1" : { 
    "http://localhost:2020/resource/vocab/accounttransactions_transactionDate" : [ { 
      "type" : "literal" ,
      "value" : "2013-11-23" ,
      "datatype" : "http://www.w3.org/2001/XMLSchema#date"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_transactionValue" : [ { 
      "type" : "literal" ,
      "value" : "12000" ,
      "datatype" : "http://www.w3.org/2001/XMLSchema#decimal"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_id" : [ { 
      "type" : "literal" ,
      "value" : "1" ,
      "datatype" : "http://www.w3.org/2001/XMLSchema#integer"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_destinationAccountNumber" : [ { 
      "type" : "literal" ,
      "value" : "47321896544567"
    }
     ] ,
    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : [ { 
      "type" : "uri" ,
      "value" : "http://localhost:2020/resource/vocab/accounttransactions"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_transactionCurrency" : [ { 
      "type" : "literal" ,
      "value" : "USD"
    }
     ] ,
    "http://www.w3.org/2000/01/rdf-schema#label" : [ { 
      "type" : "literal" ,
      "value" : "accounttransactions #1"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_transactionType" : [ { 
      "type" : "literal" ,
      "value" : "Cr"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_destinationAccountId" : [ { 
      "type" : "uri" ,
      "value" : "http://localhost:2020/resource/bankaccounts/1"
    }
     ] ,
    "http://localhost:2020/resource/vocab/accounttransactions_originAccountNumber" : [ { 
      "type" : "literal" ,
      "value" : "DB48939239"
    }
     ]
  }
}

您的数据格式不同

Turtle / N3中的数据

@prefix db:    <http://localhost:2020/resource/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix map:   <http://localhost:2020/resource/#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix vocab: <http://localhost:2020/resource/vocab/> .

<http://localhost:2020/resource/accounttransactions/1>
        a                             vocab:accounttransactions ;
        rdfs:label                    "accounttransactions #1" ;
        vocab:accounttransactions_destinationAccountId
                <http://localhost:2020/resource/bankaccounts/1> ;
        vocab:accounttransactions_destinationAccountNumber
                "47321896544567" ;
        vocab:accounttransactions_id  1 ;
        vocab:accounttransactions_originAccountNumber
                "DB48939239" ;
        vocab:accounttransactions_transactionCurrency
                "USD" ;
        vocab:accounttransactions_transactionDate
                "2013-11-23"^^xsd:date ;
        vocab:accounttransactions_transactionType
                "Cr" ;
        vocab:accounttransactions_transactionValue
                "12000"^^xsd:decimal .

N-Triples中的数据

<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_id> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_transactionCurrency> "USD" .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_originAccountNumber> "DB48939239" .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_transactionType> "Cr" .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_transactionDate> "2013-11-23"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_destinationAccountId> <http://localhost:2020/resource/bankaccounts/1> .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_transactionValue> "12000"^^<http://www.w3.org/2001/XMLSchema#decimal> .
<http://localhost:2020/resource/accounttransactions/1> <http://www.w3.org/2000/01/rdf-schema#label> "accounttransactions #1" .
<http://localhost:2020/resource/accounttransactions/1> <http://localhost:2020/resource/vocab/accounttransactions_destinationAccountNumber> "47321896544567" .
<http://localhost:2020/resource/accounttransactions/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://localhost:2020/resource/vocab/accounttransactions> .