use of org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery in project incubator-rya by apache.
the class GetQueryReportIT method getReport.
@Test
public void getReport() throws Exception {
final String sparql = "SELECT ?worker ?company ?city" + "{ " + "FILTER(?worker = <http://Alice>) " + "?worker <http://worksAt> ?company . " + "?worker <http://livesIn> ?city ." + "}";
// Triples that will be streamed into Fluo after the PCJ has been created.
final Set<RyaStatement> streamedTriples = Sets.newHashSet(new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://worksAt"), new RyaURI("http://Taco Shop")), new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://worksAt"), new RyaURI("http://Burger Join")), new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://worksAt"), new RyaURI("http://Pastery Shop")), new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://livesIn"), new RyaURI("http://Lost County")), new RyaStatement(new RyaURI("http://Alice"), new RyaURI("http://livesIn"), new RyaURI("http://Big City")), new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://livesIn"), new RyaURI("http://Big City")), new RyaStatement(new RyaURI("http://Charlie"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://Charlie"), new RyaURI("http://livesIn"), new RyaURI("http://Big City")), new RyaStatement(new RyaURI("http://David"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://David"), new RyaURI("http://livesIn"), new RyaURI("http://Lost County")), new RyaStatement(new RyaURI("http://Eve"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://Eve"), new RyaURI("http://livesIn"), new RyaURI("http://Big City")), new RyaStatement(new RyaURI("http://Frank"), new RyaURI("http://worksAt"), new RyaURI("http://Burrito Place")), new RyaStatement(new RyaURI("http://Frank"), new RyaURI("http://livesIn"), new RyaURI("http://Lost County")));
// Create the PCJ table.
final Connector accumuloConn = super.getAccumuloConnector();
final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(accumuloConn, getRyaInstanceName());
final String pcjId = pcjStorage.createPcj(sparql);
try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
// Tell the Fluo app to maintain the PCJ.
new CreateFluoPcj().withRyaIntegration(pcjId, pcjStorage, fluoClient, accumuloConn, getRyaInstanceName());
// Stream the data into Fluo.
new InsertTriples().insert(fluoClient, streamedTriples, Optional.<String>absent());
// Wait for the results to finish processing.
super.getMiniFluo().waitForObservers();
// Fetch the report.
final Map<String, PcjMetadata> metadata = new GetPcjMetadata().getMetadata(pcjStorage, fluoClient);
final Set<String> queryIds = metadata.keySet();
assertEquals(1, queryIds.size());
final String queryId = queryIds.iterator().next();
final QueryReport report = new GetQueryReport().getReport(fluoClient, queryId);
// Build the expected counts map.
final Map<String, BigInteger> expectedCounts = new HashMap<>();
final FluoQuery fluoQuery = report.getFluoQuery();
final String queryNodeId = fluoQuery.getQueryMetadata().getNodeId();
expectedCounts.put(queryNodeId, BigInteger.valueOf(8));
final String filterNodeId = fluoQuery.getFilterMetadata().iterator().next().getNodeId();
expectedCounts.put(filterNodeId, BigInteger.valueOf(8));
final String joinNodeId = fluoQuery.getJoinMetadata().iterator().next().getNodeId();
expectedCounts.put(joinNodeId, BigInteger.valueOf(13));
final Iterator<StatementPatternMetadata> patterns = fluoQuery.getStatementPatternMetadata().iterator();
final StatementPatternMetadata sp1 = patterns.next();
final StatementPatternMetadata sp2 = patterns.next();
if (sp1.getStatementPattern().contains("http://worksAt")) {
expectedCounts.put(sp1.getNodeId(), BigInteger.valueOf(9));
expectedCounts.put(sp2.getNodeId(), BigInteger.valueOf(7));
} else {
expectedCounts.put(sp2.getNodeId(), BigInteger.valueOf(9));
expectedCounts.put(sp1.getNodeId(), BigInteger.valueOf(7));
}
assertEquals(expectedCounts, report.getCounts());
}
}
use of org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery in project incubator-rya by apache.
the class StatementPatternIdCacheIT method statementPatternIdCacheTest.
/**
* Ensure streamed matches are included in the result.
*/
@Test
public void statementPatternIdCacheTest() throws Exception {
// A query that finds people who talk to Eve and work at Chipotle.
final String sparql1 = "SELECT ?x WHERE { " + "?x <urn:pred1> <urn:obj1>. " + "?x <urn:pred2> <urn:obj2>." + "}";
final String sparql2 = "SELECT ?x WHERE { " + "?x <urn:pred3> <urn:obj3>. " + "?x <urn:pred4> <urn:obj4>." + "}";
try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
String pcjId = FluoQueryUtils.createNewPcjId();
// Tell the Fluo app to maintain the PCJ.
FluoQuery query1 = new CreateFluoPcj().createPcj(pcjId, sparql1, new HashSet<>(), fluoClient);
Set<String> spIds1 = new HashSet<>();
for (StatementPatternMetadata metadata : query1.getStatementPatternMetadata()) {
spIds1.add(metadata.getNodeId());
}
StatementPatternIdCache cache = new StatementPatternIdCache();
assertEquals(spIds1, cache.getStatementPatternIds(fluoClient.newTransaction()));
FluoQuery query2 = new CreateFluoPcj().createPcj(pcjId, sparql2, new HashSet<>(), fluoClient);
Set<String> spIds2 = new HashSet<>();
for (StatementPatternMetadata metadata : query2.getStatementPatternMetadata()) {
spIds2.add(metadata.getNodeId());
}
assertEquals(Sets.union(spIds1, spIds2), cache.getStatementPatternIds(fluoClient.newTransaction()));
}
}
use of org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery in project incubator-rya by apache.
the class CreateFluoPcj method createPcj.
/**
* Tells the Fluo PCJ Updater application to maintain a new PCJ. This method provides
* no guarantees that a PCJ with the given pcjId exists outside of Fluo. This method merely
* creates the FluoQuery (metadata) inside of Fluo so that results can be incrementally generated
* inside of Fluo. Results are exported according to the Set of {@link ExportStrategy} enums. If
* the Rya ExportStrategy is specified, care should be taken to verify that the PCJ table exists.
*
* @param pcjId - Identifies the PCJ that will be updated by the Fluo app. (not null)
* @param sparql - sparql query String to be registered with Fluo
* @param strategies - ExportStrategies used to specify how final results will be handled
* @param fluo - A connection to the Fluo application that updates the PCJ index. (not null)
* @return The metadata that was written to the Fluo application for the PCJ.
* @throws UnsupportedQueryException
* @throws MalformedQueryException
*/
public FluoQuery createPcj(final String pcjId, final String sparql, final Set<ExportStrategy> strategies, final FluoClient fluo) throws MalformedQueryException, UnsupportedQueryException {
requireNonNull(pcjId);
requireNonNull(sparql);
requireNonNull(strategies);
requireNonNull(fluo);
FluoQuery fluoQuery = makeFluoQuery(sparql, pcjId, strategies);
writeFluoQuery(fluo, fluoQuery, pcjId);
return fluoQuery;
}
Aggregations