Search in sources :

Example 21 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class KafkaRunQuery method run.

@Override
public void run(final UUID queryId) throws RyaStreamsException {
    requireNonNull(queryId);
    // Fetch the query from the repository. Throw an exception if it isn't present.
    final Optional<StreamsQuery> query = queryRepo.get(queryId);
    if (!query.isPresent()) {
        throw new RyaStreamsException("Could not run the Query with ID " + queryId + " because no such query " + "is currently registered.");
    }
    // Build a processing topology using the SPARQL, provided statements topic, and provided results topic.
    final String sparql = query.get().getSparql();
    final TopologyBuilder topologyBuilder;
    try {
        topologyBuilder = topologyFactory.build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
    } catch (final Exception e) {
        throw new RyaStreamsException("Could not run the Query with ID " + queryId + " because a processing " + "topolgoy could not be built for the SPARQL " + sparql, e);
    }
    // Setup the Kafka Stream program.
    final Properties streamsProps = new Properties();
    streamsProps.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaHostname + ":" + kafkaPort);
    // Use the Query ID as the Application ID to ensure we resume where we left off the last time this command was run.
    streamsProps.put(StreamsConfig.APPLICATION_ID_CONFIG, "KafkaRunQuery-" + queryId);
    // Always start at the beginning of the input topic.
    streamsProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    final KafkaStreams streams = new KafkaStreams(topologyBuilder, new StreamsConfig(streamsProps));
    // If an unhandled exception is thrown, rethrow it.
    streams.setUncaughtExceptionHandler((t, e) -> {
        // Log the problem and kill the program.
        log.error("Unhandled exception while processing the Rya Streams query. Shutting down.", e);
        System.exit(1);
    });
    // Setup a shutdown hook that kills the streams program at shutdown.
    final CountDownLatch awaitTermination = new CountDownLatch(1);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            awaitTermination.countDown();
        }
    });
    // Run the streams program and wait for termination.
    streams.start();
    try {
        awaitTermination.await();
    } catch (final InterruptedException e) {
        log.warn("Interrupted while waiting for termination. Shutting down.");
    }
    streams.close();
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) StreamsConfig(org.apache.kafka.streams.StreamsConfig)

Example 22 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class RyaStreamsCommandsTest method stopQuery_alreadyStopped.

@Test
public void stopQuery_alreadyStopped() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final StopQuery stopQuery = mock(StopQuery.class);
    when(mockClient.getStopQuery()).thenReturn(stopQuery);
    final GetQuery getQuery = mock(GetQuery.class);
    when(mockClient.getGetQuery()).thenReturn(getQuery);
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Report the query as not running.
    final UUID queryId = UUID.randomUUID();
    when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", false, false)));
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.stopQuery(queryId.toString());
    // Verify the interactor was not invoked with the provided parameters.
    verify(stopQuery, never()).stop(queryId);
    // Verify a message is printed to the user.
    final String expected = "That query is already stopped.";
    assertEquals(expected, message);
}
Also used : StopQuery(org.apache.rya.streams.api.interactor.StopQuery) RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) GetQuery(org.apache.rya.streams.api.interactor.GetQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) UUID(java.util.UUID) Test(org.junit.Test)

Example 23 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class RyaStreamsCommandsTest method addQuery_doNotInsertQuery.

@Test
public void addQuery_doNotInsertQuery() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final AddQuery addQuery = mock(AddQuery.class);
    when(mockClient.getAddQuery()).thenReturn(addQuery);
    final String sparql = "SELECT * WHERE { ?a ?b ?c }";
    final StreamsQuery addedQuery = new StreamsQuery(UUID.randomUUID(), sparql, true, false);
    when(addQuery.addQuery(eq(sparql), eq(true), eq(false))).thenReturn(addedQuery);
    // Mock a SPARQL prompt that a user entered a query through.
    final SparqlPrompt prompt = mock(SparqlPrompt.class);
    when(prompt.getSparql()).thenReturn(Optional.of(sparql));
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, prompt, mock(ConsolePrinter.class));
    final String message = commands.addQuery(false, false);
    // Verify the interactor was invoked with the provided input.
    verify(addQuery).addQuery(sparql, true, false);
    // Verify a message is printed to the user.
    final String expected = "The added query's ID is " + addedQuery.getQueryId();
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) AddQuery(org.apache.rya.streams.api.interactor.AddQuery) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 24 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class RyaStreamsCommandsTest method printQueryDetails.

@Test
public void printQueryDetails() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final GetQuery getQuery = mock(GetQuery.class);
    when(mockClient.getGetQuery()).thenReturn(getQuery);
    final UUID queryId = UUID.fromString("da55cea5-c21c-46a5-ab79-5433eef4efaa");
    final StreamsQuery query = new StreamsQuery(queryId, "SELECT * WHERE { ?a ?b ?c . }", true, false);
    when(getQuery.getQuery(queryId)).thenReturn(java.util.Optional.of(query));
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.printQueryDetails(queryId.toString());
    // Verify the correct report is returned.
    final String expected = " Query ID: da55cea5-c21c-46a5-ab79-5433eef4efaa\n" + "Is Active: true\n" + "Is Insert: false\n" + "   SPARQL: select ?a ?b ?c\n" + "           where {\n" + "             ?a ?b ?c.\n" + "           }\n";
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) GetQuery(org.apache.rya.streams.api.interactor.GetQuery) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) UUID(java.util.UUID) Test(org.junit.Test)

Example 25 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class RyaStreamsCommandsTest method addQuery_insertQueryNotCorrectType.

@Test(expected = RuntimeException.class)
public void addQuery_insertQueryNotCorrectType() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final AddQuery addQuery = mock(AddQuery.class);
    when(mockClient.getAddQuery()).thenReturn(addQuery);
    final String sparql = "SELECT * WHERE { ?a ?b ?c }";
    final StreamsQuery addedQuery = new StreamsQuery(UUID.randomUUID(), sparql, true, true);
    when(addQuery.addQuery(eq(sparql), eq(false), eq(true))).thenReturn(addedQuery);
    // Mock a SPARQL prompt that a user entered a query through.
    final SparqlPrompt prompt = mock(SparqlPrompt.class);
    when(prompt.getSparql()).thenReturn(Optional.of(sparql));
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, prompt, mock(ConsolePrinter.class));
    commands.addQuery(true, true);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) AddQuery(org.apache.rya.streams.api.interactor.AddQuery) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Aggregations

StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)51 Test (org.junit.Test)40 UUID (java.util.UUID)24 RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 RyaClient (org.apache.rya.api.client.RyaClient)10 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)10 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)10 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)10 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)8 KafkaStreamsFactory (org.apache.rya.streams.kafka.KafkaStreamsFactory)8 CreateKafkaTopic (org.apache.rya.streams.kafka.interactor.CreateKafkaTopic)8 QueryExecutor (org.apache.rya.streams.querymanager.QueryExecutor)8 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)7 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)7 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)6 HashSet (java.util.HashSet)5