org.apache.accumulo.core.util.PeekingIterator Java Examples

The following examples show how to use org.apache.accumulo.core.util.PeekingIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccumuloItem.java    From cognition with Apache License 2.0 6 votes vote down vote up
/**
 * Using the provided PeekingIterator, construct an AccumuloItem from the Key-Value pairs it returns.  If skipBadRow
 * is true, this method will skip rows not containing AccumuloItems until there are now more Key-Value pairs or an
 * Accumulo Item is found, otherwise the method will return null;
 *
 * @param iter
 * @param skipBadRow
 * @return
 */
public static AccumuloItem buildFromIterator(PeekingIterator<Entry<Key, Value>> iter, boolean skipBadRow) {
  while (iter.hasNext()) {
    Entry<Key, Value> pair = iter.peek();
    AccumuloItem i = constructItemFromFirstPair(pair.getKey(), pair.getValue());
    if (i != null) {
      iter.next();
      i.populateFromRow(iter);
      return i;
    } else if (skipBadRow) {
      buildRowMap(iter);
    } else {
      break;
    }
  }
  return null;
}
 
Example #2
Source File: BaseIndexValuesTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
/**
 * Get elements with the key/value pair.
 * @param key
 * @param value
 * @return
 */
@SuppressWarnings("unchecked")
public <T extends Element> CloseableIterable<T> readElementsFromIndex(String key, Object value) {
  Scanner scan = getScanner();
  byte[] id = AccumuloByteSerializer.serialize(value);
  scan.setRange(Range.exact(new Text(id)));
  scan.fetchColumnFamily(new Text(key));

  final ElementIndexParser<? extends AccumuloElement> parser =
      Vertex.class.equals(elementType) ? new VertexIndexParser(globals) :
        new EdgeIndexParser(globals);

      return new ScannerIterable<T>(scan) {
        @Override
        public T next(PeekingIterator<Entry<Key,Value>> iterator) {
          return (T) parser.parse(Arrays.asList(iterator.next()));
        }      
      };
}
 
Example #3
Source File: AccumuloItem.java    From cognition with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls the Key-Value pairs representing the next row from the supplied <code>PeekingIterator</code> and returns
 * them as a <code>SortedMap</code>
 *
 * @param row
 * @return
 */
public static SortedMap<Key, Value> buildRowMap(PeekingIterator<Entry<Key, Value>> row) {
  TreeMap<Key, Value> aggregatedRow = new TreeMap<>();
  Text rowid = null;
  while (row.hasNext() && (rowid == null || rowid.equals(row.peek().getKey().getRow()))) {
    Entry<Key, Value> entry = row.next();
    if (rowid == null) {
      rowid = entry.getKey().getRow();
    }
    aggregatedRow.put(entry.getKey(), entry.getValue());
  }
  return aggregatedRow;
}
 
Example #4
Source File: EdgeTableWrapper.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
public CloseableIterable<Edge> getEdges() {
  Scanner scan = getScanner();
  scan.fetchColumnFamily(new Text(Constants.LABEL));

  if (globals.getConfig().getPreloadedProperties() != null) {
    for (String key : globals.getConfig().getPreloadedProperties()) {
      scan.fetchColumnFamily(new Text(key));
    }
  }

  final EdgeParser parser = new EdgeParser(globals);

  return new ScannerIterable<Edge>(scan) {
    @Override
    public Edge next(PeekingIterator<Entry<Key, Value>> iterator) {
      // TODO could also check local cache before creating a new instance?

      String rowId = iterator.peek().getKey().getRow().toString();

      List<Entry<Key, Value>> entries =
          new ArrayList<Entry<Key, Value>>();

      // MDL 05 Jan 2014:  Why is this equalsIgnoreCase??
      while (iterator.peek() != null && rowId.equalsIgnoreCase(iterator
          .peek().getKey().getRow().toString())) {
        entries.add(iterator.next());
      }

      AccumuloEdge edge = parser.parse(rowId, entries);
      globals.getCaches().cache(edge, Edge.class);

      return edge;
    }
  };
}
 
Example #5
Source File: EdgeTableWrapper.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
public Iterable<Edge> getEdges(String key, Object value) {
  AccumuloGraphUtils.nullCheckProperty(key, value);
  if (key.equalsIgnoreCase("label")) {
    key = Constants.LABEL;
  }
  
  BatchScanner scan = getBatchScanner();
  scan.fetchColumnFamily(new Text(key));

  byte[] val = AccumuloByteSerializer.serialize(value);
  if (val[0] != AccumuloByteSerializer.SERIALIZABLE) {
    IteratorSetting is = new IteratorSetting(10, "filter", RegExFilter.class);
    RegExFilter.setRegexs(is, null, null, null, Pattern.quote(new String(val)), false);
    scan.addScanIterator(is);

    return new ScannerIterable<Edge>(scan) {

      @Override
      public Edge next(PeekingIterator<Entry<Key,Value>> iterator) {

        Key k = iterator.next().getKey();

        if (k.getColumnFamily().toString().equals(Constants.LABEL)) {
          String[] vals = k.getColumnQualifier().toString().split(Constants.ID_DELIM);
          return new AccumuloEdge(globals, k.getRow().toString(),
              new AccumuloVertex(globals, vals[0]),
              new AccumuloVertex(globals, vals[1]), null);
        }
        return new AccumuloEdge(globals, k.getRow().toString());
      }
    };
  } else {
    // TODO
    throw new UnsupportedOperationException("Filtering on binary data not currently supported.");
  }
}
 
