Search in sources :

Example 26 with VisibilityStatement

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();
}
Also used : VisibilityStatement(org.apache.rya.api.model.VisibilityStatement)

Example 27 with VisibilityStatement

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();
}
Also used : RDFWriter(org.openrdf.rio.RDFWriter) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement)

Example 28 with VisibilityStatement

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);
}
Also used : ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Example 29 with VisibilityStatement

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with VisibilityStatement

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)43 Test (org.junit.Test)36 ValueFactory (org.openrdf.model.ValueFactory)35 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)32 ArrayList (java.util.ArrayList)29 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)28 UUID (java.util.UUID)26 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)25 HashSet (java.util.HashSet)24 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)24 MapBindingSet (org.openrdf.query.impl.MapBindingSet)24 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)23 LoadStatements (org.apache.rya.streams.api.interactor.LoadStatements)4 KafkaLoadStatements (org.apache.rya.streams.kafka.interactor.KafkaLoadStatements)4 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)4 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)3 Statement (org.openrdf.model.Statement)3 Properties (java.util.Properties)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2