Search in sources :

Example 1 with RyaStreamsClient

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

the class RyaStreamsCommands method configureRyaStreams.

@CliCommand(value = STREAMS_CONFIGURE_CMD, help = "Connect a Rya Streams subsystem to a Rya Instance.")
public String configureRyaStreams(@CliOption(key = { "kafkaHostname" }, mandatory = true, help = "The hostname of the Kafka Broker.") final String kafkaHostname, @CliOption(key = { "kafkaPort" }, mandatory = true, help = "The port of the Kafka Broker.") final int kafkaPort) {
    // If this instance was connected to a different Rya Streams subsystem, then close that client.
    final Optional<RyaStreamsClient> oldClient = state.getShellState().getRyaStreamsCommands();
    if (oldClient.isPresent()) {
        try {
            oldClient.get().close();
        } catch (final Exception e) {
            System.err.print("Warning: Could not close the old Rya Streams Client.");
            e.printStackTrace();
        }
    }
    // Update the Rya Details for the connected Rya Instance.
    final String ryaInstance = state.getShellState().getRyaInstanceName().get();
    final RyaClient ryaClient = state.getShellState().getConnectedCommands().get();
    try {
        final RyaStreamsDetails streamsDetails = new RyaStreamsDetails(kafkaHostname, kafkaPort);
        ryaClient.getSetRyaStreamsConfiguration().setRyaStreamsConfiguration(ryaInstance, streamsDetails);
    } catch (final RyaClientException e) {
        throw new RuntimeException("Could not update the Rya instance's Rya Details to include the new " + "information. This command failed to complete.", e);
    }
    // Connect a Rya Streams Client and set it in the shared state.
    final RyaStreamsClient newClient = KafkaRyaStreamsClientFactory.make(ryaInstance, kafkaHostname, kafkaPort);
    state.connectedToRyaStreams(newClient);
    // Return a message that indicates the operation was successful.
    if (oldClient.isPresent()) {
        return "The Rya Streams subsystem that this Rya instance uses has been changed. Any queries that were " + "maintained by the previous subsystem will need to be migrated to the new one.";
    } else {
        return "The Rya Instance has been updated to use the provided Rya Streams subsystem. " + "Rya Streams commands are now avaiable while connected to this instance.";
    }
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) RyaStreamsDetails(org.apache.rya.api.instance.RyaDetails.RyaStreamsDetails) RyaClientException(org.apache.rya.api.client.RyaClientException) RyaClient(org.apache.rya.api.client.RyaClient) RyaClientException(org.apache.rya.api.client.RyaClientException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) IOException(java.io.IOException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 2 with RyaStreamsClient

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

the class SharedShellStateTest method connectedToRyaStreams.

@Test
public void connectedToRyaStreams() {
    // Create a shell state.
    final SharedShellState state = new SharedShellState();
    // Connect to Accumulo.
    final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class);
    final RyaClient connectedCommands = mock(RyaClient.class);
    state.connectedToAccumulo(connectionDetails, connectedCommands);
    // Connect to an Instance.
    state.connectedToInstance("instance");
    // Connect to Rya Streams for the instance.
    final RyaStreamsClient streamsClient = mock(RyaStreamsClient.class);
    state.connectedToRyaStreams(streamsClient);
    // Verify the state.
    assertEquals(streamsClient, state.getShellState().getRyaStreamsCommands().get());
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 3 with RyaStreamsClient

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

the class RyaStreamsCommandsTest method startQuery_alreadyRunning.

@Test
public void startQuery_alreadyRunning() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final StartQuery startQuery = mock(StartQuery.class);
    when(mockClient.getStartQuery()).thenReturn(startQuery);
    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 running.
    final UUID queryId = UUID.randomUUID();
    when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", true, false)));
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.startQuery(queryId.toString());
    // Verify the interactor was not invoked.
    verify(startQuery, never()).start(queryId);
    // Verify a message is printed to the user.
    final String expected = "That query is already running.";
    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) 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) StartQuery(org.apache.rya.streams.api.interactor.StartQuery) Test(org.junit.Test)

Example 4 with RyaStreamsClient

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

the class RyaStreamsCommandsTest method addQuery_insertConstructQuery.

@Test
public void addQuery_insertConstructQuery() 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 = "PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> " + "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "CONSTRUCT { " + "?X vCard:FN ?name . " + "?X vCard:URL ?url . " + "?X vCard:TITLE ?title . " + "} " + "FROM <http://www.w3.org/People/Berners-Lee/card> " + "WHERE { " + "OPTIONAL { ?X foaf:name ?name . FILTER isLiteral(?name) . } " + "OPTIONAL { ?X foaf:homepage ?url . FILTER isURI(?url) . } " + "OPTIONAL { ?X foaf:title ?title . FILTER isLiteral(?title) . } " + "}";
    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));
    final String message = commands.addQuery(true, true);
    // Verify the interactor was invoked with the provided input.
    verify(addQuery).addQuery(sparql, false, true);
    // 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 5 with RyaStreamsClient

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

the class RyaStreamsCommandsTest method stopQuery.

@Test
public void stopQuery() 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 running.
    final UUID queryId = UUID.randomUUID();
    when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", true, 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 invoked with the provided parameters.
    verify(stopQuery).stop(queryId);
    // Verify a message is printed to the user.
    final String expected = "The query will no longer be processed by the Rya Streams subsystem.";
    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)

Aggregations

RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)22 RyaClient (org.apache.rya.api.client.RyaClient)15 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)14 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)13 Test (org.junit.Test)13 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)12 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)12 UUID (java.util.UUID)10 CliCommand (org.springframework.shell.core.annotation.CliCommand)8 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)6 AddQuery (org.apache.rya.streams.api.interactor.AddQuery)5 GetQuery (org.apache.rya.streams.api.interactor.GetQuery)5 IOException (java.io.IOException)4 RyaClientException (org.apache.rya.api.client.RyaClientException)4 MalformedQueryException (org.openrdf.query.MalformedQueryException)3 RyaStreamsDetails (org.apache.rya.api.instance.RyaDetails.RyaStreamsDetails)2 StartQuery (org.apache.rya.streams.api.interactor.StartQuery)2 StopQuery (org.apache.rya.streams.api.interactor.StopQuery)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1