use of org.apache.accumulo.core.client.TableNotFoundException in project incubator-rya by apache.
the class AccumuloSelectivityEvalDAO method getCardinality.
// obtains cardinality for StatementPattern. Returns cardinality of 0
// if no instances of constants occur in table.
// assumes composite cardinalities will be used.
@Override
public long getCardinality(RdfCloudTripleStoreConfiguration conf, StatementPattern sp) throws TableNotFoundException {
Var subjectVar = sp.getSubjectVar();
Resource subj = (Resource) getConstantValue(subjectVar);
Var predicateVar = sp.getPredicateVar();
URI pred = (URI) getConstantValue(predicateVar);
Var objectVar = sp.getObjectVar();
org.openrdf.model.Value obj = getConstantValue(objectVar);
Resource context = (Resource) getConstantValue(sp.getContextVar());
/**
* We put full triple scans before rdf:type because more often than not the triple scan is being joined with something else that is better than asking the
* full rdf:type of everything.
*/
double cardinality = 0;
try {
cardinality = 2 * getTableSize(conf);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
if (subj != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
CARDINALITY_OF card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECT;
values.add(subj);
if (pred != null) {
values.add(pred);
card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECTPREDICATE;
} else if (obj != null) {
values.add(obj);
card = RdfEvalStatsDAO.CARDINALITY_OF.SUBJECTOBJECT;
}
double evalCard = this.getCardinality(conf, card, values, context);
// the index does not exist)
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else if (pred != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
CARDINALITY_OF card = RdfEvalStatsDAO.CARDINALITY_OF.PREDICATE;
values.add(pred);
if (obj != null) {
values.add(obj);
card = RdfEvalStatsDAO.CARDINALITY_OF.PREDICATEOBJECT;
}
double evalCard = this.getCardinality(conf, card, values, context);
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else if (obj != null) {
List<org.openrdf.model.Value> values = new ArrayList<org.openrdf.model.Value>();
values.add(obj);
double evalCard = this.getCardinality(conf, RdfEvalStatsDAO.CARDINALITY_OF.OBJECT, values, context);
if (evalCard >= 0) {
cardinality = Math.min(cardinality, evalCard);
} else {
// TODO change this to agree with prospector
cardinality = 0;
}
} else {
cardinality = getTableSize(conf);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
// TODO is this okay?
return (long) cardinality;
}
use of org.apache.accumulo.core.client.TableNotFoundException in project incubator-rya by apache.
the class ProspectorServiceEvalStatsDAO method getCardinality.
@Override
public double getCardinality(RdfCloudTripleStoreConfiguration conf, CARDINALITY_OF card, List<Value> val) throws RdfDAOException {
assert conf != null && card != null && val != null;
String triplePart = null;
switch(card) {
case SUBJECT:
triplePart = TripleValueType.SUBJECT.getIndexType();
break;
case PREDICATE:
triplePart = TripleValueType.PREDICATE.getIndexType();
break;
case OBJECT:
triplePart = TripleValueType.OBJECT.getIndexType();
break;
case SUBJECTPREDICATE:
triplePart = TripleValueType.SUBJECT_PREDICATE.getIndexType();
break;
case SUBJECTOBJECT:
triplePart = TripleValueType.SUBJECT_OBJECT.getIndexType();
break;
case PREDICATEOBJECT:
triplePart = TripleValueType.PREDICATE_OBJECT.getIndexType();
break;
}
final String[] auths = conf.getAuths();
final List<String> indexedValues = new ArrayList<>();
final Iterator<Value> valueIt = val.iterator();
while (valueIt.hasNext()) {
indexedValues.add(valueIt.next().stringValue());
}
double cardinality = -1;
try {
final List<IndexEntry> entries = prospectorService.query(null, ProspectorConstants.COUNT, triplePart, indexedValues, null, auths);
if (!entries.isEmpty()) {
cardinality = entries.iterator().next().getCount();
}
} catch (final TableNotFoundException e) {
throw new RdfDAOException(e);
}
return cardinality;
}
use of org.apache.accumulo.core.client.TableNotFoundException in project incubator-rya by apache.
the class RowRuleMapper method flush.
private void flush(final Context context) throws IOException, InterruptedException {
try {
childDao.flush();
} catch (final RyaDAOException e) {
throw new IOException("Error writing to in-memory table", e);
}
final TableOperations ops = childConnector.tableOperations();
final SecurityOperations secOps = childConnector.securityOperations();
Authorizations childAuths;
try {
childAuths = secOps.getUserAuthorizations(childUser);
} catch (AccumuloException | AccumuloSecurityException e) {
throw new IOException("Error connecting to mock instance", e);
}
for (final String table : ops.list()) {
// Only copy Rya tables (skip system tables)
if (!table.startsWith(childTablePrefix)) {
continue;
}
compositeKey.setGroup(table);
try {
// Output every row in this mock table
int rows = 0;
final Scanner scanner = childDao.getConnector().createScanner(table, childAuths);
for (final Map.Entry<Key, Value> row : scanner) {
compositeKey.setKey(row.getKey());
compositeVal.setKey(row.getKey());
compositeVal.setValue(row.getValue());
context.write(compositeKey, compositeVal);
rows++;
}
log.info("Flushed " + rows + " in-memory rows to output (" + table + ").");
// Then clear the table
if (rows > 0) {
ops.deleteRows(table, null, null);
}
} catch (TableNotFoundException | AccumuloException | AccumuloSecurityException e) {
throw new IOException("Error flushing in-memory table", e);
}
}
// All tables should be empty
cachedStatements = 0;
}
use of org.apache.accumulo.core.client.TableNotFoundException in project incubator-rya by apache.
the class PcjTables method getPcjMetadata.
/**
* Fetch the {@link PCJMetadata} from an Accumulo 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 table that will be search. (not null)
* @return The PCJ Metadata that has been stolred in the in the PCJ Table.
* @throws PCJStorageException The PCJ Table does not exist.
*/
public PcjMetadata getPcjMetadata(final Connector accumuloConn, final String pcjTableName) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
Scanner scanner = null;
try {
// Create an Accumulo scanner that iterates through the metadata entries.
scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
final Iterator<Entry<Key, Value>> entries = scanner.iterator();
// No metadata has been stored in the table yet.
if (!entries.hasNext()) {
throw new PCJStorageException("Could not find any PCJ metadata in the table named: " + pcjTableName);
}
// Fetch the metadata from the entries. Assuming they all have the same cardinality and sparql query.
String sparql = null;
Long cardinality = null;
final Set<VariableOrder> varOrders = new HashSet<>();
while (entries.hasNext()) {
final Entry<Key, Value> entry = entries.next();
final Text columnQualifier = entry.getKey().getColumnQualifier();
final byte[] value = entry.getValue().get();
if (columnQualifier.equals(PCJ_METADATA_SPARQL_QUERY)) {
sparql = stringLexicoder.decode(value);
} else if (columnQualifier.equals(PCJ_METADATA_CARDINALITY)) {
cardinality = longLexicoder.decode(value);
} else if (columnQualifier.equals(PCJ_METADATA_VARIABLE_ORDERS)) {
for (final String varOrderStr : listLexicoder.decode(value)) {
varOrders.add(new VariableOrder(varOrderStr));
}
}
}
return new PcjMetadata(sparql, cardinality, varOrders);
} catch (final TableNotFoundException e) {
throw new PCJStorageException("Could not add results to a PCJ because the PCJ table does not exist.", e);
} finally {
if (scanner != null) {
scanner.close();
}
}
}
use of org.apache.accumulo.core.client.TableNotFoundException in project incubator-rya by apache.
the class PcjTables method createPcjTable.
/**
* Create a new PCJ table within an Accumulo instance for a SPARQL query.
* For example, calling the function like this:
* <pre>
* PcjTables.createPcjTable(
* accumuloConn,
*
* "foo_INDEX_query1234",
*
* Sets.newHashSet(
* new VariableOrder("city;worker;customer"),
* new VariableOrder("worker;customer;city") ,
* new VariableOrder("customer;city;worker")),
*
* "SELECT ?customer ?worker ?city { " +
* "?customer <http://talksTo> ?worker. " +
* "?worker <http://livesIn> ?city. " +
* "?worker <http://worksAt> <http://Home>. " +
* "}");
* </pre>
* </p>
* Will result in an Accumulo table named "foo_INDEX_query1234" with the following entries:
* <table border="1" style="width:100%">
* <tr> <th>Row ID</td> <th>Column</td> <th>Value</td> </tr>
* <tr> <td>pcjMetadata</td> <td>metadata:sparql</td> <td> ... UTF-8 bytes encoding the query string ... </td> </tr>
* <tr> <td>pcjMetadata</td> <td>metadata:cardinality</td> <td> The query's cardinality </td> </tr>
* <tr> <td>pcjMetadata</td> <td>metadata:variableOrders</td> <td> The variable orders the results are written to </td> </tr>
* </table>
*
* @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
* @param pcjTableName - The name of the table that will be created. (not null)
* @param varOrders - The variable orders the results within the table will be written to. (not null)
* @param sparql - The query this table's results solves. (not null)
* @throws PCJStorageException Could not create a new PCJ table either because Accumulo
* would not let us create it or the PCJ metadata was not able to be written to it.
*/
public void createPcjTable(final Connector accumuloConn, final String pcjTableName, final Set<VariableOrder> varOrders, final String sparql) throws PCJStorageException {
checkNotNull(accumuloConn);
checkNotNull(pcjTableName);
checkNotNull(varOrders);
checkNotNull(sparql);
final TableOperations tableOps = accumuloConn.tableOperations();
if (!tableOps.exists(pcjTableName)) {
BatchWriter writer = null;
try {
// Create the new table in Accumulo.
tableOps.create(pcjTableName);
// Write the PCJ Metadata to the newly created table.
final PcjMetadata pcjMetadata = new PcjMetadata(sparql, 0L, varOrders);
final List<Mutation> mutations = makeWriteMetadataMutations(pcjMetadata);
writer = accumuloConn.createBatchWriter(pcjTableName, new BatchWriterConfig());
writer.addMutations(mutations);
} catch (final TableExistsException e) {
log.warn("Something else just created the Rya PCJ export table named '" + pcjTableName + "'. This is unexpected, but we will continue as normal.");
} catch (AccumuloException | AccumuloSecurityException | TableNotFoundException e) {
throw new PCJStorageException("Could not create a new PCJ named: " + pcjTableName, e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final MutationsRejectedException e) {
log.error("Mutations rejected while creating the PCJ table.", e);
}
}
}
}
}
Aggregations