use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class AddQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final AddParameters params = new AddParameters();
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 AddQuery 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 add query command.
try {
final AddQuery addQuery = new DefaultAddQuery(queryRepo);
try {
final Boolean isActive = Boolean.parseBoolean(params.isActive);
final Boolean isInsert = Boolean.parseBoolean(params.isInsert);
// If the query's results are meant to be written back to Rya, make sure it creates statements.
if (isInsert) {
final boolean isConstructQuery = QueryInvestigator.isConstruct(params.query);
final boolean isInsertQuery = QueryInvestigator.isInsertWhere(params.query);
if (isConstructQuery) {
System.out.println("WARNING: CONSTRUCT is part of the SPARQL Query API, so they do not normally\n" + "get written back to the triple store. Consider using an INSERT, which is\n" + "part of the SPARQL Update API, in the future.");
}
if (!(isConstructQuery || isInsertQuery)) {
throw new ArgumentsException("Only CONSTRUCT queries and INSERT updates may be inserted back to the triple store.");
}
}
final StreamsQuery query = addQuery.addQuery(params.query, isActive, isInsert);
System.out.println("Added query: " + query.getSparql());
} catch (final RyaStreamsException e) {
throw new ExecutionException("Unable to add the query to Rya Streams.", e);
}
} catch (final MalformedQueryException e) {
throw new ArgumentsException("Could not parse the provided query.", e);
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method updateListenerNotify.
@Test
public void updateListenerNotify() throws Exception {
// Setup a totally in memory QueryRepository.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), SCHEDULE);
try {
queries.startAndWait();
// Add a query to it.
final StreamsQuery query = queries.add("query 1", true, false);
final Set<StreamsQuery> existing = queries.subscribe((queryChangeEvent, newQueryState) -> {
final ChangeLogEntry<QueryChange> expected = new ChangeLogEntry<>(1L, QueryChange.create(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
final Optional<StreamsQuery> expectedQueryState = Optional.of(new StreamsQuery(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
assertEquals(expected, queryChangeEvent);
assertEquals(expectedQueryState, newQueryState);
});
assertEquals(Sets.newHashSet(query), existing);
queries.add("query 2", true, false);
} finally {
queries.stop();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method deletedQueriesDisappear.
@Test
public void deletedQueriesDisappear() throws Exception {
// Setup a totally in memory QueryRepository.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), SCHEDULE);
// Add some queries to it. The second one we will delete.
final Set<StreamsQuery> expected = new HashSet<>();
expected.add(queries.add("query 1", true, true));
final UUID deletedMeId = queries.add("query 2", false, true).getQueryId();
expected.add(queries.add("query 3", true, false));
// Delete the second query.
queries.delete(deletedMeId);
// Show only queries 1 and 3 are in the list.
final Set<StreamsQuery> stored = queries.list();
assertEquals(expected, stored);
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method get_present.
@Test
public void get_present() throws Exception {
// Setup a totally in memory QueryRepository.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), SCHEDULE);
// Add a query to it.
final StreamsQuery query = queries.add("query 1", true, false);
// Show the fetched query matches the expected ones.
final Optional<StreamsQuery> fetched = queries.get(query.getQueryId());
assertEquals(query, fetched.get());
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method initializedWithPopulatedChangeLog.
@Test
public void initializedWithPopulatedChangeLog() throws Exception {
// Setup a totally in memory QueryRepository. Hold onto the change log so that we can use it again later.
final QueryChangeLog changeLog = new InMemoryQueryChangeLog();
final QueryRepository queries = new InMemoryQueryRepository(changeLog, SCHEDULE);
try {
queries.startAndWait();
// Add some queries and deletes to it.
final Set<StreamsQuery> expected = new HashSet<>();
expected.add(queries.add("query 1", true, true));
final UUID deletedMeId = queries.add("query 2", false, true).getQueryId();
expected.add(queries.add("query 3", true, false));
queries.delete(deletedMeId);
// Create a new totally in memory QueryRepository.
final QueryRepository initializedQueries = new InMemoryQueryRepository(changeLog, SCHEDULE);
try {
// Listing the queries should work using an initialized change log.
final Set<StreamsQuery> stored = initializedQueries.list();
assertEquals(expected, stored);
} finally {
queries.stop();
}
} finally {
queries.stop();
}
}
Aggregations