简体   繁体   English

当我执行插入查询RDF4J时如何获取UpdateExpr

[英]How get UpdateExpr when i execute insert query RDF4J

problem with insert query, can't pars simple statements 插入查询的问题,无法解析简单的语句

Example: query: 示例:查询:

PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA 
{
    <http://example/book1> dc:title "A new book";
                           dc:creator "A.N.Other" .
}

result: 结果:

PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX sesame: <http://www.openrdf.org/schema/sesame#> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX fn: <http://www.w3.org/2005/xpath-functions#> 
 <http://example/book1> dc:title "A new book" ; dc:creator "A.N.Other" .

But if I pars something more difficult all is well. 但是,如果我解决一些更困难的问题,一切都很好

query: 查询:

PREFIX dc:  <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

INSERT 
  { GRAPH <http://example/bookStore2> { ?book ?p ?v } }
WHERE
  { GRAPH  <http://example/bookStore>
       { ?book dc:date ?date .
         FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
         ?book ?p ?v
  } }

result: 结果:

StatementPattern FROM NAMED CONTEXT
   Var (name=book)
   Var (name=p)
   Var (name=v)
   Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)

Modify
   StatementPattern FROM NAMED CONTEXT
      Var (name=book)
      Var (name=p)
      Var (name=v)
      Var (name=_const_f1165b11_uri, value=http://example/bookStore2, anonymous)
   Filter
      Compare (>)
         Var (name=date)
         ValueConstant (value="1970-01-01T00:00:00-02:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>)
      Join
         StatementPattern FROM NAMED CONTEXT
            Var (name=book)
            Var (name=_const_9b277d39_uri, value=http://purl.org/dc/elements/1.1/date, anonymous)
            Var (name=date)
            Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)
         StatementPattern FROM NAMED CONTEXT
            Var (name=book)
            Var (name=p)
            Var (name=v)
            Var (name=_const_39534d41_uri, value=http://example/bookStore, anonymous)

I need to get something like that the first case. 我需要得到第一种情况。 What am I doing wrong? 我究竟做错了什么?

Java code for executing these queries: 用于执行这些查询的Java代码:

String query = ...
SPARQLParser parser = new SPARQLParser();
ParsedUpdate q2 = parser.parseUpdate(query, null);
Iterator var2 = q2.getUpdateExprs().iterator();
UpdateExpr updateExpr = (UpdateExpr)var2.next();
System.out.println(updateExpr);

As indicated in the comments, the data block for an INSERT DATA SPARQL update is internally stored as an (unparsed) string. 如注释中所示, INSERT DATA SPARQL更新的数据块在内部存储为(未解析的)字符串。 If you wish to process it further, you have a couple of options. 如果您希望进一步处理它,您有几个选择。

One option is to actually just execute the update, for example against a temporary/empty in-memory store, and then just retrieve the statements from that store: 一种选择是实际上只是执行更新,例如针对临时/空的内存存储,然后只检索该存储中的语句:

 String update = "INSERT DATA { .... }";
 Repository tempRep = new SailRepository(new MemoryStore());
 tempRep.init();
 try(RepositoryConnection conn = tempRep.getConnection()) {
     conn.prepareUpdate(update).execute());
     Model statements = QueryResults.asModel(conn.getStatements(null, null, null));
 }

Another option, which is perhaps more scalable, is to just extract the data block as a string and pass it through a parser. 另一个可能更具可扩展性的选项是将数据块提取为字符串并将其传递给解析器。 The SPARQLUpdateDataBlockParser specifically exists for this purpose: SPARQLUpdateDataBlockParser专门用于此目的:

InsertData insertDataExpr = (InsertData)updateExpr;

RDFParser parser = new SPARQLUpdateDataBlockParser();
StatementCollector handler = new StatementCollector();
parser.setRDFHandler(handler);

parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");

Collection<Statement> stmts = handler.getStatements();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM