Search in sources :

Example 11 with RyaStreamsException

use of org.apache.rya.streams.api.exception.RyaStreamsException in project incubator-rya by apache.

the class AddQueryCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final AddParameters params = new AddParameters();
    try {
        new JCommander(params, args);
    } catch (final ParameterException e) {
        throw new ArgumentsException("Could not add a new query because of invalid command line parameters.", e);
    }
    // Create the Kafka backed QueryChangeLog.
    final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
    final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
    final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
    // The AddQuery command doesn't use the scheduled service feature.
    final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
    final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
    // Execute the add query command.
    try {
        final AddQuery addQuery = new DefaultAddQuery(queryRepo);
        try {
            final Boolean isActive = Boolean.parseBoolean(params.isActive);
            final Boolean isInsert = Boolean.parseBoolean(params.isInsert);
            // If the query's results are meant to be written back to Rya, make sure it creates statements.
            if (isInsert) {
                final boolean isConstructQuery = QueryInvestigator.isConstruct(params.query);
                final boolean isInsertQuery = QueryInvestigator.isInsertWhere(params.query);
                if (isConstructQuery) {
                    System.out.println("WARNING: CONSTRUCT is part of the SPARQL Query API, so they do not normally\n" + "get written back to the triple store. Consider using an INSERT, which is\n" + "part of the SPARQL Update API, in the future.");
                }
                if (!(isConstructQuery || isInsertQuery)) {
                    throw new ArgumentsException("Only CONSTRUCT queries and INSERT updates may be inserted back to the triple store.");
                }
            }
            final StreamsQuery query = addQuery.addQuery(params.query, isActive, isInsert);
            System.out.println("Added query: " + query.getSparql());
        } catch (final RyaStreamsException e) {
            throw new ExecutionException("Unable to add the query to Rya Streams.", e);
        }
    } catch (final MalformedQueryException e) {
        throw new ArgumentsException("Could not parse the provided query.", e);
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) AddQuery(org.apache.rya.streams.api.interactor.AddQuery) DefaultAddQuery(org.apache.rya.streams.api.interactor.defaults.DefaultAddQuery) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) DefaultAddQuery(org.apache.rya.streams.api.interactor.defaults.DefaultAddQuery) JCommander(com.beust.jcommander.JCommander) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) MalformedQueryException(org.openrdf.query.MalformedQueryException) ParameterException(com.beust.jcommander.ParameterException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository)

Aggregations

RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)11 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)8 UUID (java.util.UUID)5 RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)5 CliCommand (org.springframework.shell.core.annotation.CliCommand)5 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)4 QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)4 JCommander (com.beust.jcommander.JCommander)3 ParameterException (com.beust.jcommander.ParameterException)3 Scheduler (com.google.common.util.concurrent.AbstractScheduledService.Scheduler)3 IOException (java.io.IOException)3 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)3 MalformedQueryException (org.openrdf.query.MalformedQueryException)3 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 KafkaStreams (org.apache.kafka.streams.KafkaStreams)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1