3

我使用HTTPRepository存储库的 URL 进行了初始化。我使用 aRepositoryConnection来检索和添加(天气)数据到存储库。从 Web 服务中检索数据,然后将其转换为 RDF 语句,并添加到存储库中。这是由一个独立的应用程序定期完成的。

当我在 IntelliJ 中运行此应用程序时,一切正常。

为了在服务器上运行这个应用程序,我创建了一个 jar 文件(包含所有依赖项)。应用程序按预期启动,并能够从存储库中检索数据。

但是,当应用程序尝试将数据写入存储库时,我得到UnsupportedRDFormatException

org.eclipse.rdf4j.rio.UnsupportedRDFormatException: Did not recognise RDF format object BinaryRDF (mimeTypes=application/x-binary-rdf; ext=brf)
    at org.eclipse.rdf4j.rio.Rio.lambda$unsupportedFormat$0(Rio.java:568) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_111]
    at org.eclipse.rdf4j.rio.Rio.createWriter(Rio.java:134) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.rio.Rio.write(Rio.java:371) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.rio.Rio.write(Rio.java:324) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.repository.http.HTTPRepositoryConnection.addModel(HTTPRepositoryConnection.java:588) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.repository.http.HTTPRepositoryConnection.flushTransactionState(HTTPRepositoryConnection.java:662) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.repository.http.HTTPRepositoryConnection.commit(HTTPRepositoryConnection.java:326) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.repository.base.AbstractRepositoryConnection.conditionalCommit(AbstractRepositoryConnection.java:366) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at org.eclipse.rdf4j.repository.base.AbstractRepositoryConnection.add(AbstractRepositoryConnection.java:431) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at nl.wur.fbr.data.weather.WeatherApp.retrieveData(WeatherApp.java:122) ~[weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at nl.wur.fbr.data.weather.WeatherData$WeatherTask.run(WeatherData.java:105) [weatherData-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
    at java.util.TimerThread.mainLoop(Timer.java:555) [na:1.8.0_111]
    at java.util.TimerThread.run(Timer.java:505) [na:1.8.0_111]

发生错误的源代码是:

    public void retrieveData(){
        logger.info("Retrieving data for weather for app: "+ID+" ");
        RepositoryConnection connection = null;
        ValueFactory vf = SimpleValueFactory.getInstance();
        try {
            connection = repository.getConnection();

            // Retrieving the locations from the repository (no problem here).
            List<Location> locations = this.retrieveLocations(connection);
            List<Statement> statements = new ArrayList<>();

            // Retrieving weather data from each location and transforming it to statements.
            for(Location location : locations){
                List<Weather> retrievedWeather = weatherService.retrieveWeatherData(location.name,location.latitude,location.longitude);
                for(Weather weather : retrievedWeather){
                    BNode phenomenon = vf.createBNode();
                    statements.add(vf.createStatement(location.ID,WEATHER.HAS_WEATHER,phenomenon,rdfStoreGraph));
                    statements.addAll(weather.getStatements(phenomenon,vf,rdfStoreGraph));
                    statements = this.correctOMIRIs(statements,vf);
                }
            }

            // Adding data retrieved from the weather API
            // This is where the exception happens.
            connection.add(statements,rdfStoreGraph);

        } catch (Exception e) {
            logger.error("Could not retrievedata for weather app: '"+ID+"' because no monitor locations could be found.",e);
        } finally {
            if(connection != null){
                connection.close();
            }
        }
    }

HTTPRespository初始化为:

        repository = new HTTPRepository(rdfStore.toString());
        ((HTTPRepository)repository).setPreferredRDFFormat(RDFFormat.BINARY);
        ((HTTPRepository)repository).setPreferredTupleQueryResultFormat(TupleQueryResultFormat.BINARY);

我尝试将格式更改为TURTLE. 但这没有什么区别。

你能告诉我如何解决这个问题吗?

注意。RDF4J 服务器和库都有版本 2.0.1 (rdf4j)。

4

2 回答 2

8

为了在服务器上运行这个应用程序,我创建了一个 jar 文件(包含所有依赖项)。

您的问题是:您创建了一个“胖 jar”,并且可能没有正确合并 SPI 注册表文件。

RDF4J 的 Rio 解析器(以及其他几个模块)使用 Java 的服务提供者接口 (SPI) 机制来注册自己。这种机制依赖于META-INF\servicesjar 文件中包含每个解析器实现的完全限定名称的文本文件。

合并 jar 时会出现问题:每个 Rio 解析器 jar 都有一个名称相同但内容不同的注册表文件。如果您使用 maven 程序集插件之类的东西来创建 fat jar,则每个注册表文件都会被下一个文件覆盖。结果是,最后,RDF4J 只能找到一个解析器——它的注册表文件最后添加到 fat jar 中的那个。

解决方案是根本不创建胖 jar,或者如果必须,使用不同的技术来创建它,合并注册表文件而不是覆盖它们。maven shade 插件为此提供了一个很好的配置选项:ServicesResourceTransformer

于 2016-12-01T21:06:42.757 回答
2

我正在重新发布这篇文章,因为我被这个问题困扰了几个小时。最后,我可以使用具有以下配置的 maven shade 插件生成一个可执行 jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>${fully.qualified.main.class}</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

我使用带有 ManifestResourceTransformer 的阴影插件来创建指示项目主类的可执行 jar,并使用 ServicesResourceTransformer 来处理 RDF4J 包命名,以避免一个解析器覆盖前一个解析器。此外,我必须包含过滤器部分以避免源自包签名的 JNI 错误。

我希望这对某人有用。

问候。

于 2021-03-26T08:59:47.740 回答