[ 
https://issues.apache.org/jira/browse/RYA-318?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16104976#comment-16104976
 ] 

ASF GitHub Bot commented on RYA-318:
------------------------------------

Github user jdasch commented on a diff in the pull request:

    https://github.com/apache/incubator-rya/pull/188#discussion_r130091994
  
    --- Diff: 
extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java
 ---
    @@ -0,0 +1,150 @@
    +/**
    + * 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.
    + */
    +package org.apache.rya.api.client.accumulo;
    +
    +import static java.util.Objects.requireNonNull;
    +
    +import java.text.DecimalFormat;
    +
    +import org.apache.accumulo.core.client.AccumuloException;
    +import org.apache.accumulo.core.client.AccumuloSecurityException;
    +import org.apache.accumulo.core.client.Connector;
    +import org.apache.log4j.Logger;
    +import org.apache.rya.accumulo.AccumuloRdfConfiguration;
    +import org.apache.rya.api.client.ExecuteSparqlQuery;
    +import org.apache.rya.api.client.InstanceDoesNotExistException;
    +import org.apache.rya.api.client.InstanceExists;
    +import org.apache.rya.api.client.LoadStatementsFile;
    +import org.apache.rya.api.client.RyaClientException;
    +import org.apache.rya.api.persist.RyaDAOException;
    +import org.apache.rya.rdftriplestore.inference.InferenceEngineException;
    +import org.apache.rya.sail.config.RyaSailFactory;
    +import org.openrdf.query.MalformedQueryException;
    +import org.openrdf.query.QueryEvaluationException;
    +import org.openrdf.query.QueryLanguage;
    +import org.openrdf.query.TupleQuery;
    +import org.openrdf.query.TupleQueryResult;
    +import org.openrdf.repository.RepositoryException;
    +import org.openrdf.repository.sail.SailRepository;
    +import org.openrdf.repository.sail.SailRepositoryConnection;
    +import org.openrdf.sail.Sail;
    +import org.openrdf.sail.SailException;
    +
    +import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
    +import edu.umd.cs.findbugs.annotations.NonNull;
    +
    +/**
    + * An Accumulo implementation of the {@link LoadStatementsFile} command.
    + */
    +@DefaultAnnotation(NonNull.class)
    +public class AccumuloExecuteSparqlQuery extends AccumuloCommand implements 
ExecuteSparqlQuery {
    +    private static final Logger log = 
Logger.getLogger(AccumuloExecuteSparqlQuery.class);
    +
    +    private final InstanceExists instanceExists;
    +
    +    /**
    +     * Constructs an instance of {@link AccumuloExecuteSparqlQuery}.
    +     *
    +     * @param connectionDetails - Details about the values that were used 
to create
    +     *   the connector to the cluster. (not null)
    +     * @param connector - Provides programmatic access to the instance of 
Accumulo
    +     *   that hosts Rya instance. (not null)
    +     */
    +    public AccumuloExecuteSparqlQuery(final AccumuloConnectionDetails 
connectionDetails, final Connector connector) {
    +        super(connectionDetails, connector);
    +        instanceExists = new AccumuloInstanceExists(connectionDetails, 
connector);
    +    }
    +
    +
    +    @Override
    +    public String executeSparqlQuery(final String ryaInstanceName, final 
String sparqlQuery)
    +            throws InstanceDoesNotExistException, RyaClientException {
    +        requireNonNull(ryaInstanceName);
    +        requireNonNull(sparqlQuery);
    +
    +        // Ensure the Rya Instance exists.
    +        if(!instanceExists.exists(ryaInstanceName)) {
    +            throw new InstanceDoesNotExistException(String.format("There 
is no Rya instance named '%s'.", ryaInstanceName));
    +        }
    +
    +
    +        Sail sail = null;
    +        SailRepository sailRepo = null;
    +        SailRepositoryConnection sailRepoConn = null;
    +
    +        try {
    +            // Get a Sail object that is connected to the Rya instance.
    +            final AccumuloRdfConfiguration ryaConf = 
getAccumuloConnectionDetails().buildAccumuloRdfConfiguration(ryaInstanceName);
    +            sail = RyaSailFactory.getInstance(ryaConf);
    +
    +            // Load the file.
    +            sailRepo = new SailRepository(sail);
    +            sailRepoConn = sailRepo.getConnection();
    +            final long start = System.currentTimeMillis();
    +            final TupleQuery tupleQuery = 
sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQuery);
    +            final TupleQueryResult result = tupleQuery.evaluate();
    +            final StringBuilder sb = new StringBuilder();
    +            final String newline = "\n";
    +            sb.append("Query Result:").append(newline);
    +            sb.append("Binding Names: 
").append(result.getBindingNames().toString()).append(newline);
    +
    +            int count = 0;
    +            while(result.hasNext()) {
    +                sb.append(result.next().toString()).append(newline);
    +                count++;
    +            }
    +            final String seconds = new 
DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
    +            sb.append("Retrieved ").append(count).append(" results in 
").append(seconds).append(" seconds.");
    --- End diff --
    
    I think it is appropriate.  The contract specifies that this method will 
return "A user-friendly String representation of the query results."  If you 
use other query shells, like impala, they also provide query execution timing.  
It provides basic feedback to the user for how performant their query is.  If 
you prefer, we could move the timing section to rya.console and append it to 
the content that is returned from this method.  Let me know which you prefer, 
but I really like seeing timing when I issue a query.



> Add Loading Data and Executing SPARQL Queries to the Rya Shell
> --------------------------------------------------------------
>
>                 Key: RYA-318
>                 URL: https://issues.apache.org/jira/browse/RYA-318
>             Project: Rya
>          Issue Type: Sub-task
>          Components: clients
>            Reporter: Jeff Dasch
>            Assignee: Jeff Dasch
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to