use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.
the class PcjTables method populatePcj.
/**
* Scan Rya for results that solve the PCJ's query and store them in the PCJ table.
* <p>
* This method assumes the PCJ table has already been created.
*
* @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
* @param pcjTableName - The name of the PCJ table that will receive the results. (not null)
* @param ryaConn - A connection to the Rya store that will be queried to find results. (not null)
* @throws PCJStorageException If results could not be written to the PCJ table,
* the PCJ table does not exist, or the query that is being execute
* was malformed.
*/
public void populatePcj(final Connector accumuloConn, final String pcjTableName, final RepositoryConnection ryaConn) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
checkNotNull(ryaConn);
try {
// Fetch the query that needs to be executed from the PCJ table.
final PcjMetadata pcjMetadata = getPcjMetadata(accumuloConn, pcjTableName);
final String sparql = pcjMetadata.getSparql();
// Query Rya for results to the SPARQL query.
final TupleQuery query = ryaConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
final TupleQueryResult results = query.evaluate();
// Load batches of 1000 of them at a time into the PCJ table
final Set<VisibilityBindingSet> batch = new HashSet<>(1000);
while (results.hasNext()) {
batch.add(new VisibilityBindingSet(results.next()));
if (batch.size() == 1000) {
addResults(accumuloConn, pcjTableName, batch);
batch.clear();
}
}
if (!batch.isEmpty()) {
addResults(accumuloConn, pcjTableName, batch);
}
} catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
throw new PCJStorageException("Could not populate a PCJ table with Rya results for the table named: " + pcjTableName, e);
}
}
use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.
the class VisibilityBindingSetStringConverterTest method fromString.
@Test
public void fromString() throws BindingSetConversionException {
// Setup the String that will be converted.
final String bindingSetString = "http://b<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://c<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://a<<~>>http://www.w3.org/2001/XMLSchema#anyURI" + VISIBILITY_DELIM + "A&B";
// Convert it to a BindingSet
final VariableOrder varOrder = new VariableOrder("y", "z", "x");
final VisibilityBindingSetStringConverter converter = new VisibilityBindingSetStringConverter();
final BindingSet bindingSet = converter.convert(bindingSetString, varOrder);
// Ensure it converted to the expected result.
final MapBindingSet expected = new MapBindingSet();
expected.addBinding("z", new URIImpl("http://c"));
expected.addBinding("y", new URIImpl("http://b"));
expected.addBinding("x", new URIImpl("http://a"));
final VisibilityBindingSet visiSet = new VisibilityBindingSet(expected, "A&B");
assertEquals(visiSet, bindingSet);
}
use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.
the class VisibilityBindingSetStringConverterTest method toString_URIs.
@Test
public void toString_URIs() throws BindingSetConversionException {
// Setup the binding set that will be converted.
final MapBindingSet originalBindingSet = new MapBindingSet();
originalBindingSet.addBinding("x", new URIImpl("http://a"));
originalBindingSet.addBinding("y", new URIImpl("http://b"));
originalBindingSet.addBinding("z", new URIImpl("http://c"));
final VisibilityBindingSet visiSet = new VisibilityBindingSet(originalBindingSet, "A&B&C");
// Convert it to a String.
final VariableOrder varOrder = new VariableOrder("y", "z", "x");
final VisibilityBindingSetStringConverter converter = new VisibilityBindingSetStringConverter();
final String bindingSetString = converter.convert(visiSet, varOrder);
// Ensure it converted to the expected result.l
final String expected = "http://b<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://c<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://a<<~>>http://www.w3.org/2001/XMLSchema#anyURI" + VISIBILITY_DELIM + "A&B&C";
assertEquals(expected, bindingSetString);
}
use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.
the class BatchIT method simpleJoinAdd.
@Test
public void simpleJoinAdd() throws Exception {
final String sparql = "SELECT ?subject ?object1 ?object2 WHERE { ?subject <urn:predicate_1> ?object1; " + " <urn:predicate_2> ?object2 } ";
try (FluoClient fluoClient = new FluoClientImpl(getFluoConfiguration())) {
RyaURI subj = new RyaURI("urn:subject_1");
RyaStatement statement2 = new RyaStatement(subj, new RyaURI("urn:predicate_2"), null);
Set<RyaStatement> statements2 = getRyaStatements(statement2, 5);
// Create the PCJ table.
final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(getAccumuloConnector(), getRyaInstanceName());
final String pcjId = pcjStorage.createPcj(sparql);
// Tell the Fluo app to maintain the PCJ.
String queryId = new CreateFluoPcj().withRyaIntegration(pcjId, pcjStorage, fluoClient, getAccumuloConnector(), getRyaInstanceName()).getQueryId();
List<String> ids = getNodeIdStrings(fluoClient, queryId);
String joinId = ids.get(2);
String rightSp = ids.get(4);
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("subject", vf.createURI("urn:subject_1"));
bs.addBinding("object1", vf.createURI("urn:object_0"));
VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
URI uri = vf.createURI("urn:subject_1");
Bytes prefixBytes = BindingHashShardingFunction.getShardedScanPrefix(rightSp, uri);
Span span = Span.prefix(prefixBytes);
// Stream the data into Fluo.
InsertTriples inserter = new InsertTriples();
inserter.insert(fluoClient, statements2, Optional.absent());
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(0, 0, 0, 0, 5));
JoinBatchInformation batch = JoinBatchInformation.builder().setBatchSize(1).setColumn(FluoQueryColumns.STATEMENT_PATTERN_BINDING_SET).setSpan(span).setTask(Task.Add).setJoinType(JoinType.NATURAL_JOIN).setSide(Side.LEFT).setBs(vBs).build();
// Verify the end results of the query match the expected results.
createSpanBatch(fluoClient, joinId, batch);
getMiniFluo().waitForObservers();
verifyCounts(fluoClient, ids, Arrays.asList(5, 5, 5, 0, 5));
}
}
use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.
the class KafkaExportIT method count.
@Test
public void count() throws Exception {
// A query that counts the number of unique items that are in the inventory.
final String sparql = "SELECT (count(?item) as ?itemCount) { " + "?item <urn:id> ?id . " + "}";
// Create the Statements that will be loaded into Rya.
final ValueFactory vf = new ValueFactoryImpl();
final Collection<Statement> statements = Sets.newHashSet(// Three that are part of the count.
vf.createStatement(vf.createURI("urn:apple"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), vf.createStatement(vf.createURI("urn:gum"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), vf.createStatement(vf.createURI("urn:sandwich"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), // One that is not.
vf.createStatement(vf.createURI("urn:sandwich"), vf.createURI("urn:price"), vf.createLiteral(3.99)));
// Create the PCJ in Fluo and load the statements into Rya.
final String pcjId = loadDataAndCreateQuery(sparql, statements);
// Create the expected results of the SPARQL query once the PCJ has been computed.
final MapBindingSet expectedResult = new MapBindingSet();
expectedResult.addBinding("itemCount", vf.createLiteral("3", XMLSchema.INTEGER));
// Ensure the last result matches the expected result.
final VisibilityBindingSet result = readLastResult(pcjId);
assertEquals(expectedResult, result);
}
Aggregations