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);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations