Search in sources :

Example 6 with PcjMetadata

use of org.apache.rya.indexing.pcj.storage.PcjMetadata 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 7 with PcjMetadata

use of org.apache.rya.indexing.pcj.storage.PcjMetadata 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 8 with PcjMetadata

use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.

the class PcjTablesIT method loadPcjResults.

/**
 * Scan accumulo for the results that are stored in a PCJ table. The
 * multimap stores a set of deserialized binding sets that were in the PCJ
 * table for every variable order that is found in the PCJ metadata.
 */
private static Multimap<String, BindingSet> loadPcjResults(final Connector accumuloConn, final String pcjTableName) throws PcjException, TableNotFoundException, BindingSetConversionException {
    final Multimap<String, BindingSet> fetchedResults = HashMultimap.create();
    // Get the variable orders the data was written to.
    final PcjTables pcjs = new PcjTables();
    final PcjMetadata pcjMetadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    // Scan Accumulo for the stored results.
    for (final VariableOrder varOrder : pcjMetadata.getVarOrders()) {
        final Scanner scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
        scanner.fetchColumnFamily(new Text(varOrder.toString()));
        for (final Entry<Key, Value> entry : scanner) {
            final byte[] serializedResult = entry.getKey().getRow().getBytes();
            final BindingSet result = converter.convert(serializedResult, varOrder);
            fetchedResults.put(varOrder.toString(), result);
        }
    }
    return fetchedResults;
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) Text(org.apache.hadoop.io.Text) Value(org.apache.accumulo.core.data.Value) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) Key(org.apache.accumulo.core.data.Key)

Example 9 with PcjMetadata

use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.

the class PcjTablesIT method populatePcj.

/**
 * Ensure when results are already stored in Rya, that we are able to populate
 * the PCJ table for a new SPARQL query using those results.
 * <p>
 * The method being tested is: {@link PcjTables#populatePcj(Connector, String, RepositoryConnection, String)}
 */
@Test
public void populatePcj() throws RepositoryException, PcjException, TableNotFoundException, BindingSetConversionException, AccumuloException, AccumuloSecurityException {
    // Load some Triples into Rya.
    final Set<Statement> triples = new HashSet<>();
    triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)));
    triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
    triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)));
    triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
    triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)));
    triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
    triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)));
    triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
    for (final Statement triple : triples) {
        ryaConn.add(triple);
    }
    // Create a PCJ table that will include those triples in its results.
    final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
    final Connector accumuloConn = cluster.getConnector();
    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);
    // Populate the PCJ table using a Rya connection.
    pcjs.populatePcj(accumuloConn, pcjTableName, ryaConn);
    // Scan Accumulo for the stored results.
    final Multimap<String, BindingSet> fetchedResults = loadPcjResults(accumuloConn, pcjTableName);
    // Make sure the cardinality was updated.
    final PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);
    assertEquals(3, metadata.getCardinality());
    // Ensure the expected results match those that were stored.
    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);
    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) Statement(org.openrdf.model.Statement) URIImpl(org.openrdf.model.impl.URIImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) StatementImpl(org.openrdf.model.impl.StatementImpl) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with PcjMetadata

use of org.apache.rya.indexing.pcj.storage.PcjMetadata in project incubator-rya by apache.

the class PcjTablesIT method dropPcj.

@Test
public void dropPcj() throws PCJStorageException, AccumuloException, AccumuloSecurityException {
    final Connector accumuloConn = cluster.getConnector();
    // Create a PCJ index.
    final String tableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), "thePcj");
    final Set<VariableOrder> varOrders = Sets.<VariableOrder>newHashSet(new VariableOrder("x"));
    final String sparql = "SELECT x WHERE ?x <http://isA> <http://Food>";
    final PcjTables pcjs = new PcjTables();
    pcjs.createPcjTable(accumuloConn, tableName, varOrders, sparql);
    // Fetch its metadata to show that it has actually been created.
    final PcjMetadata expectedMetadata = new PcjMetadata(sparql, 0L, varOrders);
    PcjMetadata metadata = pcjs.getPcjMetadata(accumuloConn, tableName);
    assertEquals(expectedMetadata, metadata);
    // Drop it.
    pcjs.dropPcjTable(accumuloConn, tableName);
    // Show the metadata is no longer present.
    PCJStorageException tableDoesNotExistException = null;
    try {
        metadata = pcjs.getPcjMetadata(accumuloConn, tableName);
    } catch (final PCJStorageException e) {
        tableDoesNotExistException = e;
    }
    assertNotNull(tableDoesNotExistException);
}
Also used : Connector(org.apache.accumulo.core.client.Connector) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) Test(org.junit.Test)

Aggregations

PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)47 Test (org.junit.Test)30 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)22 HashSet (java.util.HashSet)17 VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)16 URIImpl (org.openrdf.model.impl.URIImpl)15 MapBindingSet (org.openrdf.query.impl.MapBindingSet)15 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)14 BindingSet (org.openrdf.query.BindingSet)14 Connector (org.apache.accumulo.core.client.Connector)12 PCJStorageException (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)11 NumericLiteralImpl (org.openrdf.model.impl.NumericLiteralImpl)10 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)9 ShiftVarOrderFactory (org.apache.rya.indexing.pcj.storage.accumulo.ShiftVarOrderFactory)8 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)6 Statement (org.openrdf.model.Statement)6 LiteralImpl (org.openrdf.model.impl.LiteralImpl)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 MalformedQueryException (org.openrdf.query.MalformedQueryException)6 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)6