use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class JoinProcessorIT method manyJoins.
@Test
public void manyJoins() throws Exception {
// 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 String query = "SELECT * WHERE { " + "?person <urn:talksTo> ?employee ." + "?employee <urn:worksAt> ?business ." + "?employee <urn:hourlyWage> ?wage ." + " }";
final TopologyFactory factory = new TopologyFactory();
final TopologyBuilder builder = factory.build(query, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Create some statements that generate a bunch of right SP results.
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:hourlyWage"), vf.createLiteral(7.25)), "a"));
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
final MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("employee", vf.createURI("urn:Bob"));
bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
bs.addBinding("wage", vf.createLiteral(7.25));
expected.add(new VisibilityBindingSet(bs, "a"));
// Run the test.
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class ProjectionProcessorIT method showProcessorWorks.
@Test
public void showProcessorWorks() throws Exception {
// 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);
// Create a topology for the Query that will be tested.
final String sparql = "SELECT (?person AS ?p) ?otherPerson " + "WHERE { " + "?person <urn:talksTo> ?otherPerson . " + "}";
final TopologyBuilder builder = new TopologyFactory().build(sparql, statementsTopic, resultsTopic, new RandomUUIDFactory());
// Load some data into the input topic.
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob")), "a"));
// Show the correct binding set results from the job.
final Set<VisibilityBindingSet> expected = new HashSet<>();
final MapBindingSet expectedBs = new MapBindingSet();
expectedBs.addBinding("p", vf.createURI("urn:Alice"));
expectedBs.addBinding("otherPerson", vf.createURI("urn:Bob"));
expected.add(new VisibilityBindingSet(expectedBs, "a"));
RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, Sets.newHashSet(expected), VisibilityBindingSetDeserializer.class);
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class KafkaLoadStatements method fromFile.
@Override
public void fromFile(final Path statementsPath, final String visibilities) throws RyaStreamsException {
requireNonNull(statementsPath);
requireNonNull(visibilities);
if (!statementsPath.toFile().exists()) {
throw new RyaStreamsException("Could not load statements at path '" + statementsPath + "' because that " + "does not exist. Make sure you've entered the correct path.");
}
// Create an RDF Parser whose format is derived from the statementPath's file extension.
final RDFFormat format = RDFFormat.forFileName(statementsPath.getFileName().toString());
final RDFParser parser = Rio.createParser(format);
// Set a handler that writes the statements to the specified kafka topic.
parser.setRDFHandler(new RDFHandlerBase() {
@Override
public void startRDF() throws RDFHandlerException {
log.trace("Starting loading statements.");
}
@Override
public void handleStatement(final Statement stmnt) throws RDFHandlerException {
final VisibilityStatement visiStatement = new VisibilityStatement(stmnt, visibilities);
producer.send(new ProducerRecord<>(topic, visiStatement));
}
@Override
public void endRDF() throws RDFHandlerException {
producer.flush();
log.trace("Done.");
}
});
// Do the parse and load.
try {
parser.parse(Files.newInputStream(statementsPath), "");
} catch (RDFParseException | RDFHandlerException | IOException e) {
throw new RyaStreamsException("Could not load the RDF file's Statements into Rya Streams.", e);
}
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class LocalQueryExecutorIT method runQuery.
@Test
public void runQuery() throws Exception {
// Test values.
final String ryaInstance = "rya";
final StreamsQuery sQuery = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
// 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"));
// Start the executor that will be tested.
final CreateKafkaTopic createKafkaTopic = new CreateKafkaTopic(kafka.getZookeeperServers());
final String kafkaServers = kafka.getKafkaHostname() + ":" + kafka.getKafkaPort();
final KafkaStreamsFactory jobFactory = new SingleThreadKafkaStreamsFactory(kafkaServers);
final QueryExecutor executor = new LocalQueryExecutor(createKafkaTopic, jobFactory);
executor.startAndWait();
try {
// Start the query.
executor.startQuery(ryaInstance, sQuery);
// Wait for the program to start.
Thread.sleep(5000);
// Write some statements to the program.
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, stmtProducer);
loadStatements.fromCollection(statements);
// Read the output of the streams program.
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, sQuery.getQueryId());
resultConsumer.subscribe(Lists.newArrayList(resultsTopic));
final List<VisibilityBindingSet> results = KafkaTestUtil.pollForResults(500, 6, 3, resultConsumer);
assertEquals(expected, results);
} finally {
executor.stopAndWait();
}
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class RyaStreamsTestUtil method runStreamProcessingTest.
/**
* Runs a Kafka Streams topology, loads statements into the input topic, read the binding sets that come out of
* the results topic, and ensures the expected results match the read results.
*
* @param <T> The type of value that will be consumed from the results topic.
* @param kafka - The embedded Kafka instance that is being tested with. (not null)
* @param statementsTopic - The topic statements will be written to. (not null)
* @param resultsTopic - The topic results will be read from. (not null)
* @param builder - The streams topology that will be executed. (not null)
* @param statements - The statements that will be loaded into the topic. (not null)
* @param expected - The expected results. (not null)
* @param expectedDeserializerClass - The class of the deserializer that will be used when reading
* values from the results topic. (not null)
* @throws Exception If any exception was thrown while running the test.
*/
public static <T> void runStreamProcessingTest(final KafkaTestInstanceRule kafka, final String statementsTopic, final String resultsTopic, final TopologyBuilder builder, final List<VisibilityStatement> statements, final Set<T> expected, final Class<? extends Deserializer<T>> expectedDeserializerClass) throws Exception {
requireNonNull(kafka);
requireNonNull(statementsTopic);
requireNonNull(resultsTopic);
requireNonNull(builder);
requireNonNull(statements);
requireNonNull(expected);
requireNonNull(expectedDeserializerClass);
// Explicitly create the topics that are being used.
kafka.createTopic(statementsTopic);
kafka.createTopic(resultsTopic);
// Start the streams program.
final Properties props = kafka.createBootstrapServerConfig();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, UUID.randomUUID().toString());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final KafkaStreams streams = new KafkaStreams(builder, new StreamsConfig(props));
streams.cleanUp();
try {
streams.start();
// Wait for the streams application to start. Streams only see data after their consumers are connected.
Thread.sleep(6000);
// Load the statements into the input topic.
try (Producer<String, VisibilityStatement> producer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityStatementSerializer.class)) {
new KafkaLoadStatements(statementsTopic, producer).fromCollection(statements);
}
// Wait for the final results to appear in the output topic and verify the expected Binding Sets were found.
try (Consumer<String, T> consumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, expectedDeserializerClass)) {
// Register the topic.
consumer.subscribe(Arrays.asList(resultsTopic));
// Poll for the result.
final Set<T> results = Sets.newHashSet(KafkaTestUtil.pollForResults(500, 6, expected.size(), consumer));
// Show the correct binding sets results from the job.
assertEquals(expected, results);
}
} finally {
streams.close();
}
}
Aggregations