线程" main"中的例外情况com.hp.hpl.jena.update.UpdateException:转换结果为FAILED:

时间:2018-03-12 11:53:29

标签: sparql rdf jena virtuoso turtle-rdf

我正在使用SPARQL将三元组数据插入到IRI中。

这是我的代码,

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Iterator;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;

import virtuoso.jena.driver.VirtGraph;
import virtuoso.jena.driver.VirtuosoQueryExecution;
import virtuoso.jena.driver.VirtuosoQueryExecutionFactory;
import virtuoso.jena.driver.VirtuosoUpdateFactory;
import virtuoso.jena.driver.VirtuosoUpdateRequest;

public class VirtuosoSPARQLExample9 {

    /**
     * Executes a SPARQL query against a virtuoso url and prints results.
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {

        String url;
        if(args.length == 0)
            url = "jdbc:virtuoso://192.168.99.100:1111";
        else
            url = args[0];

/*          STEP 1          */
        VirtGraph set = new VirtGraph (url, "dba", "dba");

/*          STEP 2          */
                String str = "CLEAR GRAPH <http://192.168.99.100/test1>";
                VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();                  

               // InputStream in = new FileInputStream("src/main/webapp/resources/outputfile/test.ttl");


               // Model model1 = ModelFactory.createDefaultModel(); // creates an in-memory Jena Model
               // model1.read(in, null, "TURTLE");
              //  StringWriter out = new StringWriter();
               // model1.write(out, "TURTLE");
                str = "INSERT INTO GRAPH <http://192.168.99.100/test1> { @base <http://example.org/> .\r\n" + 
                        "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\r\n" + 
                        "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\r\n" + 
                        "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\r\n" + 
                        "@prefix rel: <http://www.perceive.net/schemas/relationship/> .\r\n" + 
                        "\r\n" + 
                        "<#green-goblin>\r\n" + 
                        "    rel:enemyOf <#spiderman> ;\r\n" + 
                        "    a foaf:Person ;    # in the context of the Marvel universe\r\n" + 
                        "    foaf:name \"Green Goblin\" .\r\n" + 
                        "\r\n" + 
                        "<#spiderman>\r\n" + 
                        "    rel:enemyOf <#green-goblin> ;\r\n" + 
                        "    a foaf:Person ;\r\n" + 
                        "    foaf:name \"Spiderman\", \"Человек-паук\"@ru . };";
                vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();                  


/*      Select all data in virtuoso */
        Query sparql = QueryFactory.create("SELECT * FROM <http://test1> WHERE { ?s ?p ?o }");
        VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, set);
        ResultSet results = vqe.execSelect();
                System.out.println("\nSELECT results:");
        while (results.hasNext()) {
            QuerySolution rs = results.nextSolution();
            RDFNode s = rs.get("s");
            RDFNode p = rs.get("p");
            RDFNode o = rs.get("o");
            System.out.println(" { " + s + " " + p + " " + o + " . }");
        }

        sparql = QueryFactory.create("DESCRIBE <http://aa> FROM <http://test1>");
        vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

        Model model = vqe.execDescribe();
            Graph g = model.getGraph();
                System.out.println("\nDESCRIBE results:");
            for (Iterator i = g.find(Node.ANY, Node.ANY, Node.ANY); i.hasNext();) 
               {
                  Triple t = (Triple)i.next();
              System.out.println(" { " + t.getSubject() + " " + 
                             t.getPredicate() + " " + 
                             t.getObject() + " . }");
            }



        sparql = QueryFactory.create("CONSTRUCT { ?x <http://test> ?y } FROM <http://test1> WHERE { ?x <http://bb> ?y }");
        vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

        model = vqe.execConstruct();
            g = model.getGraph();
                System.out.println("\nCONSTRUCT results:");
            for (Iterator i = g.find(Node.ANY, Node.ANY, Node.ANY); i.hasNext();) 
               {
                  Triple t = (Triple)i.next();
              System.out.println(" { " + t.getSubject() + " " + 
                             t.getPredicate() + " " + 
                             t.getObject() + " . }");
            }


        sparql = QueryFactory.create("ASK FROM <http://test1> WHERE { <http://aa> <http://bb> ?y }");
        vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

        boolean res = vqe.execAsk();
                System.out.println("\nASK results: "+res);


    }
}

运行查询时出现错误,

at virtuoso.jena.driver.VirtuosoUpdateRequest.exec(Unknown Source)
    at com.bordercloud.sparql.virtuoso.VirtuosoSPARQLExample9.main(VirtuosoSPARQLExample9.java:70)
Caused by: virtuoso.jdbc3.VirtuosoException: SQ074: Line 16: SP030: SPARQL compiler, line 2: syntax error at '@base' before '<http://example.org/>'
    at virtuoso.jdbc3.VirtuosoResultSet.process_result(Unknown Source)
    at virtuoso.jdbc3.VirtuosoResultSet.<init>(Unknown Source)
    at virtuoso.jdbc3.VirtuosoStatement.sendQuery(Unknown Source)
    at virtuoso.jdbc3.VirtuosoStatement.execute(Unknown Source)
    ... 2 more

当我运行示例示例时,我正在使用virtuoso Jena示例,该示例在链接Viruoso Jena example上给出,它将完美运行且没有错误。我添加了rdf triples data作为示例,其中包含前缀没有完美地跑步,我收到了错误。

另外,如果有任何方法可以使用SPARQL查询将RDF数据文件(test.ttl)直接加载到virtuoso服务器,这对我来说非常有用。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的Jena INSERT失败,因为当该内容包含INSERT@base以及其他各种内容时,SPARQL不允许您@prefix {完成Turtle文件的内容}的东西。

对于您的基本问题,正如我之前在其他地方回答的那样,有MANY documented ways to load data into Virtuoso

根据您在此处提出的具体问题,您可以使用SPARQL LOAD功能,如果您要加载的文件是可访问的通过HTTP或其他支持Virtuoso的URI。 file: scheme URIs (as @AKSW shows) are supported 如果该文件是Virtuoso实例的本地文件 - 即,如果它位于Virtuoso本地文件系统上,而不是任何远程客户端主机 - 并且 if 包含目录包含在DirsAllowed setting of the [Parameters] stanza的值中。

LOAD <file:///path_to_your_file.ttl> INTO GRAPH <http://192.168.99.100/test1>

我强烈建议你read the documentation。它确实回答了很多问题。