Search in sources :

Example 1 with ExecutionException

use of org.apache.rya.streams.client.RyaStreamsCommand.ExecutionException 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)

Aggregations

ArrayList (java.util.ArrayList)1 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)1 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)1 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)1 LoadStatements (org.apache.rya.streams.api.interactor.LoadStatements)1 ArgumentsException (org.apache.rya.streams.client.RyaStreamsCommand.ArgumentsException)1 ExecutionException (org.apache.rya.streams.client.RyaStreamsCommand.ExecutionException)1 KafkaLoadStatements (org.apache.rya.streams.kafka.interactor.KafkaLoadStatements)1 Test (org.junit.Test)1 ValueFactory (org.openrdf.model.ValueFactory)1 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)1 MapBindingSet (org.openrdf.query.impl.MapBindingSet)1