stardog sparql在java中插入查询的示例

时间:2013-04-17 07:17:27

标签: java sparql stardog

有人可以在Stardog中给出一个sparql insert / Delete查询的java示例。

只有queryExecution.execSelect()方法可用。 没有queryExecution.execInsert()queryExecution.execDelete()可用。 请举一个有效的例子。

修改

我是从stardog docs页面找到的。

http://stardog.com/docs/#notes

从1.1.5开始,Stardog的SPARQL 1.1支持不包括:UPDATE查询语言

这是否意味着在输入元组时无法编辑元组?

2 个答案:

答案 0 :(得分:2)

Stardog尚不支持SPARQL更新,但正如您在邮件列表中指出的那样,有五种方法可以在加载后修改数据。您可以直接使用我们的HTTP协议,我们支持的3个Java API中的任何一个,或者您可以使用命令行界面。

答案 1 :(得分:2)

下面是一个插入图表并将其删除的示例程序。

package com.query;

import java.util.List;

import org.openrdf.model.Graph;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.query.QueryEvaluationException;

import com.clarkparsia.stardog.StardogDBMS;
import com.clarkparsia.stardog.StardogException;
import com.clarkparsia.stardog.api.Connection;
import com.clarkparsia.stardog.api.ConnectionConfiguration;
import com.clarkparsia.stardog.jena.SDJenaFactory;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;

public class test {
public static void main(String[] args) throws StardogException, QueryEvaluationException {
    String appDbName ="memDb";
    String selectQuery="SELECT DISTINCT ?s ?p ?o WHERE { ?s ?p  ?o }";
    StardogDBMS dbms = StardogDBMS.toServer("snarl://localhost:5820/")
            .credentials("admin", "admin".toCharArray()).login();
    List<String> dbList = (List<String>) dbms.list();
    if (dbList.contains(appDbName)) {
        System.out.println("droping " + appDbName);
        dbms.drop(appDbName);
    }
    dbms.createMemory(appDbName);
    dbms.logout();
    Connection aConn = ConnectionConfiguration
            .to("memDb")    // the name of the db to connect to
            .credentials("admin", "admin") // credentials to use while connecting
            .url("snarl://localhost:5820/")
            .connect(); 
    Model aModel = SDJenaFactory.createModel(aConn);
    System.out.println("################ GRAPH IS EMPTY B4 SUBMITTING ="+aModel.getGraph()+ "################");

    URI order = ValueFactoryImpl.getInstance().createURI("RDF:president1");
    URI givenName = ValueFactoryImpl.getInstance().createURI("RDF:lincoln");
    URI predicate = ValueFactoryImpl.getInstance().createURI("RDF:GivenNane");


    Statement aStmt = ValueFactoryImpl.getInstance().createStatement(order,predicate,givenName);
    Graph aGraph = new GraphImpl();
    aGraph.add(aStmt);
    insert(aConn, aGraph);


    ParameterizedSparqlString paraQuery = new ParameterizedSparqlString(selectQuery);
    QueryExecution qExecution = QueryExecutionFactory.create(paraQuery.asQuery(),aModel);
    ResultSet queryResult = qExecution.execSelect();
    System.out.println("############### 1 TUPPLE CAME AFTER INSERT ################");
    ResultSetFormatter.out(System.out, queryResult);

    aGraph.add(aStmt);
    remove(aConn, aGraph);


    paraQuery = new ParameterizedSparqlString(selectQuery);
    qExecution = QueryExecutionFactory.create(paraQuery.asQuery(),aModel);
    queryResult = qExecution.execSelect();
    System.out.println("################ DB AGAIN EMPTY AFTER REMOVE ################");
    ResultSetFormatter.out(System.out, queryResult);


    System.out.println("closing connection and model");
    aModel.close();
    aConn.close();  
}
private static void insert(final Connection theConn, final Graph theGraph) throws StardogException {
    theConn.begin();
    theConn.add().graph(theGraph);
    theConn.commit();
}

private static void remove(final Connection theConn, final Graph theGraph) throws StardogException {
    theConn.begin();
    theConn.remove().graph(theGraph);
    theConn.commit();
}
}