Search in sources :

Example 21 with PcjMetadata

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

the class PcjTables method listResults.

/**
 * Get an {@link Iterator} over the {@link BindingSet}s that are stored in the PCJ table.
 *
 * @param accumuloConn - A connection to the Accumulo that hsots the PCJ table. (not null)
 * @param pcjTableName - The name of the PCJ table that will be scanned. (not null)
 * @param auths - the user's authorizations that will be used to scan the table. (not null)
 * @return An iterator over all of the {@link BindingSet}s that are stored as
 *   results for the PCJ.
 * @throws PCJStorageException The binding sets could not be fetched.
 */
public CloseableIterator<BindingSet> listResults(final Connector accumuloConn, final String pcjTableName, final Authorizations auths) throws PCJStorageException {
    requireNonNull(pcjTableName);
    // Fetch the Variable Orders for the binding sets and choose one of them. It
    // doesn't matter which one we choose because they all result in the same output.
    final PcjMetadata metadata = getPcjMetadata(accumuloConn, pcjTableName);
    final VariableOrder varOrder = metadata.getVarOrders().iterator().next();
    try {
        // Fetch only the Binding Sets whose Variable Order matches the selected one.
        final Scanner scanner = accumuloConn.createScanner(pcjTableName, auths);
        scanner.fetchColumnFamily(new Text(varOrder.toString()));
        // Return an Iterator that uses that scanner.
        return new ScannerBindingSetIterator(scanner, varOrder);
    } catch (final TableNotFoundException e) {
        throw new PCJStorageException(String.format("PCJ Table does not exist for name '%s'.", pcjTableName), e);
    }
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) Text(org.apache.hadoop.io.Text) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Example 22 with PcjMetadata

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

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

the class PcjTablesTest method pcjMetadata_hashCode.

@Test
public void pcjMetadata_hashCode() {
    PcjMetadata meta1 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
    PcjMetadata meta2 = new PcjMetadata("A SPARQL string.", 5, Sets.newHashSet(new VariableOrder("a", "b", "c"), new VariableOrder("d", "e", "f")));
    assertEquals(meta1.hashCode(), meta2.hashCode());
}
Also used : VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) Test(org.junit.Test)

Example 24 with PcjMetadata

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

the class CreateFluoPcj method createPcj.

/**
 * Tells the Fluo PCJ Updater application to maintain a new PCJ.  The method takes in an
 * instance of {@link PrecomputedJoinStorage} to verify that a PCJ with the given pcjId exists.
 * Results are exported to a PCJ table with the provided pcjId according to the Rya
 * {@link ExportStrategy}.
 *
 * @param pcjId - Identifies the PCJ that will be updated by the Fluo app. (not null)
 * @param pcjStorage - Provides access to the PCJ index. (not null)
 * @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 MalformedQueryException The SPARQL query stored for the {@code pcjId} is malformed.
 * @throws PcjException The PCJ Metadata for {@code pcjId} could not be read from {@code pcjStorage}.
 * @throws UnsupportedQueryException
 */
public FluoQuery createPcj(final String pcjId, final PrecomputedJoinStorage pcjStorage, final FluoClient fluo) throws MalformedQueryException, PcjException, UnsupportedQueryException {
    requireNonNull(pcjId);
    requireNonNull(pcjStorage);
    requireNonNull(fluo);
    // Parse the query's structure for the metadata that will be written to fluo.
    final PcjMetadata pcjMetadata = pcjStorage.getPcjMetadata(pcjId);
    final String sparql = pcjMetadata.getSparql();
    return createPcj(pcjId, sparql, Sets.newHashSet(ExportStrategy.RYA), fluo);
}
Also used : PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata)

Example 25 with PcjMetadata

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

the class ListQueriesCommand method execute.

@Override
public void execute(final Connector accumulo, final String ryaTablePrefix, final RyaSailRepository rya, final FluoClient fluo, final String[] args) throws ArgumentsException, ExecutionException {
    checkNotNull(accumulo);
    checkNotNull(fluo);
    checkNotNull(args);
    log.trace("Executing the List Queries Command...");
    // Parse the command line arguments.
    final Parameters params = new Parameters();
    try {
        new JCommander(params, args);
    } catch (final ParameterException e) {
        throw new ArgumentsException("Could not list the queries because of invalid command line parameters.", e);
    }
    // Fetch the PCJ metadata that will be included in the report.
    final GetPcjMetadata getPcjMetadata = new GetPcjMetadata();
    final Map<String, PcjMetadata> metadata = new HashMap<String, PcjMetadata>();
    try {
        final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(accumulo, ryaTablePrefix);
        if (params.queryId != null) {
            log.trace("Fetch the PCJ Metadata from Accumulo for Query ID '" + params.queryId + "'.");
            metadata.put(params.queryId, getPcjMetadata.getMetadata(pcjStorage, fluo, params.queryId));
        } else {
            log.trace("Fetch the PCJ Metadata from Accumulo for all queries that are being updated by Fluo.");
            metadata.putAll(getPcjMetadata.getMetadata(pcjStorage, fluo));
        }
    } catch (NotInFluoException | NotInAccumuloException e) {
        throw new ExecutionException("Could not fetch some of the metadata required to build the report.", e);
    }
    // Write the metadata to the console.
    log.trace("Rendering the queries report...");
    if (metadata.isEmpty()) {
        System.out.println("No queries are being tracked by Fluo.");
    } else {
        final PcjMetadataRenderer renderer = new PcjMetadataRenderer();
        try {
            final String report = renderer.render(metadata);
            System.out.println("The number of Queries that are being tracked by Fluo: " + metadata.size());
            System.out.println(report);
        } catch (final Exception e) {
            throw new ExecutionException("Unable to render the query metadata report for output.", e);
        }
    }
    log.trace("Finished executing the List Queries Command.");
}
Also used : NotInFluoException(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata.NotInFluoException) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) NotInAccumuloException(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata.NotInAccumuloException) HashMap(java.util.HashMap) ParameterException(com.beust.jcommander.ParameterException) NotInFluoException(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata.NotInFluoException) NotInAccumuloException(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata.NotInAccumuloException) JCommander(com.beust.jcommander.JCommander) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) PcjMetadataRenderer(org.apache.rya.indexing.pcj.fluo.client.util.PcjMetadataRenderer) ParameterException(com.beust.jcommander.ParameterException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) GetPcjMetadata(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata) GetPcjMetadata(org.apache.rya.indexing.pcj.fluo.api.GetPcjMetadata)

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