下面列出了怎么用org.apache.hadoop.util.bloom.Filter的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
public Boolean evaluate(@Nullable Text bloomStr, @Nullable List<Text> keys)
throws HiveException {
if (bloomStr == null) {
return null;
}
if (keys == null) {
return Boolean.FALSE;
}
final Filter bloom = getFilter(bloomStr);
for (Text keyStr : keys) {
if (keyStr == null) {
continue;
}
key.set(keyStr.copyBytes(), 1.0d);
if (bloom.membershipTest(key) == false) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
@Nonnull
private Filter getFilter(@Nonnull final Text bloomStr) throws HiveException {
final Filter bloom;
if (prevBf != null && prevBfStr.equals(bloomStr)) {
bloom = prevBf;
} else {
try {
bloom = BloomFilterUtils.deserialize(bloomStr, new DynamicBloomFilter());
} catch (IOException e) {
throw new HiveException(e);
}
this.prevBfStr = new Text(bloomStr);
this.prevBf = bloom;
}
return bloom;
}
@Nullable
public Boolean evaluate(@Nullable Text bloomStr, @Nullable List<Text> keys)
throws HiveException {
if (bloomStr == null) {
return null;
}
if (keys == null) {
return Boolean.FALSE;
}
final Filter bloom = getFilter(bloomStr);
for (Text keyStr : keys) {
if (keyStr == null) {
continue;
}
key.set(keyStr.copyBytes(), 1.0d);
if (bloom.membershipTest(key)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
@Nonnull
private Filter getFilter(@Nonnull final Text bloomStr) throws HiveException {
final Filter bloom;
if (prevBf != null && prevBfStr.equals(bloomStr)) {
bloom = prevBf;
} else {
try {
bloom = BloomFilterUtils.deserialize(bloomStr, new DynamicBloomFilter());
} catch (IOException e) {
throw new HiveException(e);
}
this.prevBfStr = new Text(bloomStr);
this.prevBf = bloom;
}
return bloom;
}
@Nonnull
public static byte[] serialize(@Nonnull final Filter filter) throws IOException {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
Base91OutputStream base91 = new Base91OutputStream(bos);
DataOutputStream out = new DataOutputStream(base91);
filter.write(out);
out.flush();
base91.finish();
return bos.toByteArray();
}
@Nonnull
public static Text serialize(@Nonnull final Filter filter, @Nonnull final Text dst)
throws IOException {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
Base91OutputStream base91 = new Base91OutputStream(bos);
DataOutputStream out = new DataOutputStream(base91);
filter.write(out);
out.flush();
base91.finish();
dst.set(bos.getInternalArray(), 0, bos.size());
return dst;
}
@Nonnull
public static <F extends Filter> F deserialize(@Nonnull final byte[] buf,
@Nonnegative final int offset, @Nonnegative final int len, @Nonnull final F dst)
throws IOException {
FastByteArrayInputStream fis = new FastByteArrayInputStream(buf, offset, len);
DataInput in = new DataInputStream(new Base91InputStream(fis));
dst.readFields(in);
return dst;
}
@Nullable
public Boolean evaluate(@Nullable Text bloomStr, @Nullable Text keyStr) throws HiveException {
if (bloomStr == null) {
return null;
}
if (keyStr == null) {
return Boolean.FALSE;
}
Filter bloom = getFilter(bloomStr);
key.set(keyStr.copyBytes(), 1.0d);
return Boolean.valueOf(bloom.membershipTest(key));
}
private static void assertEquals(@Nonnull Filter expected, @Nonnull Filter actual, long seed,
int size) {
final Key key = new Key();
final Random rnd1 = new Random(seed);
for (int i = 0; i < size; i++) {
double d = rnd1.nextGaussian();
String s = Double.toHexString(d);
key.set(s.getBytes(), 1.0);
Assert.assertEquals(expected.membershipTest(key), actual.membershipTest(key));
}
}
private static void assertNotContains(@Nonnull Filter expected, @Nonnull Filter actual,
long seed, int size) {
final Key key = new Key();
final Random rnd1 = new Random(seed);
for (int i = 0; i < size; i++) {
double d = rnd1.nextGaussian();
String s = Double.toHexString(d);
key.set(s.getBytes(), 1.0);
Assert.assertEquals(expected.membershipTest(key), actual.membershipTest(key));
}
}
@Nonnull
public static <F extends Filter> F deserialize(@Nonnull final Text in, @Nonnull final F dst)
throws IOException {
return deserialize(in.getBytes(), 0, in.getLength(), dst);
}
@Nonnull
public static <F extends Filter> F deserialize(@Nonnull final byte[] buf, @Nonnull final F dst)
throws IOException {
return deserialize(buf, 0, buf.length, dst);
}
/**
* Retrieve the Bloom filter used by this instance of the Reader.
* @return a Bloom filter (see {@link Filter})
*/
public Filter getBloomFilter() {
return bloomFilter;
}
/**
* Retrieve the Bloom filter used by this instance of the Reader.
* @return a Bloom filter (see {@link Filter})
*/
public Filter getBloomFilter() {
return bloomFilter;
}
/**
* Retrieve the Bloom filter used by this instance of the Reader.
* @return a Bloom filter (see {@link Filter})
*/
public Filter getBloomFilter() {
return bloomFilter;
}
/**
* Retrieve the Bloom filter used by this instance of the Reader.
* @return a Bloom filter (see {@link Filter})
*/
public Filter getBloomFilter() {
return bloomFilter;
}