Search in sources :

Example 41 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJ method updatePCJResults.

private void updatePCJResults(final String ryaInstanceName, final String pcjId) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
    // Things that have to be closed before we exit.
    Sail sail = null;
    SailConnection sailConn = null;
    CloseableIteration<? extends BindingSet, QueryEvaluationException> results = null;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(super.getConnector(), ryaInstanceName)) {
        // Create an instance of Sail backed by the Rya instance.
        sail = connectToRya(ryaInstanceName);
        // Purge the old results from the PCJ.
        try {
            pcjStorage.purge(pcjId);
        } catch (final PCJStorageException e) {
            throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
        }
        // Parse the PCJ's SPARQL query.
        final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
        final String sparql = metadata.getSparql();
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery parsedQuery = parser.parseQuery(sparql, null);
        // Execute the query.
        sailConn = sail.getConnection();
        results = sailConn.evaluate(parsedQuery.getTupleExpr(), null, null, false);
        // Load the results into the PCJ table.
        final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
        while (results.hasNext()) {
            final VisibilityBindingSet result = new VisibilityBindingSet(results.next(), "");
            batch.add(result);
            if (batch.size() == 1000) {
                pcjStorage.addResults(pcjId, batch);
                batch.clear();
            }
        }
        if (!batch.isEmpty()) {
            pcjStorage.addResults(pcjId, batch);
            batch.clear();
        }
    } catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException e) {
        throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (final QueryEvaluationException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sailConn != null) {
            try {
                sailConn.close();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
    }
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Example 42 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class PcjTablesIT method addResults.

/**
 * Ensure when results have been written to the PCJ table that they are in Accumulo.
 * <p>
 * The method being tested is {@link PcjTables#addResults(Connector, String, java.util.Collection)}
 */
@Test
public void addResults() throws PcjException, TableNotFoundException, BindingSetConversionException, AccumuloException, AccumuloSecurityException {
    final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
    final Connector accumuloConn = cluster.getConnector();
    // Create a PCJ table in the Mini Accumulo.
    final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
    final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
    final PcjTables pcjs = new PcjTables();
    pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
    // Add a few results to the PCJ table.
    final MapBindingSet alice = new MapBindingSet();
    alice.addBinding("name", new URIImpl("http://Alice"));
    alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
    final MapBindingSet bob = new MapBindingSet();
    bob.addBinding("name", new URIImpl("http://Bob"));
    bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
    final MapBindingSet charlie = new MapBindingSet();
    charlie.addBinding("name", new URIImpl("http://Charlie"));
    charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
    final Set<BindingSet> results = Sets.<BindingSet>newHashSet(alice, bob, charlie);
    pcjs.addResults(accumuloConn, pcjTableName, Sets.<VisibilityBindingSet>newHashSet(new VisibilityBindingSet(alice), new VisibilityBindingSet(bob), new VisibilityBindingSet(charlie)));
    // Make sure the cardinality was updated.
    final PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    assertEquals(3, metadata.getCardinality());
    // Scan Accumulo for the stored results.
    final Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
    // Ensure the expected results match those that were stored.
    final Multimap<String, BindingSet> expectedResults = HashMultimap.create();
    expectedResults.putAll("name;age", results);
    expectedResults.putAll("age;name", results);
    assertEquals(expectedResults, fetchedResults);
}
Also used : Connector(org.apache.accumulo.core.client.Connector) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) URIImpl(org.openrdf.model.impl.URIImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 43 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class PcjTablesIT method purge.

@Test
public void purge() throws PCJStorageException, AccumuloException, AccumuloSecurityException {
    final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
    final Connector accumuloConn = cluster.getConnector();
    // Create a PCJ table in the Mini Accumulo.
    final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "testPcj");
    final Set<VariableOrder> varOrders = new ShiftVarOrderFactory().makeVarOrders(new VariableOrder("name;age"));
    final PcjTables pcjs = new PcjTables();
    pcjs.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql);
    // Add a few results to the PCJ table.
    final MapBindingSet alice = new MapBindingSet();
    alice.addBinding("name", new URIImpl("http://Alice"));
    alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
    final MapBindingSet bob = new MapBindingSet();
    bob.addBinding("name", new URIImpl("http://Bob"));
    bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
    final MapBindingSet charlie = new MapBindingSet();
    charlie.addBinding("name", new URIImpl("http://Charlie"));
    charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
    pcjs.addResults(accumuloConn, pcjTableName, Sets.<VisibilityBindingSet>newHashSet(new VisibilityBindingSet(alice), new VisibilityBindingSet(bob), new VisibilityBindingSet(charlie)));
    // Make sure the cardinality was updated.
    PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    assertEquals(3, metadata.getCardinality());
    // Purge the data.
    pcjs.purgePcjTable(accumuloConn, pcjTableName);
    // Make sure the cardinality was updated to 0.
    metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    assertEquals(0, metadata.getCardinality());
}
Also used : Connector(org.apache.accumulo.core.client.Connector) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) URIImpl(org.openrdf.model.impl.URIImpl) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 44 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class MongoPcjDocumentsTest method metadataExists.

