use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.
the class KafkaRunQueryIT method runQuery.
@Test
public void runQuery() throws Exception {
// Setup some constant that will be used through the test.
final String ryaInstance = UUID.randomUUID().toString();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
// This query is completely in memory, so it doesn't need to be closed.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
// Add the query to the query repository.
final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
final UUID queryId = sQuery.getQueryId();
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// The thread that will run the tested interactor.
final Thread testThread = new Thread() {
@Override
public void run() {
final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
try {
runQuery.run(queryId);
} catch (final RyaStreamsException e) {
// Do nothing. Test will still fail because the expected results will be missing.
}
}
};
// Create the topics.
kafka.createTopic(statementsTopic);
kafka.createTopic(resultsTopic);
// Create the statements that will be loaded.
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
// Create the expected results.
final List<VisibilityBindingSet> expected = new ArrayList<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
expected.add(new VisibilityBindingSet(bs, "a"));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Bob"));
bs.addBinding("business", vf.createURI("urn:TacoShop"));
expected.add(new VisibilityBindingSet(bs, "a"));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Charlie"));
bs.addBinding("business", vf.createURI("urn:TacoShop"));
expected.add(new VisibilityBindingSet(bs, "a"));
// Execute the test. This will result in a set of results that were read from the results topic.
final List<VisibilityBindingSet> results;
try {
// Wait for the program to start.
testThread.start();
Thread.sleep(2000);
// Write some statements to the program.
final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
loadStatements.fromCollection(statements);
// Read the output of the streams program.
consumer.subscribe(Lists.newArrayList(resultsTopic));
results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
} finally {
// Tear down the test.
testThread.interrupt();
testThread.join(3000);
}
// Show the read results matched the expected ones.
assertEquals(expected, results);
}
use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.
the class ListQueriesCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final KafkaParameters params = new KafkaParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not list the queries 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 ListQueries 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 list queries command.
try {
final ListQueries listQueries = new DefaultListQueries(queryRepo);
try {
final Set<StreamsQuery> queries = listQueries.all();
System.out.println(formatQueries(queries));
} catch (final RyaStreamsException e) {
System.err.println("Unable to retrieve the queries.");
e.printStackTrace();
System.exit(1);
}
} catch (final Exception e) {
System.err.println("Problem encountered while closing the QueryRepository.");
e.printStackTrace();
System.exit(1);
}
}
use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.
the class DeleteQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final RemoveParameters params = new RemoveParameters();
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 DeleteQuery 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 delete query command.
try {
final DeleteQuery deleteQuery = new DefaultDeleteQuery(queryRepo);
try {
deleteQuery.delete(UUID.fromString(params.queryId));
System.out.println("Deleted query: " + params.queryId);
} catch (final RyaStreamsException e) {
System.err.println("Unable to delete query with ID: " + params.queryId);
e.printStackTrace();
System.exit(1);
}
} catch (final Exception e) {
System.err.println("Problem encountered while closing the QueryRepository.");
e.printStackTrace();
System.exit(1);
}
}
use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.
the class RunQueryCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final RunParameters params = new RunParameters();
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 RunQuery command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Look up the query to be executed from the change log.
try {
try {
final UUID queryId = UUID.fromString(params.queryId);
final Optional<StreamsQuery> query = queryRepo.get(queryId);
if (!query.isPresent()) {
throw new ArgumentsException("There is no registered query for queryId " + params.queryId);
}
// Make sure the topics required by the application exists for the specified Rya instances.
final Set<String> topics = new HashSet<>();
topics.add(KafkaTopics.statementsTopic(params.ryaInstance));
topics.add(KafkaTopics.queryResultsTopic(params.ryaInstance, queryId));
KafkaTopics.createTopics(params.zookeeperServers, topics, 1, 1);
// Run the query that uses those topics.
final KafkaRunQuery runQuery = new KafkaRunQuery(params.kafkaIP, params.kafkaPort, KafkaTopics.statementsTopic(params.ryaInstance), KafkaTopics.queryResultsTopic(params.ryaInstance, queryId), queryRepo, new TopologyFactory());
runQuery.run(queryId);
} catch (final Exception e) {
throw new ExecutionException("Could not execute the Run Query command.", e);
}
} catch (final ExecutionException e) {
// Rethrow the exceptions that are advertised by execute.
throw e;
} catch (final Exception e) {
throw new ExecutionException("Problem encountered while closing the QueryRepository.", e);
}
}
use of org.apache.rya.streams.api.queries.QueryRepository in project incubator-rya by apache.
the class KafkaRyaStreamsClientFactory method make.
/**
* Initialize a {@link RyaStreamsClient} that will interact with an instance of Rya Streams
* that is backed by Kafka.
*
* @param ryaInstance - The name of the Rya Instance the client is connected to. (not null)
* @param kafkaHostname - The hostname of the Kafka Broker.
* @param kafkaPort - The port of the Kafka Broker.
* @return The initialized commands.
*/
public static RyaStreamsClient make(final String ryaInstance, final String kafkaHostname, final int kafkaPort) {
requireNonNull(ryaInstance);
requireNonNull(kafkaHostname);
// Setup Query Repository used by the Kafka Rya Streams subsystem.
final Producer<?, QueryChange> queryProducer = makeProducer(kafkaHostname, kafkaPort, StringSerializer.class, QueryChangeSerializer.class);
final Consumer<?, QueryChange> queryConsumer = fromStartConsumer(kafkaHostname, kafkaPort, StringDeserializer.class, QueryChangeDeserializer.class);
final String changeLogTopic = KafkaTopics.queryChangeLogTopic(ryaInstance);
final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
final QueryRepository queryRepo = new InMemoryQueryRepository(changeLog, SCHEDULER);
// Create the Rya Streams client that is backed by a Kafka Query Change Log.
return new RyaStreamsClient(new DefaultAddQuery(queryRepo), new DefaultGetQuery(queryRepo), new DefaultDeleteQuery(queryRepo), new KafkaGetQueryResultStream<>(kafkaHostname, "" + kafkaPort, VisibilityStatementDeserializer.class), new KafkaGetQueryResultStream<>(kafkaHostname, "" + kafkaPort, VisibilityBindingSetDeserializer.class), new DefaultListQueries(queryRepo), new DefaultStartQuery(queryRepo), new DefaultStopQuery(queryRepo)) {
/**
* Close the QueryRepository used by the returned client.
*/
@Override
public void close() {
try {
queryRepo.stopAndWait();
} catch (final Exception e) {
log.warn("Couldn't close a QueryRepository.", e);
}
}
};
}
Aggregations