Java examples for Big Data:apache jena
demonstrate Selector methods in apache jena
/*/* w w w.j a v a 2 s .c om*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.apache.jena.query.*; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; import org.apache.jena.util.FileManager; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.util.Iterator; //import org.apache.jena.util.FileManager; //import java.io.*; /** Tutorial 8 - demonstrate Selector methods */ public class Tutorial08_2 extends Object { static final String inputFileName = "nquads.nq"; public static void main(String args[]) { // create an empty model //Model model = ModelFactory.createDefaultModel(); // // // use the FileManager to find the input file // InputStream in = FileManager.get().open(inputFileName); // if (in == null) { // throw new IllegalArgumentException( "File: " + inputFileName + " not found"); // } // // // read the RDF/XML file // model.read(in, "" ); String nquad = "<http://a.example/s> <http://a.example/p> \"x\" <fgh> . <http://a.example/s> <http://a.example/p> \"x\" <huj> ."; Dataset ds = DatasetFactory.createTxnMem(); DatasetAccessor dsa = DatasetAccessorFactory .createHTTP("http://localhost:3030/test"); RDFDataMgr.read(ds, new StringReader(nquad), null, Lang.NQUADS); //Dataset ds = RDFDataMgr.loadDataset(inputFileName, Lang.NQUADS); //model.radd("fgh", ds.getDefaultModel()); //RDFDataMgr.write(System.out, ds, Lang.NQUADS) ; Iterator<String> listNames = ds.listNames(); while (listNames.hasNext()) { String name = listNames.next(); dsa.add(name, ds.getNamedModel(name)); } // DatasetAccessorFactory.createHTTP("http://localhost:3030/test").add("demo", fin); //dsa.add("abc", model); //model.write(System.out, "RDF/XML"); //DatasetAccessor ds = DatasetAccessorFactory.createHTTP("http://localhost:3030/test"); // DatasetAccessorFactory.createHTTP("http://localhost:3030/test").add("demo", fin); //ds.add("demo", model); //model.write(System.out, "RDF/XML"); // Create a new query // String queryString = // "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + // "SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\" }"; // // String queryString2 = // "SELECT ?givenName\n" + // "\tWHERE\n" + // "\t { ?y <http://www.w3.org/2001/vcard-rdf/3.0#Family> \"Smith\" .\n" + // "\t \t?y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?givenName .\n" + // "\t \t}"; // // String queryString3 = "CREATE GRAPH test"; // // Query query = QueryFactory.create(queryString3); // //// Execute the query and obtain results // QueryExecution qe = QueryExecutionFactory.sparqlService("http://localhost:3030/ds", query); // //QueryExecutionFactory.create(query, model); //QueryExecutionFactory.sparqlService("http://localhost:3030/ds", query); //create(query, model); // qe.execSelect(); // //// Output query results // //ResultSetFormatter.out(System.out, results, query); // //// Important - free up resources used running the query // qe.close(); // Bag smiths = model.createBag(); // // // select all the resources with a VCARD.FN property // // whose value ends with "Smith" // StmtIterator iter = model.listStatements( // new // SimpleSelector(null, VCARD.FN, (RDFNode) null) { // @Override // public boolean selects(Statement s) { // return s.getString().endsWith("Smith"); // } // }); // //// if (iter.hasNext()) { //// System.out.println("The database contains vcards for:"); //// while (iter.hasNext()) { //// System.out.println(" " + iter.nextStatement() //// .getString()); //// } //// } else { //// System.out.println("No Smith's were found in the database"); //// } // // //add smiths to a bag // while(iter.hasNext()){ // // smiths.add(iter.nextStatement().getSubject()); // // } // // //print out members of container BAG // NodeIterator iter2 = smiths.iterator(); // if(iter2.hasNext()){ // System.out.println("the bag contains:"); // while(iter2.hasNext()){ // System.out.println(" " + // ((Resource) iter2.next()) // .getProperty(VCARD.FN) // .getString()); // } // }else{ // System.out.println("BAG empty"); // } } }