Search in sources :

Example 6 with PcjException

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

the class PcjIntegrationTestingUtil 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 PcjException
 *             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 static void populatePcj(final Connector accumuloConn, final String pcjTableName, final RepositoryConnection ryaConn) throws PcjException {
    checkNotNull(accumuloConn);
    checkNotNull(pcjTableName);
    checkNotNull(ryaConn);
    try {
        // Fetch the query that needs to be executed from the PCJ table.
        final PcjMetadata pcjMetadata = new PcjTables().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<BindingSet> batch = new HashSet<>(1000);
        while (results.hasNext()) {
            batch.add(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 PcjException("Could not populate a PCJ table with Rya results for the table named: " + pcjTableName, e);
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) TupleQuery(org.openrdf.query.TupleQuery) RepositoryException(org.openrdf.repository.RepositoryException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PcjTables(org.apache.rya.indexing.pcj.storage.accumulo.PcjTables) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet)

Example 7 with PcjException

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

the class PcjIntegrationTestingUtil method makeWriteResultMutations.

/**
 * Create the {@link Mutations} required to write a new {@link BindingSet}
 * to a PCJ table for each {@link VariableOrder} that is provided.
 *
 * @param varOrders
 *            - The variables orders the result will be written to. (not
 *            null)
 * @param result
 *            - A new PCJ result. (not null)
 * @return Mutation that will write the result to a PCJ table.
 * @throws PcjException
 *             The binding set could not be encoded.
 */
private static Set<Mutation> makeWriteResultMutations(final Set<VariableOrder> varOrders, final BindingSet result) throws PcjException {
    checkNotNull(varOrders);
    checkNotNull(result);
    final Set<Mutation> mutations = new HashSet<>();
    for (final VariableOrder varOrder : varOrders) {
        try {
            // Serialize the result to the variable order.
            final byte[] serializedResult = converter.convert(result, varOrder);
            // Row ID = binding set values, Column Family = variable order
            // of the binding set.
            final Mutation addResult = new Mutation(serializedResult);
            addResult.put(varOrder.toString(), "", "");
            mutations.add(addResult);
        } catch (final BindingSetConversionException e) {
            throw new PcjException("Could not serialize a result.", e);
        }
    }
    return mutations;
}
Also used : VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) Mutation(org.apache.accumulo.core.data.Mutation) BindingSetConversionException(org.apache.rya.indexing.pcj.storage.accumulo.BindingSetConverter.BindingSetConversionException) HashSet(java.util.HashSet)

Example 8 with PcjException

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

the class PcjIntegrationTestingUtil method writeResults.

/**
 * Add a collection of results to a specific PCJ table.
 *
 * @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 results
 *            - Binding sets that will be written to the PCJ table. (not
 *            null)
 * @throws PcjException
 *             The provided PCJ table doesn't exist, is missing the PCJ
 *             metadata, or the result could not be written to it.
 */
private static void writeResults(final Connector accumuloConn, final String pcjTableName, final Collection<BindingSet> results) throws PcjException {
    checkNotNull(accumuloConn);
    checkNotNull(pcjTableName);
    checkNotNull(results);
    // Fetch the variable orders from the PCJ table.
    final PcjMetadata metadata = new PcjTables().getPcjMetadata(accumuloConn, pcjTableName);
    // Write each result formatted using each of the variable orders.
    BatchWriter writer = null;
    try {
        writer = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
        for (final BindingSet result : results) {
            final Set<Mutation> addResultMutations = makeWriteResultMutations(metadata.getVarOrders(), result);
            writer.addMutations(addResultMutations);
        }
    } catch (TableNotFoundException | MutationsRejectedException e) {
        throw new PcjException("Could not add results to the PCJ table named: " + pcjTableName, e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (final MutationsRejectedException e) {
                throw new PcjException("Could not add results to a PCJ table because some of the mutations were rejected.", e);
            }
        }
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PcjTables(org.apache.rya.indexing.pcj.storage.accumulo.PcjTables) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 9 with PcjException

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

the class AccumuloCreatePeriodicPCJ method createPeriodicPCJ.

@Override
public String createPeriodicPCJ(String instanceName, String sparql, String periodicTopic, String bootStrapServers) throws RyaClientException {
    requireNonNull(instanceName);
    requireNonNull(sparql);
    final Optional<RyaDetails> ryaDetailsHolder = getInstanceDetails.getDetails(instanceName);
    final boolean ryaInstanceExists = ryaDetailsHolder.isPresent();
    if (!ryaInstanceExists) {
        throw new InstanceDoesNotExistException(String.format("The '%s' instance of Rya does not exist.", instanceName));
    }
    final PCJIndexDetails pcjIndexDetails = ryaDetailsHolder.get().getPCJIndexDetails();
    final boolean pcjIndexingEnabeld = pcjIndexDetails.isEnabled();
    if (!pcjIndexingEnabeld) {
        throw new RyaClientException(String.format("The '%s' instance of Rya does not have PCJ Indexing enabled.", instanceName));
    }
    // If a Fluo application is being used, task it with updating the PCJ.
    final Optional<FluoDetails> fluoDetailsHolder = pcjIndexDetails.getFluoDetails();
    if (fluoDetailsHolder.isPresent()) {
        final String fluoAppName = fluoDetailsHolder.get().getUpdateAppName();
        try {
            return updateFluoAppAndRegisterWithKafka(instanceName, fluoAppName, sparql, periodicTopic, bootStrapServers);
        } catch (RepositoryException | MalformedQueryException | SailException | QueryEvaluationException | PcjException | RyaDAOException | PeriodicQueryCreationException e) {
            throw new RyaClientException("Problem while initializing the Fluo application with the new PCJ.", e);
        } catch (UnsupportedQueryException e) {
            throw new RyaClientException("The new PCJ could not be initialized because it either contains an unsupported query node " + "or an invalid ExportStrategy for the given QueryType.  Projection queries can be exported to either Rya or Kafka," + "unless they contain an aggregation, in which case they can only be exported to Kafka.  Construct queries can be exported" + "to Rya and Kafka, and Periodic queries can only be exported to Rya.");
        }
    } else {
        throw new RyaClientException(String.format("The '%s' instance of Rya does not have PCJ Indexing enabled.", instanceName));
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) UnsupportedQueryException(org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException) RyaDetails(org.apache.rya.api.instance.RyaDetails) RepositoryException(org.openrdf.repository.RepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) PeriodicQueryCreationException(org.apache.rya.indexing.pcj.fluo.api.CreatePeriodicQuery.PeriodicQueryCreationException) FluoDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails) PCJIndexDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails)

Aggregations

PcjException (org.apache.rya.indexing.pcj.storage.PcjException)9 MalformedQueryException (org.openrdf.query.MalformedQueryException)6 RepositoryException (org.openrdf.repository.RepositoryException)5 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)4 BindingSet (org.openrdf.query.BindingSet)4 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)4 HashSet (java.util.HashSet)3 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)3 UnsupportedQueryException (org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException)3 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)3 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)3 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)3 Mutation (org.apache.accumulo.core.data.Mutation)2 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)2 RyaClientException (org.apache.rya.api.client.RyaClientException)2 RyaDetails (org.apache.rya.api.instance.RyaDetails)2 PCJIndexDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails)2 FluoDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails)2 CreateFluoPcj (org.apache.rya.indexing.pcj.fluo.api.CreateFluoPcj)2 PcjTables (org.apache.rya.indexing.pcj.storage.accumulo.PcjTables)2