A CMS instance, which is used for the counting and the frequency estimation performed by this class.
A threshold for finding heavy hitters, i.e., elements that appear at least (heavyHittersPct * totalCount) times in the stream.
Creates a sketch out of multiple items.
Creates a sketch out of a single item.
Combines the two sketches.
Combines the two sketches.
The sketches must use the same hash functions.
override this if there is a faster way to do this sum than reduceLeftOption on plus
override this if there is a faster way to do this sum than reduceLeftOption on plus
Monoid for Top-% based TopCMS sketches.
Usage
The type
K
is the type of items you want to count. You must provide an implicitCMSHasher[K]
forK
, and Algebird ships with several such implicits for commonly used types such as Long and BigInt:import com.twitter.algebird.CMSHasherImplicits._
If your type
K
is not supported out of the box, you have two options: 1) You provide a "translation" function to convert items of your (unsupported) typeK
to a supported type such as Double, and then use thecontramap
function of CMSHasher to create the requiredCMSHasher[K]
for your type (see the documentation of CMSHasher for an example); 2) You implement aCMSHasher[K]
from scratch, using the existing CMSHasher implementations as a starting point.Note: Because Arrays in Scala/Java not have sane
equals
andhashCode
implementations, you cannot safely use types such asArray[Byte]
. Extra work is required for Arrays. For example, you may opt to convertArray[T]
to aSeq[T]
viatoSeq
, or you can provide appropriate wrapper classes. Algebird provides one such wrapper class, Bytes, to safely wrap anArray[Byte]
for use with CMS.The type used to identify the elements to be counted. For example, if you want to count the occurrence of user names, you could map each username to a unique numeric ID expressed as a
Long
, and then count the occurrences of thoseLong
s with a CMS of typeK=Long
. Note that this mapping between the elements of your problem domain and their identifiers used for counting via CMS should be bijective. We require a CMSHasher context bound forK
, see CMSHasherImplicits for available implicits that can be imported. Which type K should you pick in practice? For domains that have less than2^64
unique elements, you'd typically use Long. For larger domains you can try BigInt, for example.