Search in sources :

Example 1 with Scheduler

use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.

the class ListQueriesCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final KafkaParameters params = new KafkaParameters();
    try {
        new JCommander(params, args);
    } catch (final ParameterException e) {
        throw new ArgumentsException("Could not list the queries 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 ListQueries 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 list queries command.
    try {
        final ListQueries listQueries = new DefaultListQueries(queryRepo);
        try {
            final Set<StreamsQuery> queries = listQueries.all();
            System.out.println(formatQueries(queries));
        } catch (final RyaStreamsException e) {
            System.err.println("Unable to retrieve the queries.");
            e.printStackTrace();
            System.exit(1);
        }
    } catch (final Exception e) {
        System.err.println("Problem encountered while closing the QueryRepository.");
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) ListQueries(org.apache.rya.streams.api.interactor.ListQueries) DefaultListQueries(org.apache.rya.streams.api.interactor.defaults.DefaultListQueries) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) JCommander(com.beust.jcommander.JCommander) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) DefaultListQueries(org.apache.rya.streams.api.interactor.defaults.DefaultListQueries)

Example 2 with Scheduler

use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.

the class DeleteQueryCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final RemoveParameters params = new RemoveParameters();
    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 DeleteQuery 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 delete query command.
    try {
        final DeleteQuery deleteQuery = new DefaultDeleteQuery(queryRepo);
        try {
            deleteQuery.delete(UUID.fromString(params.queryId));
            System.out.println("Deleted query: " + params.queryId);
        } catch (final RyaStreamsException e) {
            System.err.println("Unable to delete query with ID: " + params.queryId);
            e.printStackTrace();
            System.exit(1);
        }
    } catch (final Exception e) {
        System.err.println("Problem encountered while closing the QueryRepository.");
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) DefaultDeleteQuery(org.apache.rya.streams.api.interactor.defaults.DefaultDeleteQuery) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) JCommander(com.beust.jcommander.JCommander) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) DeleteQuery(org.apache.rya.streams.api.interactor.DeleteQuery) DefaultDeleteQuery(org.apache.rya.streams.api.interactor.defaults.DefaultDeleteQuery)

Example 3 with Scheduler

use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler in project incubator-rya by apache.

the class QueryManagerDaemon method init.

@Override
public void init(final DaemonContext context) throws DaemonInitException, Exception {
    requireNonNull(context);
    // Parse the command line arguments for the configuration file to use.
    final String[] args = context.getArguments();
    final DaemonParameters params = new DaemonParameters();
    try {
        new JCommander(params).parse(args);
    } catch (final ParameterException e) {
        throw new DaemonInitException("Unable to parse the command line arguments.", e);
    }
    final Path configFile = params.config != null ? Paths.get(params.config) : DEFAULT_CONFIGURATION_PATH;
    log.info("Loading the following configuration file: " + configFile);
    // Unmarshall the configuration file into an object.
    final QueryManagerConfig config;
    try (final InputStream stream = Files.newInputStream(configFile)) {
        config = QueryManagerConfigUnmarshaller.unmarshall(stream);
    } catch (final JAXBException | SAXException e) {
        throw new DaemonInitException("Unable to marshall the configuration XML file: " + configFile, e);
    }
    // Read the source polling period from the configuration.
    final QueryChanngeLogDiscoveryPeriod periodConfig = config.getPerformanceTunning().getQueryChanngeLogDiscoveryPeriod();
    final long period = periodConfig.getValue().longValue();
    final TimeUnit units = TimeUnit.valueOf(periodConfig.getUnits().toString());
    log.info("Query Change Log Polling Period: " + period + " " + units);
    final Scheduler scheduler = Scheduler.newFixedRateSchedule(0, period, units);
    // Initialize a QueryChangeLogSource.
    final Kafka kafka = config.getQueryChangeLogSource().getKafka();
    log.info("Kafka Source: " + kafka.getHostname() + ":" + kafka.getPort());
    final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getHostname(), kafka.getPort(), scheduler);
    // Initialize a QueryExecutor.
    final String zookeeperServers = config.getQueryExecutor().getLocalKafkaStreams().getZookeepers();
    final KafkaStreamsFactory streamsFactory = new SingleThreadKafkaStreamsFactory(kafka.getHostname() + ":" + kafka.getPort());
    final QueryExecutor queryExecutor = new LocalQueryExecutor(new CreateKafkaTopic(zookeeperServers), streamsFactory);
    // Initialize the QueryManager using the configured resources.
    manager = new QueryManager(queryExecutor, source, period, units);
}
Also used : Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) Kafka(org.apache.rya.streams.querymanager.xml.Kafka) SAXException(org.xml.sax.SAXException) LocalQueryExecutor(org.apache.rya.streams.querymanager.kafka.LocalQueryExecutor) JCommander(com.beust.jcommander.JCommander) LocalQueryExecutor(org.apache.rya.streams.querymanager.kafka.LocalQueryExecutor) TimeUnit(java.util.concurrent.TimeUnit) ParameterException(com.beust.jcommander.ParameterException) Path(java.nio.file.Path) QueryChanngeLogDiscoveryPeriod(org.apache.rya.streams.querymanager.xml.QueryManagerConfig.PerformanceTunning.QueryChanngeLogDiscoveryPeriod) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) InputStream(java.io.InputStream) KafkaQueryChangeLogSource(org.apache.rya.streams.querymanager.kafka.KafkaQueryChangeLogSource) JAXBException(javax.xml.bind.JAXBException) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) SingleThreadKafkaStreamsFactory(org.apache.rya.streams.kafka.SingleThreadKafkaStreamsFactory) SingleThreadKafkaStreamsFactory(org.apache.rya.streams.kafka.SingleThreadKafkaStreamsFactory) KafkaQueryChangeLogSource(org.apache.rya.streams.querymanager.kafka.KafkaQueryChangeLogSource) DaemonInitException(org.apache.commons.daemon.DaemonInitException) QueryManagerConfig(org.apache.rya.streams.querymanager.xml.QueryManagerConfig)

Example 4 with Scheduler

use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler 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)

Example 5 with Scheduler

use of com.google.common.util.concurrent.AbstractScheduledService.Scheduler 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

JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 Scheduler (com.google.common.util.concurrent.AbstractScheduledService.Scheduler)6 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)5 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)5 QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)5 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)4 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)3 UUID (java.util.UUID)2 MalformedQueryException (org.openrdf.query.MalformedQueryException)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JAXBException (javax.xml.bind.JAXBException)1 DaemonInitException (org.apache.commons.daemon.DaemonInitException)1 AddQuery (org.apache.rya.streams.api.interactor.AddQuery)1 DeleteQuery (org.apache.rya.streams.api.interactor.DeleteQuery)1 ListQueries (org.apache.rya.streams.api.interactor.ListQueries)1