如何使用Java API在Sesame RDF存储库上执行SPARQL更新?

时间:2014-05-15 12:18:19

标签: java sesame openrdf

请,我有一个代码,并希望用新的三元组更新repo但执行方法有一些错误:

以下是我的代码:

public void temporalsendRIG(RepositoryConnection con) {
        ArrayList<String> userInputArrayList = new ArrayList<String>();
        populateArrayList(userInputArrayList);
        try {
            con = makeConnection(RemoteServer_url, Repository_id);
            String updateQuery = buildUpdateQuery(userInputArrayList);
            System.out.println(updateQuery);
            Update update = con.prepareUpdate(QueryLanguage.SPARQL,
                        updateQuery);
                        update.execute(); 


        } catch (MalformedQueryException e) {
            e.printStackTrace();
        }catch (RepositoryException e) {
            e.printStackTrace();
        }catch(UpdateExecutionException e){
            e.printStackTrace();
        }

    }

及以下是我正在解析的updateQuery字符串:

public String buildUpdateQuery(ArrayList<String> arr){
    String updateQuery = 
        "PREFIX owl:<http://www.w3.org/2002/07/owl#> \n" + 
        "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
        "DELETE \n" +
        "{ \n" +
             "?s ?p ?o.\n" +

            "}\n" +
            "INSERT \n" +
            "{\n" +
            "?s \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/bookerFirstName>" + "\"" + arr.get(0) + "\" ; \n"  +
                    "<http://localhost:9090/reservationService/onto/Reservation/cityOfInterest>" + "\"" + arr.get(1) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/distanceToCityOfInterest>"  + "\"" + arr.get(2) + "\" ;\n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/maximumShift>" + "\"" + arr.get(3) + "\" ; \n"  +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredAmountOfDays>" + "\"" + arr.get(4) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredBedrooms>" + "\"" + arr.get(5) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredDistanceToLake>"  + "\"" + arr.get(6) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredOccupacy>"  + "\"" + arr.get(7) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/startingBookingDay>" + "\"" + arr.get(8) + "\" . \n" +

            "} \n" +
            "WHERE \n" +
            "{ \n" +
                " ?s rdf:type <http://sswapmeet.sswap.info/sswap/Subject>. \n" +
                " ?s ?p ?o. \n" +
                " FILTER NOT EXISTS  { ?s rdf:type  ?o  } \n" +
                 "FILTER NOT EXISTS  { ?s  <http://sswapmeet.sswap.info/sswap/mapsTo>  ?o  } \n" +
            "} \n"
             ;


            return updateQuery;
}

最后,这是我运行代码时遇到的错误:

May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.openrdf.repository.http.HTTPUpdateExecutionException: Connection refused
    at org.openrdf.repository.http.HTTPUpdate.execute(HTTPUpdate.java:70)
    at org.henry.queryUpdate.temporalsendRIG(queryUpdate.java:63)
    at org.henry.queryUpdate.main(queryUpdate.java:138)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:280)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at org.openrdf.http.client.HTTPClient.sendUpdate(HTTPClient.java:387)
    at org.openrdf.repository.http.HTTPUpdate.execute(HTTPUpdate.java:57)
    ... 2 more

1 个答案:

答案 0 :(得分:1)

问题解决了!我在repository_url中输入了一个拼写错误。如问题所示,代码非常精细。唯一的问题是我调用localhost:8080 ..当我的locahost端口是9090.这是由于一些复制错误。我已经从主项目复制代码片段进行测试,忘记正确更改端口号。感谢