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);
}
}
Aggregations