【Sesame】查詢與修改資料

來源:互聯網
上載者:User

Sesame資料庫利用Java API查詢資料與修改資料。

1. 查詢資料擷取查詢資料(通過SPARQL)
public static void testQuery() {try {   RepositoryConnection con = repo.getConnection();   try {  String queryString = "PREFIX rk:<http://rk.com/test/> " +  "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<String> 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. 修改資料
public static void updateQuery() {String updateQuery = "PREFIX rk:<http://rk.com/test/> " +"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();}}

具體SPARQL語言可以參考書籍《Learning SPARQL》。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.