use of org.apache.rya.streams.api.interactor.LoadStatements in project incubator-rya by apache.
the class KafkaRunQueryIT method runQuery.
@Test
public void runQuery() throws Exception {
// Setup some constant that will be used through the test.
final String ryaInstance = UUID.randomUUID().toString();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
// This query is completely in memory, so it doesn't need to be closed.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
// Add the query to the query repository.
final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
final UUID queryId = sQuery.getQueryId();
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// The thread that will run the tested interactor.
final Thread testThread = new Thread() {
@Override
public void run() {
final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
try {
runQuery.run(queryId);
} catch (final RyaStreamsException e) {
// Do nothing. Test will still fail because the expected results will be missing.
}
}
};
// Create the topics.
kafka.createTopic(statementsTopic);
kafka.createTopic(resultsTopic);
// 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.
testThread.start();
Thread.sleep(2000);
// Write some statements to the program.
final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
loadStatements.fromCollection(statements);
// Read the output of the streams program.
consumer.subscribe(Lists.newArrayList(resultsTopic));
results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
} finally {
// Tear down the test.
testThread.interrupt();
testThread.join(3000);
}
// Show the read results matched the expected ones.
assertEquals(expected, results);
}
use of org.apache.rya.streams.api.interactor.LoadStatements 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);
}
use of org.apache.rya.streams.api.interactor.LoadStatements 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();
}
}
use of org.apache.rya.streams.api.interactor.LoadStatements 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();
}
}
Aggregations