Use apache jena QueryExecutionFactory - Java Big Data

Java examples for Big Data:apache jena

Description

Use apache jena QueryExecutionFactory

Demo Code

/*// 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.*;
//import org.apache.jena.util.FileManager;

//import java.io.*;

/** Tutorial 8 - demonstrate Selector methods
 */
public class Tutorial08 extends Object {

    static final String inputFileName = "vc-db-1.rdf";

    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, "" );

        // 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}";

        Query query = QueryFactory.create(queryString2);

        // Execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.sparqlService(
                "http://localhost:3030/ds/query", query); //create(query, model);
        ResultSet results = 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");
        //        }

    }
}

Related Tutorials