Search in sources :

Example 16 with RyaStreamsClient

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

the class RyaStreamsCommandsTest method listQueries.

@Test
public void listQueries() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final ListQueries listQueries = mock(ListQueries.class);
    when(mockClient.getListQueries()).thenReturn(listQueries);
    final Set<StreamsQuery> queries = Sets.newHashSet(new StreamsQuery(UUID.fromString("33333333-3333-3333-3333-333333333333"), "SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false), new StreamsQuery(UUID.fromString("11111111-1111-1111-1111-111111111111"), "SELECT * WHERE { ?a ?b ?c . }", true, false), new StreamsQuery(UUID.fromString("22222222-2222-2222-2222-222222222222"), "SELECT * WHERE { ?d ?e ?f . }", false, false));
    when(listQueries.all()).thenReturn(queries);
    // 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.listQueries();
    // Verify the correct report is returned.
    final String expected = "-----------------------------------------------\n" + " Query ID: 11111111-1111-1111-1111-111111111111\n" + "Is Active: true\n" + "Is Insert: false\n" + "   SPARQL: select ?a ?b ?c\n" + "           where {\n" + "             ?a ?b ?c.\n" + "           }\n" + "-----------------------------------------------\n" + " Query ID: 22222222-2222-2222-2222-222222222222\n" + "Is Active: false\n" + "Is Insert: false\n" + "   SPARQL: select ?d ?e ?f\n" + "           where {\n" + "             ?d ?e ?f.\n" + "           }\n" + "-----------------------------------------------\n" + " Query ID: 33333333-3333-3333-3333-333333333333\n" + "Is Active: true\n" + "Is Insert: false\n" + "   SPARQL: select ?person ?business\n" + "           where {\n" + "             ?person <urn:worksAt> ?business.\n" + "           }\n" + "-----------------------------------------------\n";
    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) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) ListQueries(org.apache.rya.streams.api.interactor.ListQueries) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 17 with RyaStreamsClient

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

the class RyaStreamsCommands method stopQuery.

@CliCommand(value = STREAM_QUERIES_STOP_CMD, help = "Stop processing a SPARQL query using the Rya Streams subsystem.")
public String stopQuery(@CliOption(key = { "queryId" }, mandatory = true, help = "The ID of the query to stop processing.") final String queryId) {
    final RyaStreamsClient streamsClient = state.getShellState().getRyaStreamsCommands().get();
    try {
        // Ensure the query exists.
        final UUID id = UUID.fromString(queryId);
        final java.util.Optional<StreamsQuery> streamsQuery = streamsClient.getGetQuery().getQuery(id);
        if (!streamsQuery.isPresent()) {
            throw new RuntimeException("No Rya Streams query exists for ID " + queryId);
        }
        // Ensure it isn't already stopped.
        if (!streamsQuery.get().isActive()) {
            return "That query is already stopped.";
        }
        // Stop it.
        streamsClient.getStopQuery().stop(id);
        return "The query will no longer be processed by the Rya Streams subsystem.";
    } catch (final RyaStreamsException e) {
        throw new RuntimeException("Unable to start the Query.", e);
    }
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) UUID(java.util.UUID) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 18 with RyaStreamsClient

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

the class RyaStreamsCommands method printQueryDetails.

@CliCommand(value = STREAM_QUERIES_DETAILS_CMD, help = "Print detailed information about a specific query managed by the Rya Streams subsystem.")
public String printQueryDetails(@CliOption(key = { "queryId" }, mandatory = true, help = "The ID of the query whose details will be printed.") final String queryId) {
    final RyaStreamsClient streamsClient = state.getShellState().getRyaStreamsCommands().get();
    final UUID id = UUID.fromString(queryId);
    try {
        final java.util.Optional<StreamsQuery> query = streamsClient.getGetQuery().getQuery(id);
        if (!query.isPresent()) {
            return "There is no query with the specified ID.";
        }
        return StreamsQueryFormatter.format(query.get());
    } catch (final RyaStreamsException e) {
        throw new RuntimeException("Unable to fetch the query from the Rya Streams subsystem.", e);
    } catch (final Exception e) {
        throw new RuntimeException("Unable to print the query to the console.", e);
    }
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) UUID(java.util.UUID) 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 19 with RyaStreamsClient

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

