Search in sources :

Example 51 with VisibilityBindingSet

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);
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) TupleQuery(org.openrdf.query.TupleQuery) RepositoryException(org.openrdf.repository.RepositoryException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet)

Example 52 with VisibilityBindingSet

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 53 with VisibilityBindingSet

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 54 with VisibilityBindingSet

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));
    }
}
Also used : FluoClient(org.apache.fluo.api.client.FluoClient) FluoClientImpl(org.apache.fluo.core.client.FluoClientImpl) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) InsertTriples(org.apache.rya.indexing.pcj.fluo.api.InsertTriples) RyaStatement(org.apache.rya.api.domain.RyaStatement) CreateFluoPcj(org.apache.rya.indexing.pcj.fluo.api.CreateFluoPcj) JoinBatchInformation(org.apache.rya.indexing.pcj.fluo.app.batch.JoinBatchInformation) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Span(org.apache.fluo.api.data.Span) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) Bytes(org.apache.fluo.api.data.Bytes) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) Test(org.junit.Test)

Example 55 with VisibilityBindingSet

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Aggregations

VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)140 Test (org.junit.Test)105 MapBindingSet (org.openrdf.query.impl.MapBindingSet)93 ValueFactory (org.openrdf.model.ValueFactory)66 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)66 HashSet (java.util.HashSet)52 ArrayList (java.util.ArrayList)31 UUID (java.util.UUID)28 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)28 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)24 BindingSet (org.openrdf.query.BindingSet)24 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)23 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)23 URIImpl (org.openrdf.model.impl.URIImpl)19 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)19 VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)18 Bytes (org.apache.fluo.api.data.Bytes)16 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)15 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)12 Statement (org.openrdf.model.Statement)12