Skip to content

Instantly share code, notes, and snippets.

@subintp
Created August 24, 2020 10:19
Show Gist options
  • Save subintp/e820e2a7b51090fe38d610e5820075db to your computer and use it in GitHub Desktop.
Save subintp/e820e2a7b51090fe38d610e5820075db to your computer and use it in GitHub Desktop.
import com.twitter.algebird.{
HLL,
HyperLogLog,
HyperLogLogAggregator,
HyperLogLogMonoid
}
object HLL extends App {
val hllMonoid = new HyperLogLogMonoid(bits = 4)
val agg = HyperLogLogAggregator.withErrorGeneric[String](0.01)
val data = List("hello", "world", "wowww")
val combinedHll: HLL = agg(data)
println(s"old size: ${combinedHll.estimatedSize}")
// HLL to bytes
val hbytes = HyperLogLog.toBytes(combinedHll)
// Bytes to HLL
val newHll = HyperLogLog.fromBytes(hbytes)
// combine HLLS
val sum = hllMonoid.sum(Seq(combinedHll, newHll))
println(s"new size: ${newHll.estimatedSize}")
println(s"combined size: ${sum.estimatedSize}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment