Search in sources :

Example 1 with KafkaLoadStatements

use of org.apache.rya.streams.kafka.interactor.KafkaLoadStatements in project incubator-rya by apache.

the class RunQueryCommandIT method runQuery.

@Test
public void runQuery() throws Exception {
    // Register a query with the Query Repository.
    final StreamsQuery sQuery = queryRepo.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
    // Arguments that run the query we just registered with Rya Streams.
    final String[] args = new String[] { "--ryaInstance", "" + ryaInstance, "--kafkaHostname", kafka.getKafkaHostname(), "--kafkaPort", kafka.getKafkaPort(), "--queryID", sQuery.getQueryId().toString(), "--zookeepers", kafka.getZookeeperServers() };
    // Create a new Thread that runs the command.
    final Thread commandThread = new Thread() {

        @Override
        public void run() {
            final RunQueryCommand command = new RunQueryCommand();
            try {
                command.execute(args);
            } catch (ArgumentsException | ExecutionException e) {
            // Do nothing. Test will still fail because the expected results will be missing.
            }
        }
    };
    // 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"));
    // Execute the test. This will result in a set of results that were read from the results topic.
    final List<VisibilityBindingSet> results;
    try {
        // Wait for the program to start.
        commandThread.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));
        results = KafkaTestUtil.pollForResults(500, 6, 3, resultConsumer);
    } finally {
        // Tear down the test.
        commandThread.interrupt();
        commandThread.join(3000);
    }
    // Show the read results matched the expected ones.
    assertEquals(expected, results);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ArgumentsException(org.apache.rya.streams.client.RyaStreamsCommand.ArgumentsException) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ExecutionException(org.apache.rya.streams.client.RyaStreamsCommand.ExecutionException) Test(org.junit.Test)

Example 2 with KafkaLoadStatements

use of org.apache.rya.streams.kafka.interactor.KafkaLoadStatements 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();
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) SingleThreadKafkaStreamsFactory(org.apache.rya.streams.kafka.SingleThreadKafkaStreamsFactory) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) SingleThreadKafkaStreamsFactory(org.apache.rya.streams.kafka.SingleThreadKafkaStreamsFactory) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 3 with KafkaLoadStatements

use of org.apache.rya.streams.kafka.interactor.KafkaLoadStatements 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();
    }
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) Properties(java.util.Properties) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) StreamsConfig(org.apache.kafka.streams.StreamsConfig)

Example 4 with KafkaLoadStatements

use of org.apache.rya.streams.kafka.interactor.KafkaLoadStatements in project incubator-rya by apache.

the class LoadStatementsCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final LoadStatementsParameters params = new LoadStatementsParameters();
    try {
        new JCommander(params, args);
    } catch (final ParameterException e) {
        throw new ArgumentsException("Could not load the Statements file because of invalid command line parameters.", e);
    }
    final Path statementsPath = Paths.get(params.statementsFile);
    final Properties producerProps = buildProperties(params);
    try (final Producer<Object, VisibilityStatement> producer = new KafkaProducer<>(producerProps)) {
        final LoadStatements statements = new KafkaLoadStatements(KafkaTopics.statementsTopic(params.ryaInstance), producer);
        System.out.printf("Loading statements from file `%s` using visibilities `%s`.\n", statementsPath, params.visibilities);
        statements.fromFile(statementsPath, params.visibilities);
    } catch (final Exception e) {
        System.err.println("Unable to parse statements file: " + statementsPath.toString());
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) Properties(java.util.Properties) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ParameterException(com.beust.jcommander.ParameterException) JCommander(com.beust.jcommander.JCommander) KafkaLoadStatements(org.apache.rya.streams.kafka.interactor.KafkaLoadStatements) ParameterException(com.beust.jcommander.ParameterException)

Aggregations

VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)4 KafkaLoadStatements (org.apache.rya.streams.kafka.interactor.KafkaLoadStatements)4 LoadStatements (org.apache.rya.streams.api.interactor.LoadStatements)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)2 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)2 Test (org.junit.Test)2 ValueFactory (org.openrdf.model.ValueFactory)2 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)2 MapBindingSet (org.openrdf.query.impl.MapBindingSet)2 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 Path (java.nio.file.Path)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 KafkaStreams (org.apache.kafka.streams.KafkaStreams)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 ArgumentsException (org.apache.rya.streams.client.RyaStreamsCommand.ArgumentsException)1 ExecutionException (org.apache.rya.streams.client.RyaStreamsCommand.ExecutionException)1 KafkaStreamsFactory (org.apache.rya.streams.kafka.KafkaStreamsFactory)1