Search in sources :

Example 1 with QueryRepository

use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.

the class KafkaRunQueryIT method runQuery.

@Test
public void runQuery() throws Exception {
    // Setup some constant that will be used through the test.
    final String ryaInstance = UUID.randomUUID().toString();
    final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
    // This query is completely in memory, so it doesn't need to be closed.
    final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
    // Add the query to the query repository.
    final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
    final UUID queryId = sQuery.getQueryId();
    final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
    // The thread that will run the tested interactor.
    final Thread testThread = new Thread() {

        @Override
        public void run() {
            final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
            try {
                runQuery.run(queryId);
            } catch (final RyaStreamsException e) {
            // Do nothing. Test will still fail because the expected results will be missing.
            }
        }
    };
    // Create the topics.
    kafka.createTopic(statementsTopic);
    kafka.createTopic(resultsTopic);
    // Create the statements that will be loaded.
    final ValueFactory vf = new ValueFactoryImpl();
    final List<VisibilityStatement> statements = new ArrayList<>();
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    // Create the expected results.
    final List<VisibilityBindingSet> expected = new ArrayList<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Charlie"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    // Execute the test. This will result in a set of results that were read from the results topic.
    final List<VisibilityBindingSet> results;
    try {
        // Wait for the program to start.
        testThread.start();
        Thread.sleep(2000);
        // Write some statements to the program.
        final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
        loadStatements.fromCollection(statements);
        // Read the output of the streams program.
        consumer.subscribe(Lists.newArrayList(resultsTopic));
        results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
    } finally {
        // Tear down the test.
        testThread.interrupt();
        testThread.join(3000);
    }
    // Show the read results matched the expected ones.
    assertEquals(expected, results);
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RunQuery(org.apache.rya.streams.api.interactor.RunQuery) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) ValueFactory(org.openrdf.model.ValueFactory) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with QueryRepository

use of org.apache.rya.streams.api.queries.QueryRepository 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 3 with QueryRepository

use of org.apache.rya.streams.api.queries.QueryRepository 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 4 with QueryRepository

use of org.apache.rya.streams.api.queries.QueryRepository 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 QueryRepository

use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.

the class KafkaRyaStreamsClientFactory method make.

/**
 * Initialize a {@link RyaStreamsClient} that will interact with an instance of Rya Streams
 * that is backed by Kafka.
 *
 * @param ryaInstance - The name of the Rya Instance the client is connected to. (not null)
 * @param kafkaHostname - The hostname of the Kafka Broker.
 * @param kafkaPort - The port of the Kafka Broker.
 * @return The initialized commands.
 */
public static RyaStreamsClient make(final String ryaInstance, final String kafkaHostname, final int kafkaPort) {
    requireNonNull(ryaInstance);
    requireNonNull(kafkaHostname);
    // Setup Query Repository used by the Kafka Rya Streams subsystem.
    final Producer<?, QueryChange> queryProducer = makeProducer(kafkaHostname, kafkaPort, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = fromStartConsumer(kafkaHostname, kafkaPort, StringDeserializer.class, QueryChangeDeserializer.class);
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic(ryaInstance);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    final QueryRepository queryRepo = new InMemoryQueryRepository(changeLog, SCHEDULER);
    // Create the Rya Streams client that is backed by a Kafka Query Change Log.
    return new RyaStreamsClient(new DefaultAddQuery(queryRepo), new DefaultGetQuery(queryRepo), new DefaultDeleteQuery(queryRepo), new KafkaGetQueryResultStream<>(kafkaHostname, "" + kafkaPort, VisibilityStatementDeserializer.class), new KafkaGetQueryResultStream<>(kafkaHostname, "" + kafkaPort, VisibilityBindingSetDeserializer.class), new DefaultListQueries(queryRepo), new DefaultStartQuery(queryRepo), new DefaultStopQuery(queryRepo)) {

        /**
         * Close the QueryRepository used by the returned client.
         */
        @Override
        public void close() {
            try {
                queryRepo.stopAndWait();
            } catch (final Exception e) {
                log.warn("Couldn't close a QueryRepository.", e);
            }
        }
    };
}
Also used : DefaultGetQuery(org.apache.rya.streams.api.interactor.defaults.DefaultGetQuery) VisibilityBindingSetDeserializer(org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer) DefaultDeleteQuery(org.apache.rya.streams.api.interactor.defaults.DefaultDeleteQuery) VisibilityStatementDeserializer(org.apache.rya.streams.kafka.serialization.VisibilityStatementDeserializer) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) DefaultAddQuery(org.apache.rya.streams.api.interactor.defaults.DefaultAddQuery) DefaultStopQuery(org.apache.rya.streams.api.interactor.defaults.DefaultStopQuery) QueryChange(org.apache.rya.streams.api.queries.QueryChange) DefaultStartQuery(org.apache.rya.streams.api.interactor.defaults.DefaultStartQuery) 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)

Aggregations

QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)9 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)7 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)6 JCommander (com.beust.jcommander.JCommander)5 ParameterException (com.beust.jcommander.ParameterException)5 Scheduler (com.google.common.util.concurrent.AbstractScheduledService.Scheduler)5 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)5 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)4 UUID (java.util.UUID)3 AddQuery (org.apache.rya.streams.api.interactor.AddQuery)3 Test (org.junit.Test)3 DefaultAddQuery (org.apache.rya.streams.api.interactor.defaults.DefaultAddQuery)2 DefaultDeleteQuery (org.apache.rya.streams.api.interactor.defaults.DefaultDeleteQuery)2 DefaultListQueries (org.apache.rya.streams.api.interactor.defaults.DefaultListQueries)2 VisibilityBindingSetDeserializer (org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer)2 VisibilityStatementDeserializer (org.apache.rya.streams.kafka.serialization.VisibilityStatementDeserializer)2 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)2 MalformedQueryException (org.openrdf.query.MalformedQueryException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1