the class RyaStreamsCommands method startQuery.

@CliCommand(value = STREAM_QUERIES_START_CMD, help = "Start processing a SPARQL query using the Rya Streams subsystem.")
public String startQuery(@CliOption(key = { "queryId" }, mandatory = true, help = "The ID of the query to start processing.") final String queryId) {
    final RyaStreamsClient streamsClient = state.getShellState().getRyaStreamsCommands().get();
    try {
        // Ensure the query exists.
        final UUID id = UUID.fromString(queryId);
        final java.util.Optional<StreamsQuery> streamsQuery = streamsClient.getGetQuery().getQuery(id);
        if (!streamsQuery.isPresent()) {
            throw new RuntimeException("No Rya Streams query exists for ID " + queryId);
        }
        // Ensure it isn't already started.
        if (streamsQuery.get().isActive()) {
            return "That query is already running.";
        }
        // Start it.
        streamsClient.getStartQuery().start(id);
        return "The query will be processed by the Rya Streams subsystem.";
    } catch (final RyaStreamsException e) {
        throw new RuntimeException("Unable to start the Query.", e);
    }
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) UUID(java.util.UUID) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 20 with RyaStreamsClient

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

the class RyaStreamsCommands method addQuery.

@CliCommand(value = STREAM_QUERIES_ADD_CMD, help = "Add a SPARQL query to the Rya Streams subsystem.")
public String addQuery(@CliOption(key = { "inactive" }, mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Setting this flag will add the query, but not run it. (default: false)") final boolean inactive, @CliOption(key = { "insert" }, mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Setting this flag will insert the query's results back into Rya. (default: false)") final boolean isInsert) {
    final RyaStreamsClient streamsClient = state.getShellState().getRyaStreamsCommands().get();
    // Prompt the user for the SPARQL that defines the query.
    try {
        final Optional<String> sparql = sparqlPrompt.getSparql();
        // If the user aborted the prompt, return.
        if (!sparql.isPresent()) {
            return "";
        }
        final boolean isConstructQuery = QueryInvestigator.isConstruct(sparql.get());
        final boolean isInsertQuery = QueryInvestigator.isInsertWhere(sparql.get());
        // If the user wants to insert a CONSTRUCT into Rya, print a warning.
        if (isInsert && isConstructQuery) {
            consolePrinter.println("WARNING: CONSTRUCT is part of the SPARQL Query API, so they do not normally");
            consolePrinter.println("get written back to the triple store. Consider using an INSERT, which is");
            consolePrinter.println("part of the SPARQL Update API, in the future.");
        }
        // If the user wants to use an INSERT query, but not insert it back into Rya, suggest using a construct.
        if (!isInsert && isInsertQuery) {
            consolePrinter.println("WARNING: INSERT is part of the SPARQL Update API, so they normally get written");
            consolePrinter.println("back to the triple store. Consider using a CONSTRUCT, which is part of the");
            consolePrinter.println("SPARQL Query API, in the future.");
        }
        // If the user wants to insert the query back into Rya, make sure it is a legal query to do that.
        if (isInsert && !(isConstructQuery || isInsertQuery)) {
            throw new RuntimeException("Only CONSTRUCT queries and INSERT updates may be inserted back to the triple store.");
        }
        final StreamsQuery streamsQuery = streamsClient.getAddQuery().addQuery(sparql.get(), !inactive, isInsert);
        return "The added query's ID is " + streamsQuery.getQueryId();
    } catch (final MalformedQueryException | IOException | RyaStreamsException e) {
        throw new RuntimeException("Unable to add the SPARQL query to the Rya Streams subsystem.", e);
    }
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) MalformedQueryException(org.openrdf.query.MalformedQueryException) IOException(java.io.IOException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

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