1

Can somebody give one java example of sparql insert/Delete query in Stardog.

there is only queryExecution.execSelect() method available. there is no queryExecution.execInsert() or queryExecution.execDelete() available. Please give one working example.

EDIT

I've found this this from stardog docs page.

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

As of 1.1.5, Stardog's SPARQL 1.1 support does not include: UPDATE query language

does that means no way out for editing a tuple once entered?

Vishnudev K
  • 2,874
  • 3
  • 27
  • 42
  • This can be of help : http://mail-archives.apache.org/mod_mbox/jena-users/201209.mbox/%3COFAB6B5A21.2EBF2448-ONC1257A7E.007AEBC5-C1257A7E.007BB147@netage.nl%3E – AllTooSir Apr 17 '13 at 08:21

2 Answers2

2

Stardog does not yet support SPARQL update, but as was pointed out to you on the mailing list, there are 5 ways you can modify the data once it's loaded. You can use our HTTP protocol directly, any of the 3 Java API's we support, or you can use the command line interface.

Michael
  • 4,858
  • 19
  • 32
  • It will be really helpful if you can provide an example of Java API way to modify data once it is loaded. – Vishnudev K Apr 17 '13 at 15:17
  • 2
    Examples for using every single API Stardog supports are provided *in* the distribution. Further, there is rather extensive documentation on the website which shows code examples. – Michael Apr 17 '13 at 15:31
  • 2
    Per @Michael's comment - see Programming with Java (http://stardog.com/docs/java/) to get started or see the examples in the Stardog distribution – RobV Apr 17 '13 at 18:59
  • For reference here is the mailing list thread referenced - https://groups.google.com/a/clarkparsia.com/forum/#!topic/stardog/nNklIn37O2A – RobV Apr 17 '13 at 19:02
  • thank you all.. got a sample code from sample programs in the distribution. Modified it and made it a simple example. please find the next answer. – Vishnudev K Apr 18 '13 at 10:50
  • @Michael Unfortunately, it needs to be updated in some other places, too. I found this question through [Stardog data loading and Jena](http://stackoverflow.com/q/24332922/1281433), and that got me looking through the documentation, and I think I found that old link in there somewhere too (but I can't find it at the moment, unfortunately). – Joshua Taylor Jun 21 '14 at 12:53
  • Just an update - Stardog does support SPARQL Update now - http://docs.stardog.com/#_sparql_update – user1156544 Apr 29 '16 at 16:38
2

Below one is a sample program of inserting a graph and removing it.

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();
}
}
Vishnudev K
  • 2,874
  • 3
  • 27
  • 42