Search in sources :

Example 6 with ScannerBase

use of org.apache.accumulo.core.client.ScannerBase in project incubator-rya by apache.

the class AccumuloTemporalIndexer method queryInstantAfterInstant.

/**
 * get statements where the date object is after the given queryInstant.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantAfterInstant(final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException {
    final Query query = new Query() {

        @Override
        public Range getRange(final KeyParts keyParts) {
            // <-- specific logic
            final Text start = Range.followingPrefix(keyParts.getQueryKey());
            // no constraints                            // <-- specific logic
            Text endAt = null;
            if (keyParts.constraintPrefix != null) {
                endAt = Range.followingPrefix(keyParts.constraintPrefix);
            }
            // System.out.println("Scanning queryInstantAfterInstant from after:" + KeyParts.toHumanString(start) + " up to:" + KeyParts.toHumanString(endAt));
            return new Range(start, true, endAt, false);
        }
    };
    final ScannerBase scanner = query.doQuery(queryInstant, constraints);
    return getContextIteratorWrapper(scanner, constraints.getContext());
}
Also used : ScannerBase(org.apache.accumulo.core.client.ScannerBase) Text(org.apache.hadoop.io.Text) KeyParts(org.apache.rya.indexing.KeyParts) Range(org.apache.accumulo.core.data.Range)

Example 7 with ScannerBase

use of org.apache.accumulo.core.client.ScannerBase in project incubator-rya by apache.

the class AccumuloRyaQueryEngine method queryWithBindingSet.

@Override
public CloseableIteration<? extends Map.Entry<RyaStatement, BindingSet>, RyaDAOException> queryWithBindingSet(Collection<Map.Entry<RyaStatement, BindingSet>> stmts, AccumuloRdfConfiguration conf) throws RyaDAOException {
    if (conf == null) {
        conf = configuration;
    }
    // query configuration
    Authorizations authorizations = conf.getAuthorizations();
    Long ttl = conf.getTtl();
    Long maxResults = conf.getLimit();
    Integer maxRanges = conf.getMaxRangesForScanner();
    Integer numThreads = conf.getNumThreads();
    // TODO: cannot span multiple tables here
    try {
        Collection<Range> ranges = new HashSet<Range>();
        RangeBindingSetEntries rangeMap = new RangeBindingSetEntries();
        TABLE_LAYOUT layout = null;
        RyaURI context = null;
        TriplePatternStrategy strategy = null;
        RyaURI columnFamily = null;
        boolean columnFamilySet = false;
        for (Map.Entry<RyaStatement, BindingSet> stmtbs : stmts) {
            RyaStatement stmt = stmtbs.getKey();
            context = stmt.getContext();
            // Scanner will fetch all ColumnFamilies.
            if (!columnFamilySet) {
                columnFamily = context;
                columnFamilySet = true;
            } else if (columnFamily != null && !columnFamily.equals(context)) {
                columnFamily = null;
            }
            BindingSet bs = stmtbs.getValue();
            strategy = ryaContext.retrieveStrategy(stmt);
            if (strategy == null) {
                throw new IllegalArgumentException("TriplePattern[" + stmt + "] not supported");
            }
            Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), stmt.getContext(), conf);
            // use range to set scanner
            // populate scanner based on authorizations, ttl
            layout = entry.getKey();
            ByteRange byteRange = entry.getValue();
            Range range = new Range(new Text(byteRange.getStart()), new Text(byteRange.getEnd()));
            Range rangeMapRange = range;
            // as the Value specified in the BindingSet
            if (context != null) {
                byte[] contextBytes = context.getData().getBytes("UTF-8");
                rangeMapRange = range.bound(new Column(contextBytes, new byte[] { (byte) 0x00 }, new byte[] { (byte) 0x00 }), new Column(contextBytes, new byte[] { (byte) 0xff }, new byte[] { (byte) 0xff }));
            }
            // ranges gets a Range that has no Column bounds, but
            // rangeMap gets a Range that does have Column bounds
            // If we inserted multiple Ranges with the same Row (but
            // distinct Column bounds) into the Set ranges, we would get
            // duplicate
            // results when the Row is not exact. So RyaStatements that
            // differ only in their context are all mapped to the same
            // Range (with no Column bounds) for scanning purposes.
            // However, context information is included in a Column that
            // bounds the Range inserted into rangeMap. This is because
            // in the class {@link RyaStatementBindingSetKeyValueIterator},
            // the rangeMap is
            // used to join the scan results with the BindingSets to produce
            // the query results. The additional ColumnFamily info is
            // required in this join
            // process to allow for the Statement contexts to be compared
            // with the BindingSet contexts
            // See {@link RangeBindingSetEntries#containsKey}.
            ranges.add(range);
            rangeMap.put(rangeMapRange, bs);
        }
        // no ranges. if strategy alone is null, it would be thrown in the loop above.
        if (layout == null || strategy == null) {
            return null;
        }
        String regexSubject = conf.getRegexSubject();
        String regexPredicate = conf.getRegexPredicate();
        String regexObject = conf.getRegexObject();
        TripleRowRegex tripleRowRegex = strategy.buildRegex(regexSubject, regexPredicate, regexObject, null, null);
        String table = layoutToTable(layout, conf);
        boolean useBatchScanner = ranges.size() > maxRanges;
        RyaStatementBindingSetKeyValueIterator iterator = null;
        if (useBatchScanner) {
            ScannerBase scanner = connector.createBatchScanner(table, authorizations, numThreads);
            ((BatchScanner) scanner).setRanges(ranges);
            fillScanner(scanner, columnFamily, null, ttl, null, tripleRowRegex, conf);
            iterator = new RyaStatementBindingSetKeyValueIterator(layout, ryaContext, scanner, rangeMap);
        } else {
            Scanner scannerBase = null;
            Iterator<Map.Entry<Key, Value>>[] iters = new Iterator[ranges.size()];
            int i = 0;
            for (Range range : ranges) {
                scannerBase = connector.createScanner(table, authorizations);
                scannerBase.setRange(range);
                fillScanner(scannerBase, columnFamily, null, ttl, null, tripleRowRegex, conf);
                iters[i] = scannerBase.iterator();
                i++;
            }
            iterator = new RyaStatementBindingSetKeyValueIterator(layout, Iterators.concat(iters), rangeMap, ryaContext);
        }
        if (maxResults != null) {
            iterator.setMaxResults(maxResults);
        }
        return iterator;
    } catch (Exception e) {
        throw new RyaDAOException(e);
    }
}
Also used : BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) ByteRange(org.apache.rya.api.query.strategy.ByteRange) BatchScanner(org.apache.accumulo.core.client.BatchScanner) RyaStatement(org.apache.rya.api.domain.RyaStatement) Column(org.apache.accumulo.core.data.Column) TripleRowRegex(org.apache.rya.api.resolver.triple.TripleRowRegex) Iterator(java.util.Iterator) HashSet(java.util.HashSet) BindingSet(org.openrdf.query.BindingSet) Authorizations(org.apache.accumulo.core.security.Authorizations) TriplePatternStrategy(org.apache.rya.api.query.strategy.TriplePatternStrategy) ScannerBase(org.apache.accumulo.core.client.ScannerBase) Text(org.apache.hadoop.io.Text) RyaRange(org.apache.rya.api.domain.RyaRange) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) IOException(java.io.IOException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.apache.accumulo.core.data.Value) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.apache.accumulo.core.data.Key)

Example 8 with ScannerBase

use of org.apache.accumulo.core.client.ScannerBase in project vertexium by visallo.

the class OverflowIntoHdfsStreamingPropertyValueStorageStrategy method streamingPropertyValueTableDatas.

private Map<String, byte[]> streamingPropertyValueTableDatas(List<String> dataRowKeys) {
    try {
        if (dataRowKeys.size() == 0) {
            return Collections.emptyMap();
        }
        List<org.apache.accumulo.core.data.Range> ranges = dataRowKeys.stream().map(RangeUtils::createRangeFromString).collect(Collectors.toList());
        final long timerStartTime = System.currentTimeMillis();
        ScannerBase scanner = graph.createBatchScanner(graph.getDataTableName(), ranges, new org.apache.accumulo.core.security.Authorizations());
        graph.getGraphLogger().logStartIterator(scanner);
        Span trace = Trace.start("streamingPropertyValueTableData");
        trace.data("dataRowKeyCount", Integer.toString(dataRowKeys.size()));
        try {
            Map<String, byte[]> results = new HashMap<>();
            for (Map.Entry<Key, Value> col : scanner) {
                results.put(col.getKey().getRow().toString(), col.getValue().get());
            }
            return results;
        } finally {
            scanner.close();
            trace.stop();
            graph.getGraphLogger().logEndIterator(System.currentTimeMillis() - timerStartTime);
        }
    } catch (Exception ex) {
        throw new VertexiumException(ex);
    }
}
Also used : HashMap(java.util.HashMap) ScannerBase(org.apache.accumulo.core.client.ScannerBase) Span(org.apache.accumulo.core.trace.Span) VertexiumException(org.vertexium.VertexiumException) IOException(java.io.IOException) VertexiumException(org.vertexium.VertexiumException) org.vertexium.accumulo(org.vertexium.accumulo) Value(org.apache.accumulo.core.data.Value) StreamingPropertyValue(org.vertexium.property.StreamingPropertyValue) HashMap(java.util.HashMap) Map(java.util.Map) DataTableRowKey(org.vertexium.accumulo.keys.DataTableRowKey) Key(org.apache.accumulo.core.data.Key)

Example 9 with ScannerBase

use of org.apache.accumulo.core.client.ScannerBase in project vertexium by visallo.

the class StreamingPropertyValueTable method streamingPropertyValueTableData.

public byte[] streamingPropertyValueTableData(String dataRowKey, Long timestamp) {
    try {
        List<Range> ranges = Lists.newArrayList(RangeUtils.createRangeFromString(dataRowKey));
        long timerStartTime = System.currentTimeMillis();
        ScannerBase scanner = graph.createBatchScanner(graph.getDataTableName(), ranges, new org.apache.accumulo.core.security.Authorizations());
        if (timestamp != null && !DataTableRowKey.isLegacy(dataRowKey)) {
            IteratorSetting iteratorSetting = new IteratorSetting(80, TimestampFilter.class.getSimpleName(), TimestampFilter.class);
            TimestampFilter.setStart(iteratorSetting, timestamp, true);
            TimestampFilter.setEnd(iteratorSetting, timestamp, true);
            scanner.addScanIterator(iteratorSetting);
        }
        GRAPH_LOGGER.logStartIterator(scanner);
        Span trace = Trace.start("streamingPropertyValueTableData");
        trace.data("dataRowKeyCount", Integer.toString(1));
        try {
            byte[] result = null;
            for (Map.Entry<Key, Value> col : scanner) {
                String foundKey = col.getKey().getRow().toString();
                byte[] value = col.getValue().get();
                if (foundKey.equals(dataRowKey)) {
                    result = value;
                }
            }
            if (result == null) {
                throw new VertexiumException("Could not find data with key: " + dataRowKey);
            }
            return result;
        } finally {
            scanner.close();
            trace.stop();
            GRAPH_LOGGER.logEndIterator(System.currentTimeMillis() - timerStartTime);
        }
    } catch (Exception ex) {
        throw new VertexiumException(ex);
    }
}
Also used : TimestampFilter(org.apache.accumulo.core.iterators.user.TimestampFilter) ScannerBase(org.apache.accumulo.core.client.ScannerBase) Range(org.apache.accumulo.core.data.Range) Span(org.apache.accumulo.core.trace.Span) VertexiumException(org.vertexium.VertexiumException) VertexiumException(org.vertexium.VertexiumException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) StreamingPropertyValue(org.vertexium.property.StreamingPropertyValue) Map(java.util.Map) DataTableRowKey(org.vertexium.accumulo.keys.DataTableRowKey) Key(org.apache.accumulo.core.data.Key)

Example 10 with ScannerBase

use of org.apache.accumulo.core.client.ScannerBase in project incubator-rya by apache.

the class AccumuloTemporalIndexer method queryInstantEqualsInstant.

/**
 * statements where the datetime is exactly the same as the queryInstant.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantEqualsInstant(final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException {
    // get rows where the repository time is equal to the given time in queryInstant.
    final Query query = new Query() {

        @Override
        public Range getRange(final KeyParts keyParts) {
            // <-- specific logic
            return Range.prefix(keyParts.getQueryKey());
        }
    };
    final ScannerBase scanner = query.doQuery(queryInstant, constraints);
    // TODO currently context constraints are filtered on the client.
    return getContextIteratorWrapper(scanner, constraints.getContext());
}
Also used : ScannerBase(org.apache.accumulo.core.client.ScannerBase) KeyParts(org.apache.rya.indexing.KeyParts)

Aggregations

ScannerBase (org.apache.accumulo.core.client.ScannerBase)12 Key (org.apache.accumulo.core.data.Key)8 Value (org.apache.accumulo.core.data.Value)8 Range (org.apache.accumulo.core.data.Range)7 Text (org.apache.hadoop.io.Text)4 KeyParts (org.apache.rya.indexing.KeyParts)4 IOException (java.io.IOException)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 BatchScanner (org.apache.accumulo.core.client.BatchScanner)3 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)3 Scanner (org.apache.accumulo.core.client.Scanner)3 Span (org.apache.accumulo.core.trace.Span)3 HashMap (java.util.HashMap)2 BatchWriter (org.apache.accumulo.core.client.BatchWriter)2 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)2 ClientSideIteratorScanner (org.apache.accumulo.core.client.ClientSideIteratorScanner)2 Connector (org.apache.accumulo.core.client.Connector)2 IsolatedScanner (org.apache.accumulo.core.client.IsolatedScanner)2 SampleNotPresentException (org.apache.accumulo.core.client.SampleNotPresentException)2