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);
}
}
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;
}
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);
}
}
}
}
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));
}
}
Aggregations