我为 sparql 执行编写的代码正确吗?

发布于 2024-10-21 12:59:08 字数 1900 浏览 2 评论 0原文

我的代码:

package sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class QueryTest {

public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(new File("foaf-ijd.rdf"));

    Model model = ModelFactory.createMemModelMaker().createDefaultModel();

    model.read(in, null);
    in.close();

    String queryString = "SELECT ?x WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, 'John Smith')";

    Query query = QueryFactory.create(queryString);

    QueryExecution qe = QueryExecutionFactory.create(query, model);
    ResultSet results = qe.execSelect();

    ResultSetFormatter.out(System.out, results, query);

    qe.close();

}

}

生成错误

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.Logger.trace(Ljava/lang/String;)V
    at com.hp.hpl.jena.sparql.lib.SystemUtils.chooseClassLoader(SystemUtils.java:23)
    at com.hp.hpl.jena.sparql.lib.Metadata.init(Metadata.java:45)
    at com.hp.hpl.jena.sparql.lib.Metadata.get(Metadata.java:75)
    at com.hp.hpl.jena.query.ARQ.<clinit>(ARQ.java:253)
    at com.hp.hpl.jena.query.Query.<clinit>(Query.java:54)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:71)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:43)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:31)
    at sample.QueryTest.main(QueryTest.java:29)

*

my code:

package sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class QueryTest {

public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(new File("foaf-ijd.rdf"));

    Model model = ModelFactory.createMemModelMaker().createDefaultModel();

    model.read(in, null);
    in.close();

    String queryString = "SELECT ?x WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, 'John Smith')";

    Query query = QueryFactory.create(queryString);

    QueryExecution qe = QueryExecutionFactory.create(query, model);
    ResultSet results = qe.execSelect();

    ResultSetFormatter.out(System.out, results, query);

    qe.close();

}

}

errors getting generated

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.Logger.trace(Ljava/lang/String;)V
    at com.hp.hpl.jena.sparql.lib.SystemUtils.chooseClassLoader(SystemUtils.java:23)
    at com.hp.hpl.jena.sparql.lib.Metadata.init(Metadata.java:45)
    at com.hp.hpl.jena.sparql.lib.Metadata.get(Metadata.java:75)
    at com.hp.hpl.jena.query.ARQ.<clinit>(ARQ.java:253)
    at com.hp.hpl.jena.query.Query.<clinit>(Query.java:54)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:71)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:43)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:31)
    at sample.QueryTest.main(QueryTest.java:29)

*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-10-28 12:59:08

您的代码看起来不错,您收到的错误来自 Jena 使用的库之一。您使用的是哪个版本的 Jena?您尝试过最新版本吗?您是否确保 Jena 下载的 lib/ 目录中的所有 .jar 都位于您的 CLASSPATH 中?如果是这样,您是否检查过以确保您的 CLASSPATH 上没有 Jena(或 slf4j-*.jar)的多个冲突版本?

Your code looks OK, the error that you are getting comes from one of the libraries that Jena uses. Which version of Jena are you using? Have you tried with the latest release? Have you ensured that all of the .jars in the lib/ directory of your Jena download are on your CLASSPATH? If so, have you checked to ensure that you don't have multiple conflicting versions of Jena (or of slf4j-*.jar) on your CLASSPATH?

最初的梦 2024-10-28 12:59:08

SPARQL查询是错误的,您应该使用'{'而不是'('并且没有逗号:

    String queryString = "SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\"}";

The SPARQL query is wrong, you should use '{' instead of '(' and no commas:

    String queryString = "SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\"}";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文