我有一个问题
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) . }
返回结果:
1
USD
DB48939239
Cr
2013-11-23
12000
accounttransactions #1
47321896544567
当我尝试使用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
实例的方法.
数据中的所有三元组(很好)具有相同的主题.这可能更容易在更易读的Turtle格式中看到,或者非常明显地以每行三行N-Triples格式显示.我在这个答案的最后列出了这些内容.由于所有三元组都有相同的主题,我怀疑发生了什么
jsonShell.put(String.valueOf(stmt.getSubject().visitWith(rdfVisitor)), innerJson); // |-----------------------------------------------------| // same every time
每次都会覆盖前一次迭代的结果,因为如上所述,每次迭代的密钥都是相同的.如果你在循环中添加一些打印语句,我希望你会发现你实际上正在迭代模型中的每个三元组.
我不能告诉你你应该在那里使用什么密钥,因为我不清楚该密钥将如何有用,因为三元组的主题已经在输出中编码.看起来你想要某种语句ID,所以也许你可以使用字符串表示语句或其他东西.
我要指出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" } ] } }
@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 .
<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> .