use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class KafkaLoadStatements method fromCollection.
@Override
public void fromCollection(final Collection<VisibilityStatement> statements) throws RyaStreamsException {
requireNonNull(statements);
for (final VisibilityStatement statement : statements) {
producer.send(new ProducerRecord<>(topic, statement));
}
producer.flush();
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class QueryResultsOutputUtil method toNtriplesFile.
/**
* Writes the results of a {@link QueryResultStream} to the output stream as NTriples until the
* shutdown signal is set.
*
* @param out - The stream the NTriples data will be written to. (not null)
* @param resultsStream - The results stream that will be polled for results to
* write to {@code out}. (not null)
* @param shutdownSignal - Setting this signal will cause the thread that
* is processing this function to finish and leave. (not null)
* @throws RDFHandlerException A problem was encountered while
* writing the NTriples to the output stream.
* @throws IllegalStateException The {@code resultsStream} is closed.
* @throws RyaStreamsException Could not fetch the next set of results.
*/
public static void toNtriplesFile(final OutputStream out, final QueryResultStream<VisibilityStatement> resultsStream, final AtomicBoolean shutdownSignal) throws RDFHandlerException, IllegalStateException, RyaStreamsException {
requireNonNull(out);
requireNonNull(resultsStream);
requireNonNull(shutdownSignal);
final RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, out);
writer.startRDF();
while (!shutdownSignal.get()) {
final Iterable<VisibilityStatement> it = resultsStream.poll(1000);
for (final VisibilityStatement result : it) {
writer.handleStatement(result);
}
}
writer.endRDF();
}
use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.
the class LoadStatementsCommandIT method longParams.
@Test
public void longParams() throws Exception {
// Load a file of statements into Kafka.
final String visibilities = "a|b|c";
final String[] args = new String[] { "--ryaInstance", "" + ryaInstance, "--kafkaHostname", kafka.getKafkaHostname(), "--kafkaPort", kafka.getKafkaPort(), "--statementsFile", TURTLE_FILE.toString(), "--visibilities", visibilities };
// Load the file of statements into the Statements topic.
new LoadStatementsCommand().execute(args);
// Show that the statements were loaded into the topic.
final List<VisibilityStatement> read = new ArrayList<>();
try (final Consumer<String, VisibilityStatement> consumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityStatementDeserializer.class)) {
// Subscribe for messages.
consumer.subscribe(Arrays.asList(KafkaTopics.statementsTopic(ryaInstance)));
// Read the messages and extract their values.
final Iterator<ConsumerRecord<String, VisibilityStatement>> iter = consumer.poll(3000).iterator();
while (iter.hasNext()) {
read.add(iter.next().value());
}
}
final ValueFactory VF = ValueFactoryImpl.getInstance();
final List<VisibilityStatement> expected = new ArrayList<>();
expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#alice"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#bob")), visibilities));
expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#bob"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#charlie")), visibilities));
expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#charlie"), VF.createURI("http://example#likes"), VF.createURI("http://example#icecream")), visibilities));
// Show the written statements matches the read ones.
assertEquals(expected, read);
}
use of org.apache.rya.api.model.VisibilityStatement 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.model.VisibilityStatement 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);
}
Aggregations