[Sesame] queries and modifies data.

Source: Internet
Author: User
The Sesame database uses Java APIs to query and modify data. 1. query data to obtain the query data (through SPARQL) publicstaticvoidtestQuery () {try {RepositoryConnectionconrepo. getConnection (); try {StringqueryStringPREFIXrk: rk. comtest + SELECT? S

The Sesame database uses Java APIs to query and modify data. 1. query data (through SPARQL) public static void testQuery () {try {RepositoryConnection con = repo. getConnection (); try {String queryString = PREFIX rk: http://rk.com/test/ + SELECT? S

The Sesame database uses Java APIs to query and modify data.

1. query data to obtain the queried data (through SPARQL)
public static void testQuery() {try {   RepositoryConnection con = repo.getConnection();   try {  String queryString = "PREFIX rk:
  " +  "SELECT ?s ?o " +  "WHERE { " +  "?s rk:type rk:CreativeWork ." +  "?s ?p ?o ." +  "} ";  TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);  long startTime = System.currentTimeMillis();  TupleQueryResult result = tupleQuery.evaluate();  long secondTime = System.currentTimeMillis();    List
 
   bindingNames = result.getBindingNames(); //get the name of binded variables  while (result.hasNext()) {     BindingSet bindingSet = result.next();     Value firstValue = bindingSet.getValue("s"); //bindingSet.getValue(bindingNames.get(0));     Value secondValue = bindingSet.getValue("o"); //bindingSet.getValue(bindingNames.get(1));     System.out.println(firstValue);     System.out.println(secondValue);     // do something interesting with the values here...  }  long endTime = System.currentTimeMillis();  System.out.println("evaluation time = "+(secondTime-startTime));  System.out.println("fetch time = "+(endTime-secondTime));   }   finally {      con.close();   }}catch (OpenRDFException e) {    // handle exceptione.printStackTrace();}}
 
2. modify data
public static void updateQuery() {String updateQuery = "PREFIX rk:
  " +"DELETE { " +"?creativeWork rk:type ?type ." +"} " +"INSERT { " +"?creativeWork rk:type ?typeUri ." +"} " +"WHERE { " +"?creativeWork rk:type ?type ." +"BIND( URI(?type) AS ?typeUri ) " +"} ";RepositoryConnection conn;try {conn = repo.getConnection();Update update = conn.prepareUpdate(QueryLanguage.SPARQL, updateQuery);update.execute();conn.commit();conn.close();} catch (RepositoryException e) {e.printStackTrace();} catch (MalformedQueryException e) {e.printStackTrace();} catch (UpdateExecutionException e) {e.printStackTrace();}}

For more information about the SPARQL language, see Learning SPARQL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.