Search in sources :

Example 1 with InMemoryQueryRepository

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

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

the class RunQueryCommandIT method setup.

@Before
public void setup() {
    // Make sure the topic that the change log uses exists.
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
    kafka.createTopic(changeLogTopic);
    // Setup the QueryRepository used by the test.
    final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
    // Initialize the Statements Producer and the Results Consumer.
    stmtProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityStatementSerializer.class);
    resultConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityBindingSetDeserializer.class);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) VisibilityStatementSerializer(org.apache.rya.streams.kafka.serialization.VisibilityStatementSerializer) VisibilityBindingSetDeserializer(org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer) QueryChange(org.apache.rya.streams.api.queries.QueryChange) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) Before(org.junit.Before)

Example 3 with InMemoryQueryRepository

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

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

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

the class AddQueryCommandIT method setup.

@Before
public void setup() {
    // Make sure the topic that the change log uses exists.
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
    kafka.createTopic(changeLogTopic);
    // Setup the QueryRepository used by the test.
    final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
}
Also used : QueryChange(org.apache.rya.streams.api.queries.QueryChange) 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) Before(org.junit.Before)

Aggregations

InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)11 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)10 QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)7 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 QueryChange (org.apache.rya.streams.api.queries.QueryChange)5 KafkaQueryChangeLog (org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog)5 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)4 Before (org.junit.Before)4 UUID (java.util.UUID)3 VisibilityBindingSetDeserializer (org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer)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 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