通过Java代码插入一些数据时,Sparql查询不会更新 [英] Sparql query doesn't upadate when insert some data through java code

查看:87
本文介绍了通过Java代码插入一些数据时,Sparql查询不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的java代码将数据插入到加载到Fuseki服务器的owl文件中。更新查询不会给出任何错误消息。但是owl文件没有更新。我正在使用jena库并使用java代码实现。我的代码有什么问题?

  public boolean addLecturerTriples(String fName,String lName,
String id,String模块){
try {
ArrayList< String> subject = new ArrayList< String>();
ArrayList< String> predicate = new ArrayList< String>();
ArrayList< String> object = new ArrayList< String>();

subject.add(< http://people.brunel.ac.uk/~csstnns/university.owl#+ fName +>);
predicate.add(< http://www.w3.org/1999/02/22-rdf-syntax-ns#type>);
object.add(< http://people.brunel.ac.uk/~csstnns/university.owl#Lecturer>);

for(int i = 0; i< subject.size(); i ++){
String qry =INSERT DATA+
{+
subject.get(i)+\ n+
predicate.get(i)+\ n+
object.get(i)+\ n+
};

UpdateRequest update = UpdateFactory.create(qry);
UpdateProcessor qexec = UpdateExecutionFactory.createRemote(update,http:// localhost:3030 / ds / update);
qexec.execute();
}
} catch(例外e){
返回false;
}
返回true;
}


解决方案

如果你有提供了一个最小的完整示例,即您已经包含了Fuseki配置以及OWL文件如何加载到Fuseki的详细信息。



但是我会假设您没有使用任何特定的配置,并启动Fuseki,如下所示:

  java -jar fuseki-server-VER.jar --update --loc / path / to / db / ds 

所以你在这里做的是启动Fuseki并启用更新并使用位置 / path / to / db 作为数据集的磁盘上TDB数据库位置和URL / ds



您打开浏览器并点击控制面板> / ds 然后使用上传文件功能上传您的OWL文件。当您上传文件时,它会被读入Fuseki并复制到数据集中,在此示例中,您的数据集是位于 / path / to / db 的磁盘TDB数据库。 / p>

重要的是要了解不保留对原始文件的引用,因为Fuseki只是复制来自文件到数据集。



然后使用SPARQL更新表单添加一些数据(或者在您的情况下通过Java代码执行此操作)。更新将应用于数据集,在此示例中重申位于 / path / to / db 的磁盘TDB数据库,其中引用原始文件。因此,您的原始文件不会更改。



使用SPARQL更新来更新原始文件



如果Fuseki不是那么你可以将文件加载到本地内存并在那里运行更新:

 模型m = ModelFactory.createDefaultModel() ; 
m.read(example.owl,RDF / XML);

//准备你的更新...

//在本地模型上创建一个UpdateExecution
UpdateProcessor processor = UpdateExecutionFactory.create(update,GraphStoreFactory.create(米));
processor.execute();

//保存更新的模型
updated.write(new FileOutputStream(example.owl),RDF / XML);

但是如果您想/必须坚持使用Fuseki,您可以通过检索修改后的原始文件来更新来自Fuseki的图表并将其写回您的文件,例如

  DatasetAccessor accessor = DatasetAccessorFactory.createHTTP(http:// localhost: 3030 / DS /数据); 

//下载更新的模型
Model updated = accessor.getModel();

//将更新的模型保存在原始文件
updated.write(new FileOutputStream(example.owl),RDF / XML);

此示例假设您已将OWL文件加载到默认图表中,如果不使用 getModel(http:// graph)重载以加载相关的命名图


I'm trying to insert data through my java code to the owl file which is loaded into Fuseki server. Update query doesn't give any error message. But owl file doesn't update.I'm using jena library and implemented using java code. What is the wrong in my code?

    public boolean addLecturerTriples(String fName, String lName,
    String id, String module) {
    try{
    ArrayList<String> subject = new ArrayList<String>();
    ArrayList<String> predicate = new ArrayList<String>();
    ArrayList<String> object = new ArrayList<String>();

    subject.add("<http://people.brunel.ac.uk/~csstnns/university.owl#"+fName+">");
    predicate.add("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
    object.add("<http://people.brunel.ac.uk/~csstnns/university.owl#Lecturer>");

    for(int i = 0; i < subject.size(); i++){
        String qry = "INSERT DATA"+
                "{"+
                subject.get(i)+"\n"+
                predicate.get(i)+"\n"+
                object.get(i)+"\n"+
                "}";

        UpdateRequest update  = UpdateFactory.create(qry);
        UpdateProcessor qexec = UpdateExecutionFactory.createRemote(update, "http://localhost:3030/ds/update");
        qexec.execute();
    }
    }catch(Exception e){
        return false;
    }
    return true;
}

解决方案

It would help if you have provided a minimal complete example i.e. you had included your Fuseki configuration and the details of how your OWL file is loaded into Fuseki.

However I will assume you have not used any specific configuration and just launching Fuseki like so:

java -jar fuseki-server-VER.jar --update --loc /path/to/db /ds

So what you've done here is launch Fuseki with updates enabled and using the location /path/to/db as the on-disk TDB database location and the URL /ds for your dataset

The you open your browser and click through Control Panel > /ds and then use the Upload file function to upload your OWL file. When you upload a file it is read into Fuseki and copied into the dataset, in this example your dataset is the on disk TDB database located at /path/to/db.

It is important to understand that no reference to the original file is kept since Fuseki has simply copied the data from the file to the dataset.

You then use the SPARQL Update form to add some data (or in your case you do this via Java code). The update is applied to the dataset which to reiterate is in this example the on disk TDB database located at /path/to/db which has no reference to the original file. Therefore your original file will not change.

Using SPARQL Update to update the original file

If Fuseki is not essential then you could just load your file into local memory and run the update there instead:

Model m = ModelFactory.createDefaultModel();
m.read("example.owl", "RDF/XML");

// Prepare your update...

// Create an UpdateExecution on the local model
UpdateProcessor processor = UpdateExecutionFactory.create(update, GraphStoreFactory.create(m));
processor.execute();

// Save the updated model 
updated.write(new FileOutputStream("example.owl"), "RDF/XML");

However if you want to/must stick with using Fuseki you can update your original file by retrieving the modified graph from Fuseki and writing it back out to your file e.g.

DatasetAccessor accessor = DatasetAccessorFactory.createHTTP("http://localhost:3030/ds/data");

// Download the updated model
Model updated = accessor.getModel();

// Save the updated model over the original file
updated.write(new FileOutputStream("example.owl"), "RDF/XML");

This example assumes that you have loaded the OWL file into the default graph, if not use the getModel("http://graph") overload to load the relevant named graph

这篇关于通过Java代码插入一些数据时,Sparql查询不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