Skip to content

Instantly share code, notes, and snippets.

@melrief
Last active July 15, 2016 18:21
Show Gist options
  • Save melrief/5f2ca248f1a980ddead2f2eeb19e6389 to your computer and use it in GitHub Desktop.
Save melrief/5f2ca248f1a980ddead2f2eeb19e6389 to your computer and use it in GitHub Desktop.
val scanner = accumulo.connector.createScanner(tableName, new Authorizations())
val iteratorSetting = new IteratorSetting(Int.MaxValue, classOf[RowCounterIterator])
scanner.addScanIterator(iteratorSetting)
val iterator = scanner.iterator()
iterator.hasNext shouldEqual true
val entry = iterator.next()
entry.getValue.get().length shouldEqual 0
println(entry)
LongByteCodec.decode(entry.getKey.getRowData.toArray) shouldEqual daySubscriberProfiles.size
scanner.close()
import org.apache.accumulo.core.data.{ Key, Value }
import org.apache.accumulo.core.iterators.{ IteratorEnvironment, SortedKeyValueIterator, WrappingIterator }
/**
* An `Iterator` that counts the element of the wrapping iterator and returns one
* key-value with the counter
*/
class RowCounterIterator extends WrappingIterator {
/** Build a `RowCounterIterator` from a `IteratorEnviroment` and another `RowCounterIterator` */
def this(env: IteratorEnvironment, that: RowCounterIterator) {
this()
setSource(that.getSource.deepCopy(env))
counter = that.counter
nextHasBeenCalled = that.nextHasBeenCalled
hasNext = that.hasNext
}
override def deepCopy(env: IteratorEnvironment): RowCounterIterator = new RowCounterIterator(env, this)
/** Number of elements found */
private[iterator] var counter = 0L
/** `true` if `next()` has been called, `false` otherwise */
private[iterator] var nextHasBeenCalled = false
/** `true` if `next()` has been called zero or one time, `false` otherwise */
private[iterator] var hasNext = true
override def hasTop(): Boolean = this.hasNext
/** @return a `Key` with the row set to this `counter` */
override def getTopKey(): Key = new Key(LongByteCodec.encode(this.counter), ByteCodec.emptyByteArray,
ByteCodec.emptyByteArray, ByteCodec.emptyByteArray, Long.MaxValue)
/** @return an empty `Value` */
override def getTopValue(): Value = new Value()
override def next(): Unit = {
if (this.nextHasBeenCalled) {
this.hasNext = false
} else {
while (super.hasTop() && !super.getTopKey.isDeleted) {
this.counter += 1L
super.next()
}
this.nextHasBeenCalled = true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment