Search in sources :

Example 1 with KafkaRunQuery

use of org.apache.rya.streams.kafka.interactor.KafkaRunQuery in project incubator-rya by apache.

the class RunQueryCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final RunParameters params = new RunParameters();
    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 RunQuery command doesn't use the scheduled service feature.
    final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
    final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
    // Look up the query to be executed from the change log.
    try {
        try {
            final UUID queryId = UUID.fromString(params.queryId);
            final Optional<StreamsQuery> query = queryRepo.get(queryId);
            if (!query.isPresent()) {
                throw new ArgumentsException("There is no registered query for queryId " + params.queryId);
            }
            // Make sure the topics required by the application exists for the specified Rya instances.
            final Set<String> topics = new HashSet<>();
            topics.add(KafkaTopics.statementsTopic(params.ryaInstance));
            topics.add(KafkaTopics.queryResultsTopic(params.ryaInstance, queryId));
            KafkaTopics.createTopics(params.zookeeperServers, topics, 1, 1);
            // Run the query that uses those topics.
            final KafkaRunQuery runQuery = new KafkaRunQuery(params.kafkaIP, params.kafkaPort, KafkaTopics.statementsTopic(params.ryaInstance), KafkaTopics.queryResultsTopic(params.ryaInstance, queryId), queryRepo, new TopologyFactory());
            runQuery.run(queryId);
        } catch (final Exception e) {
            throw new ExecutionException("Could not execute the Run Query command.", e);
        }
    } catch (final ExecutionException e) {
        // Rethrow the exceptions that are advertised by execute.
        throw e;
    } catch (final Exception e) {
        throw new ExecutionException("Problem encountered while closing the QueryRepository.", e);
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) ParameterException(com.beust.jcommander.ParameterException) KafkaRunQuery(org.apache.rya.streams.kafka.interactor.KafkaRunQuery) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 Scheduler (com.google.common.util.concurrent.AbstractScheduledService.Scheduler)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)1 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)1 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)1 QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)1 KafkaRunQuery (org.apache.rya.streams.kafka.interactor.KafkaRunQuery)1 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)1