Sparql Select Query works on Sesame-Workbench but not on Java

泪湿孤枕 提交于 2019-12-23 05:20:04

问题


I'm trying to create a Sparql-Query to get all messages, that are related to a lecture. I'm new at the whole 'semantic web'-story so be patient if the usage namespace is completely wrong.

The query I try to execute is:

PREFIX siocNS: <http://rdfs.org/sioc/ns#>
PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX purlPred: <http://purl.org/dc/terms/>
PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>
select ?post ?title ?content ?time ?creator where
{
  ?post rdfPred:type siocNS:Post.
  ?post purlPred:title ?title. 
  ?post purlPred:content ?content. 
  ?post purlPred:created ?time. 
  ?post purlPred:creator ?creator.
  ?post purlPred:context purlPredLecture:1337-7331.
}

where 1337-7331 is an ID.

On Sesame-Workbench the query seems working fine (because I get my 2 expected rows). On Java ,using Jena, it seems that my QueryExecuion isn't correct.

Here my code:

String prefix = "PREFIX siocNS: <http://rdfs.org/sioc/ns#>\n"
            + "PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX purlPred: <http://purl.org/dc/terms/>\n"
            + "PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>\n";
String queryString = "select ?post ?title ?content ?time ?creator where\n"
        + "{\n"
        + "?post rdfPred:type siocNS:Post.\n"
        + "?post purlPred:title ?title. \n"
        + "?post purlPred:content ?content. \n"
        + "?post purlPred:created ?time. \n"
        + "?post purlPred:creator ?creator.\n"
        + "?post purlPred:context purlPredLecture:"
        + lectureID + ".\n" + "}";
logger.info("Created Query:" + prefix + queryString);

Query query = QueryFactory.create(prefix
        + queryString, Syntax.syntaxSPARQL);
query.addResultVar("?post");
query.addResultVar("?title");
query.addResultVar("?content");
query.addResultVar("?time");
query.addResultVar("?creator");
Model model = ModelFactory.createDefaultModel();

QueryExecution execution = QueryExecutionFactory
        .create(query, model);
ResultSet execSelect = execution.execSelect();
while (execSelect.hasNext()) {
...        
DO SOME STUFF
...
}

The query shown above was copied from the java-debugger. Did I missed something on preparation of the Query(Execution) and does Sesame-Workbench complete missing things?

Thx for your help!

dpa


回答1:


So my solution for my own problem ;)

String prefix = "PREFIX siocNS: <http://rdfs.org/sioc/ns#>\n"
            + "PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
            + "PREFIX purlPred: <http://purl.org/dc/terms/>\n"
            + "PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>\n";
String queryString = "select ?post ?title ?content ?time ?creator where\n"
        + "{\n"
        + "?post rdfPred:type siocNS:Post.\n"
        + "?post purlPred:title ?title. \n"
        + "?post purlPred:content ?content. \n"
        + "?post purlPred:created ?time. \n"
        + "?post purlPred:creator ?creator.\n"
        + "?post purlPred:context purlPredLecture:"
        + lectureID + ".\n" + "}";
logger.info("Created Query:" + prefix + queryString);
String completeQuery = prefix + queryString;
        logger.info("Created Query:\n" + completeQuery);
        RepositoryConnection con = createRepositoryConnection();
        if (con == null) {
            logger.warn("Error creating repository connection. Returning epty list");
            return new ArrayList<UserMessage>();
        }
        ArrayList<UserMessage> messages = new ArrayList<UserMessage>();
    try {
        try {
            TupleQuery prepareTupleQuery = con
                    .prepareTupleQuery(
                            QueryLanguage.SPARQL,
                            completeQuery);
            TupleQueryResult result = prepareTupleQuery
                    .evaluate();
            while (result.hasNext()) {
               ...
            }
            for (UserMessage message : messages) {
                collectReplysOfMessage(lectureID,
                        message.getId());
            }
        } catch (MalformedQueryException e) {
            logger.error("Malformed Exception caught",
                    e);
        } catch (QueryEvaluationException e) {
            logger.error(
                    "Error on evaluating the query", e);
        } finally {
            con.close();
        }
    } catch (RepositoryException e) {
        logger.error(
                "Error on creationg repository connection",
                e);
    }

Where 'createRepositoryConnection()' looks like

private static RepositoryConnection createRepositoryConnection() {
        if (repoistory == null) {
            prepareRepository();
        }
        RepositoryConnection con = null;
        try {
            con = repoistory.getConnection();
        } catch (RepositoryException e) {
            logger.error(
                    "Error on creating repository connection",
                    e);
        }
        return con;
    }


来源:https://stackoverflow.com/questions/16594533/sparql-select-query-works-on-sesame-workbench-but-not-on-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!