Pipe in your stream of buffers/strings to get an approximate cardinality (using HyperLogLog).
var Hll = require('hll-stream');
var es = require('event-stream');
var fs = require('fs');
var hll = new Hll(8);
fs.createReadStream('data.txt')
.pipe(es.split())
.pipe(hll);
hll.on('finish', function() {
console.log(hll.cardinality());
});
hll-stream uses 32-bit hash values since node.js won't currently do bit-wise arithmetic on more than 32 bits.
Construct a new writable Hll (extends Stream.Writable
).
precision
- number of bits of precision (4-16) (default 4). Memory usage is on the order of 2precision.hashType
- which hashing algorithm to use on the values. Can be any algorithm supported bycrypto.createHash
(default:'sha1'
).streamOpts
- the options to pass along to the stream constructor.
Compute the approximate cardinality.
Merge another Hll with this one. The two Hlls must have the same hashType
and precision
. Returns a new Hll.
hll
- another instance ofhll-stream
to merge with this one.
Export the Hll's data. Returns an object like:
{
precision: 8,
hashType: 'sha1',
registers: [...]
}
Import Hll data (as exported by export()
). Replaces any pre-existing data.
data
- the data object to import. Should be in the same format as exported byexport()
.