简体   繁体   English

如何使用 Apache Jena Java API 创建 Fuseki SPARQL 服务器?

[英]How do you create a Fuseki SPARQL server using the Apache Jena Java API?

I am trying to create a Fuseki SPARQL server on my machine.我正在尝试在我的机器上创建一个 Fuseki SPARQL 服务器。 The documentation on the Jena website describes how to create such a server from the command-line, here: http://jena.apache.org/documentation/serving_data/ . Jena 网站上的文档描述了如何从命令行创建这样的服务器,此处为: http://jena.apache.org/documentation/serving_data/ I am looking for a way of creating and initializing such a server just using the Jena Java API.我正在寻找一种仅使用 Jena Java API 来创建和初始化此类服务器的方法。 I have looked over the Jena API but have not made any progress in working out how to proceed.我查看了 Jena API,但在确定如何进行方面没有任何进展。 Has anyone done this before?有没有人这样做过?

If you use如果你使用

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>apache-jena-libs</artifactId>
  <type>pom</type>
  <version>2.11.2-SNAPSHOT</version>
</dependency>

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>jena-fuseki</artifactId>
  <version>1.0.2-SNAPSHOT</version>
</dependency>

You can start an embedded Server.您可以启动嵌入式服务器。

Dataset dataset = TDBFactory.createDataset(MagicStrings.TDBLocation);
dataset.begin(ReadWrite.WRITE);
Model tdb = dataset.getDefaultModel();
EmbeddedFusekiServer server = EmbeddedFusekiServer.
create(3030,getDataset().asDatasetGraph(), "comp");

Yes this is possible but it is not how Fuseki was designed to operate so do so at your own risk.是的,这是可能的,但这不是 Fuseki 的设计方式,因此您需要自担风险。

You will need to pull in Fuseki as a dependency, via maven this would be the following:您需要通过 maven 将 Fuseki 作为依赖项引入,如下所示:

<dependency>
  <groupId>org.apache.jena</groupId>
  <artifactId>jena-fuseki</artifactId>
  <version>0.2.7</version>
</dependency>

Then you can use the SPARQLServer class to create a server and call start() to actually run the server and stop() when you are done.然后,您可以使用SPARQLServer class 创建服务器并调用start()来实际运行服务器,完成后调用stop() (This is located in the org.apache.jena.fuseki.server package) (这位于org.apache.jena.fuseki.server包中)

It is important to note that if you do this the server is embedded in the JVM from which you start it, when that JVM shuts down the server shuts down.重要的是要注意,如果您这样做,则服务器嵌入在您启动它的 JVM 中,当 JVM 关闭时,服务器将关闭。 This may be your intention but it may not.这可能是您的意图,但可能不是。

Btw you question is unclear as to exactly why you want to do this?顺便说一句,您的问题不清楚您为什么要这样做? There may be alternative ways to achieve your goal without embedding Fuseki if you explain your goals further如果您进一步解释您的目标,可能有其他方法可以在不嵌入 Fuseki 的情况下实现您的目标

The answers here are quite old and I am afraid they would yield results anymore with the more recent versions of the Jena API.这里的答案已经很老了,我担心他们会在更新版本的耶拿 API 中产生结果。

//Creating a persistent triple store (Jena TDB)
String directory = "C:\\Users\\..."; // Directory where you want to create the triplestore
Dataset dataset = TDBFactory.createDataset(directory);

//Loading an ontology stored on the disk
String ontPath = "C:\\Users... ...product.owl"; //Directory of your RDF/OWL file
Model KB = RDFDataMgr.loadModel(ontPath);
KB.write(System.out, "RDF/XML"); // (optional) checking what you have loaded in the console
dataset.addNamedModel("https://joedavid91.github.io/ontologies/camo/product",KB);

// Starting the fuseki server
FusekiServer fusekiServer = FusekiServer.create()
            .port(3001)
            .add("/ds", dataset, true)
            .build();

fusekiServer.start();

Now lets try to fetch some results using a REST client (eg POSTMAN) to see if this works.现在让我们尝试使用 REST 客户端(例如 POSTMAN)获取一些结果,看看这是否有效。 You can see that it indeed does.你可以看到确实如此。 Make sure your follow the SPARQL Protocol for the REST request.确保您遵循 REST 请求的SPARQL 协议

Jena Fuseki 通过 POSTMAN 获得的结果

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

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