use of org.apache.rya.api.function.projection.RandomUUIDFactory in project incubator-rya by apache.
the class KafkaRunQuery method run.
@Override
public void run(final UUID queryId) throws RyaStreamsException {
requireNonNull(queryId);
// Fetch the query from the repository. Throw an exception if it isn't present.
final Optional<StreamsQuery> query = queryRepo.get(queryId);
if (!query.isPresent()) {
throw new RyaStreamsException("Could not run the Query with ID " + queryId + " because no such query " + "is currently registered.");
}
// Build a processing topology using the SPARQL, provided statements topic, and provided results topic.
final String sparql = query.get().getSparql();
final TopologyBuilder topologyBuilder;
try {
topologyBuilder = topologyFactory.build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
} catch (final Exception e) {
throw new RyaStreamsException("Could not run the Query with ID " + queryId + " because a processing " + "topolgoy could not be built for the SPARQL " + sparql, e);
}
// Setup the Kafka Stream program.
final Properties streamsProps = new Properties();
streamsProps.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaHostname + ":" + kafkaPort);
// Use the Query ID as the Application ID to ensure we resume where we left off the last time this command was run.
streamsProps.put(StreamsConfig.APPLICATION_ID_CONFIG, "KafkaRunQuery-" + queryId);
// Always start at the beginning of the input topic.
streamsProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final KafkaStreams streams = new KafkaStreams(topologyBuilder, new StreamsConfig(streamsProps));
// If an unhandled exception is thrown, rethrow it.
streams.setUncaughtExceptionHandler((t, e) -> {
// Log the problem and kill the program.
log.error("Unhandled exception while processing the Rya Streams query. Shutting down.", e);
System.exit(1);
});
// Setup a shutdown hook that kills the streams program at shutdown.
final CountDownLatch awaitTermination = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
awaitTermination.countDown();
}
});
// Run the streams program and wait for termination.
streams.start();
try {
awaitTermination.await();
} catch (final InterruptedException e) {
log.warn("Interrupted while waiting for termination. Shutting down.");
}
streams.close();
}
use of org.apache.rya.api.function.projection.RandomUUIDFactory in project incubator-rya by apache.
the class TopologyFactoryTest method projectionJoinStatementPattern.
@Test
public void projectionJoinStatementPattern() throws Exception {
final String query = "SELECT * WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "?otherPerson <urn:talksTo> ?dog . " + "}";
FACTORY.build(query, "source", "sink", new RandomUUIDFactory());
final List<ProcessorEntry> entries = FACTORY.getProcessorEntry();
assertTrue(entries.get(0).getNode() instanceof Projection);
assertTrue(entries.get(1).getNode() instanceof Join);
StatementPattern expected = new StatementPattern(new Var("person"), TALKS_TO, new Var("otherPerson"));
assertEquals(expected, entries.get(2).getNode());
expected = new StatementPattern(new Var("otherPerson"), TALKS_TO, new Var("dog"));
assertEquals(expected, entries.get(3).getNode());
}
use of org.apache.rya.api.function.projection.RandomUUIDFactory in project incubator-rya by apache.
the class AggregationProcessorIT method multipleAggregations.
@Test
public void multipleAggregations() throws Exception {
// A query that figures out what the youngest and oldest ages are across all people.
final String sparql = "SELECT (min(?age) as ?youngest) (max(?age) as ?oldest) " + "WHERE { " + "?person <urn:age> ?age " + "}";
// Create the statements that will be input into the query..
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:age"), vf.createLiteral(13)), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:age"), vf.createLiteral(14)), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:age"), vf.createLiteral(7)), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:David"), vf.createURI("urn:age"), vf.createLiteral(5)), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Eve"), vf.createURI("urn:age"), vf.createLiteral(25)), ""));
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("youngest", vf.createLiteral(13));
bs.addBinding("oldest", vf.createLiteral(13));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("youngest", vf.createLiteral(13));
bs.addBinding("oldest", vf.createLiteral(14));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("youngest", vf.createLiteral(7));
bs.addBinding("oldest", vf.createLiteral(14));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("youngest", vf.createLiteral(5));
bs.addBinding("oldest", vf.createLiteral(14));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("youngest", vf.createLiteral(5));
bs.addBinding("oldest", vf.createLiteral(25));
expected.add(new VisibilityBindingSet(bs, ""));
// Enumerate some topics that will be re-used
final String ryaInstance = UUID.randomUUID().toString();
final UUID queryId = UUID.randomUUID();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Run the test.
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
use of org.apache.rya.api.function.projection.RandomUUIDFactory in project incubator-rya by apache.
the class AggregationProcessorIT method count.
@Test
public void count() throws Exception {
// A query that figures out how many books each person has.
final String sparql = "SELECT ?person (count(?book) as ?bookCount) " + "WHERE { " + "?person <urn:hasBook> ?book " + "} GROUP BY ?person";
// Create the statements that will be input into the query..
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:hasBook"), vf.createLiteral("Book 1")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:hasBook"), vf.createLiteral("Book 1")), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:hasBook"), vf.createLiteral("Book 2")), "b"));
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("bookCount", vf.createLiteral("1", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, "a"));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Bob"));
bs.addBinding("bookCount", vf.createLiteral("1", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("bookCount", vf.createLiteral("2", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, "a&b"));
// Enumerate some topics that will be re-used
final String ryaInstance = UUID.randomUUID().toString();
final UUID queryId = UUID.randomUUID();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Run the test.
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
use of org.apache.rya.api.function.projection.RandomUUIDFactory in project incubator-rya by apache.
the class AggregationProcessorIT method sum.
@Test
public void sum() throws Exception {
// A query that figures out how much food each person has.
final String sparql = "SELECT ?person (sum(?foodCount) as ?totalFood) " + "WHERE { " + "?person <urn:hasFoodType> ?food . " + "?food <urn:count> ?foodCount . " + "} GROUP BY ?person";
// Create the statements that will be input into the query..
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:hasFoodType"), vf.createURI("urn:corn")), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:hasFoodType"), vf.createURI("urn:apple")), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:corn"), vf.createURI("urn:count"), vf.createLiteral(4)), ""));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:apple"), vf.createURI("urn:count"), vf.createLiteral(3)), ""));
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("totalFood", vf.createLiteral("4", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, ""));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("totalFood", vf.createLiteral("7", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, ""));
// Enumerate some topics that will be re-used
final String ryaInstance = UUID.randomUUID().toString();
final UUID queryId = UUID.randomUUID();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// Setup a topology.
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Run the test.
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
Aggregations