Search in sources :

Example 16 with RandomUUIDFactory

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();
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) StreamsConfig(org.apache.kafka.streams.StreamsConfig)

Example 17 with RandomUUIDFactory

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());
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) Var(org.openrdf.query.algebra.Var) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) ProcessorEntry(org.apache.rya.streams.kafka.topology.TopologyFactory.ProcessorEntry) Test(org.junit.Test)

Example 18 with RandomUUIDFactory

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);
}
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 19 with RandomUUIDFactory

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);
}
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 20 with RandomUUIDFactory

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);
}
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

RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)29 Test (org.junit.Test)27 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)25 HashSet (java.util.HashSet)23 UUID (java.util.UUID)23 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)23 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)23 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)23 ValueFactory (org.openrdf.model.ValueFactory)23 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)23 MapBindingSet (org.openrdf.query.impl.MapBindingSet)19 ArrayList (java.util.ArrayList)18 ProcessorEntry (org.apache.rya.streams.kafka.topology.TopologyFactory.ProcessorEntry)4 Projection (org.openrdf.query.algebra.Projection)4 StatementPattern (org.openrdf.query.algebra.StatementPattern)4 Var (org.openrdf.query.algebra.Var)4 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)4 Properties (java.util.Properties)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2