Search in sources :

Example 16 with TableNotFoundException

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;
}
Also used : Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) RdfDAOException(org.apache.rya.api.persist.RdfDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) Value(org.apache.accumulo.core.data.Value) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with TableNotFoundException

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;
}
Also used : TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) IndexEntry(org.apache.rya.prospector.domain.IndexEntry) RdfDAOException(org.apache.rya.api.persist.RdfDAOException)

Example 18 with TableNotFoundException

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;
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) SecurityOperations(org.apache.accumulo.core.client.admin.SecurityOperations) IOException(java.io.IOException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) TableOperations(org.apache.accumulo.core.client.admin.TableOperations) Value(org.apache.accumulo.core.data.Value) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Map(java.util.Map) Key(org.apache.accumulo.core.data.Key)

Example 19 with TableNotFoundException

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();
        }
    }
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) Text(org.apache.hadoop.io.Text) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet)

Example 20 with TableNotFoundException

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 &lt;http://talksTo> ?worker. " +
 *            "?worker &lt;http://livesIn> ?city. " +
 *            "?worker &lt;http://worksAt> &lt;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);
                }
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) TableOperations(org.apache.accumulo.core.client.admin.TableOperations) TableExistsException(org.apache.accumulo.core.client.TableExistsException) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Aggregations

TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)160 AccumuloException (org.apache.accumulo.core.client.AccumuloException)100 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)89 Text (org.apache.hadoop.io.Text)51 Value (org.apache.accumulo.core.data.Value)46 Key (org.apache.accumulo.core.data.Key)42 Scanner (org.apache.accumulo.core.client.Scanner)36 IOException (java.io.IOException)34 BatchWriter (org.apache.accumulo.core.client.BatchWriter)31 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)31 Connector (org.apache.accumulo.core.client.Connector)30 Mutation (org.apache.accumulo.core.data.Mutation)29 Range (org.apache.accumulo.core.data.Range)28 Authorizations (org.apache.accumulo.core.security.Authorizations)26 ArrayList (java.util.ArrayList)25 Entry (java.util.Map.Entry)25 TableExistsException (org.apache.accumulo.core.client.TableExistsException)25 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)23 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)23 HashMap (java.util.HashMap)19