@Test
public void metadataExists() throws Exception {
    final List<VariableOrder> varOrders = Lists.newArrayList(new VariableOrder("b", "a"), new VariableOrder("a", "b"));
    final MongoPcjDocuments docConverter = new MongoPcjDocuments(getMongoClient(), conf.getRyaInstanceName());
    final String sparql = "SELECT * WHERE { ?a <http://isA> ?b }";
    docConverter.createPcj("pcjTest", sparql);
    PcjMetadata actual = docConverter.getPcjMetadata("pcjTest");
    PcjMetadata expected = new PcjMetadata(sparql, 0, varOrders);
    assertEquals(expected, actual);
    // Setup the binding set that will be converted.
    final MapBindingSet originalBindingSet1 = new MapBindingSet();
    originalBindingSet1.addBinding("x", new URIImpl("http://a"));
    originalBindingSet1.addBinding("y", new URIImpl("http://b"));
    originalBindingSet1.addBinding("z", new URIImpl("http://c"));
    final VisibilityBindingSet results1 = new VisibilityBindingSet(originalBindingSet1, "A&B&C");
    // Setup the binding set that will be converted.
    final MapBindingSet originalBindingSet2 = new MapBindingSet();
    originalBindingSet2.addBinding("x", new URIImpl("http://1"));
    originalBindingSet2.addBinding("y", new URIImpl("http://2"));
    originalBindingSet2.addBinding("z", new URIImpl("http://3"));
    final VisibilityBindingSet results2 = new VisibilityBindingSet(originalBindingSet2, "A&B&C");
    final List<VisibilityBindingSet> bindingSets = new ArrayList<>();
    bindingSets.add(results1);
    bindingSets.add(results2);
    docConverter.addResults("pcjTest", bindingSets);
    actual = docConverter.getPcjMetadata("pcjTest");
    expected = new PcjMetadata(sparql, 2, varOrders);
    assertEquals(expected, actual);
    docConverter.purgePcjs("pcjTest");
    actual = docConverter.getPcjMetadata("pcjTest");
    expected = new PcjMetadata(sparql, 0, varOrders);
    assertEquals(expected, actual);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) ArrayList(java.util.ArrayList) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 45 with VisibilityBindingSet

use of org.apache.rya.api.model.VisibilityBindingSet in project incubator-rya by apache.

the class PcjDocumentsIntegrationTest method listResults.

@Test
public void listResults() throws Exception {
    final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
    final String pcjTableName = "testPcj";
    final MongoPcjDocuments pcjs = new MongoPcjDocuments(getMongoClient(), conf.getRyaInstanceName());
    pcjs.createPcj(pcjTableName, sparql);
    // Add a few results to the PCJ table.
    final MapBindingSet alice = new MapBindingSet();
    alice.addBinding("name", new URIImpl("http://Alice"));
    alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
    final MapBindingSet bob = new MapBindingSet();
    bob.addBinding("name", new URIImpl("http://Bob"));
    bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
    final MapBindingSet charlie = new MapBindingSet();
    charlie.addBinding("name", new URIImpl("http://Charlie"));
    charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
    pcjs.addResults(pcjTableName, Sets.<VisibilityBindingSet>newHashSet(new VisibilityBindingSet(alice), new VisibilityBindingSet(bob), new VisibilityBindingSet(charlie)));
    // Fetch the Binding Sets that have been stored in the PCJ table.
    final Set<BindingSet> results = new HashSet<>();
    final CloseableIterator<BindingSet> resultsIt = pcjs.listResults(pcjTableName);
    try {
        while (resultsIt.hasNext()) {
            results.add(resultsIt.next());
        }
    } finally {
        resultsIt.close();
    }
    // Verify the fetched results match the expected ones.
    final Set<BindingSet> expected = Sets.<BindingSet>newHashSet(alice, bob, charlie);
    assertEquals(expected, results);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) 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