Example #6
Source File: VertexTableWrapper.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
public CloseableIterable<Vertex> getVerticesInRange(Object fromId, Object toId) {
  Scanner scan = getScanner();
  scan.setRange(new Range(fromId != null ? fromId.toString() : null,
      toId != null ? toId.toString() : null));
  scan.fetchColumnFamily(new Text(Constants.LABEL));

  if (globals.getConfig().getPreloadedProperties() != null) {
    for (String key : globals.getConfig().getPreloadedProperties()) {
      scan.fetchColumnFamily(new Text(key));
    }
  }

  final VertexParser parser = new VertexParser(globals);

  return new ScannerIterable<Vertex>(scan) {
    @Override
    public Vertex next(PeekingIterator<Entry<Key, Value>> iterator) {
      // TODO could also check local cache before creating a new instance?

      String rowId = iterator.peek().getKey().getRow().toString();

      List<Entry<Key, Value>> entries =
          new ArrayList<Entry<Key, Value>>();

      while (iterator.peek() != null && rowId.equals(iterator
          .peek().getKey().getRow().toString())) {
        entries.add(iterator.next());
      }

      AccumuloVertex vertex = parser.parse(rowId, entries);
      globals.getCaches().cache(vertex, Vertex.class);

      return vertex;
    }
  };
}
 
Example #7
Source File: ShardLimitingIterator.java    From datawave with Apache License 2.0 4 votes vote down vote up
public ShardLimitingIterator(Iterator<Entry<Key,Value>> kvIter, int maxShardsPerDay) {
    this.kvIter = new PeekingIterator<>(kvIter);
    this.maxShardsPerDay = maxShardsPerDay;
    currentQueue = Queues.newArrayDeque();
}
 
Example #8
Source File: ScannerIterable.java    From AccumuloGraph with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<T> iterator() {
  return new ScannerIterator(new PeekingIterator<Entry<Key,Value>>(scanner.iterator()));
}
 
Example #9
Source File: ScannerIterable.java    From AccumuloGraph with Apache License 2.0 4 votes vote down vote up
private ScannerIterator(PeekingIterator<Entry<Key,Value>> iterator) {
  this.iterator = iterator;
}
 
Example #10
Source File: VertexTableWrapper.java    From AccumuloGraph with Apache License 2.0 4 votes vote down vote up
public CloseableIterable<Edge> getEdges(Vertex vertex, Direction direction,
    String... labels) {
  Scanner scan = getScanner();
  scan.setRange(new Range(vertex.getId().toString()));
  if (direction.equals(Direction.IN)) {
    scan.fetchColumnFamily(new Text(Constants.IN_EDGE));
  } else if (direction.equals(Direction.OUT)) {
    scan.fetchColumnFamily(new Text(Constants.OUT_EDGE));
  } else {
    scan.fetchColumnFamily(new Text(Constants.IN_EDGE));
    scan.fetchColumnFamily(new Text(Constants.OUT_EDGE));
  }

  if (labels.length > 0) {
    applyEdgeLabelValueFilter(scan, labels);
  }

  return new ScannerIterable<Edge>(scan) {

    @Override
    public Edge next(PeekingIterator<Entry<Key,Value>> iterator) {
      // TODO better use of information readily available...
      // TODO could also check local cache before creating a new
      // instance?

      Entry<Key,Value> kv = iterator.next();

      String[] parts = kv.getKey().getColumnQualifier().toString().split(Constants.ID_DELIM);
      String label = (new String(kv.getValue().get())).split(Constants.ID_DELIM)[1];

      AccumuloEdge edge;
      if (kv.getKey().getColumnFamily().toString().equalsIgnoreCase(Constants.IN_EDGE)) {
        edge = new AccumuloEdge(globals, parts[1],
            new AccumuloVertex(globals, kv.getKey().getRow().toString()),
            new AccumuloVertex(globals, parts[0]), label);
      } else {
        edge = new AccumuloEdge(globals, parts[1],
            new AccumuloVertex(globals, parts[0]),
            new AccumuloVertex(globals, kv.getKey().getRow().toString()), label);
      }
      globals.getCaches().cache(edge, Edge.class);

      return edge;
    }
  };
}
 
Example #11
Source File: AccumuloItem.java    From cognition with Apache License 2.0 2 votes vote down vote up
/**
 * Populate the contents of this item using the supplied <code>PeekingIterator</code>, only consuming the next row
 * from the iterator.
 *
 * @param row
 */
public void populateFromRow(PeekingIterator<Entry<Key, Value>> row) {
  populateFromRow(buildRowMap(row));
}
 
Example #12
Source File: AccumuloItem.java    From cognition with Apache License 2.0 2 votes vote down vote up
/**
 * Builds an AccumuloItem from the give PeekingIterator, skipping rows until none remain or an AccumuloItem is
 * found.
 *
 * @param iter
 * @return
 */
public static AccumuloItem buildFromIterator(PeekingIterator<Entry<Key, Value>> iter) {
  return buildFromIterator(iter, true);
}
 
Example #13
Source File: ScannerIterable.java    From AccumuloGraph with Apache License 2.0 votes vote down vote up
public abstract T next(PeekingIterator<Entry<Key,Value>> iterator);