|
1 |
| -package mrdp.appendixA; |
2 |
| - |
3 |
| -import java.io.BufferedReader; |
4 |
| -import java.io.InputStreamReader; |
5 |
| -import java.util.zip.GZIPInputStream; |
6 |
| - |
7 |
| -import org.apache.hadoop.conf.Configuration; |
8 |
| -import org.apache.hadoop.fs.FSDataOutputStream; |
9 |
| -import org.apache.hadoop.fs.FileStatus; |
10 |
| -import org.apache.hadoop.fs.FileSystem; |
11 |
| -import org.apache.hadoop.fs.Path; |
12 |
| -import org.apache.hadoop.util.GenericOptionsParser; |
13 |
| -import org.apache.hadoop.util.bloom.BloomFilter; |
14 |
| -import org.apache.hadoop.util.bloom.Key; |
15 |
| -import org.apache.hadoop.util.hash.Hash; |
16 |
| - |
17 |
| -public class BloomFilterDriver { |
18 |
| - |
19 |
| - public static void main(String[] args) throws Exception { |
20 |
| - Configuration conf = new Configuration(); |
21 |
| - String[] otherArgs = new GenericOptionsParser(conf, args) |
22 |
| - .getRemainingArgs(); |
23 |
| - if (otherArgs.length != 4) { |
24 |
| - System.err |
25 |
| - .println("Usage: BloomFilterWriter <inputfile> <nummembers> <falseposrate> <bfoutfile>"); |
26 |
| - System.exit(1); |
27 |
| - } |
28 |
| - |
29 |
| - FileSystem fs = FileSystem.get(new Configuration()); |
30 |
| - |
31 |
| - // Parse command line arguments |
32 |
| - Path inputFile = new Path(otherArgs[0]); |
33 |
| - int numMembers = Integer.parseInt(otherArgs[1]); |
34 |
| - float falsePosRate = Float.parseFloat(otherArgs[2]); |
35 |
| - Path bfFile = new Path(otherArgs[3]); |
36 |
| - |
37 |
| - // Calculate our vector size and optimal K value based on approximations |
38 |
| - int vectorSize = getOptimalBloomFilterSize(numMembers, falsePosRate); |
39 |
| - int nbHash = getOptimalK(numMembers, vectorSize); |
40 |
| - |
41 |
| - // create new Bloom filter |
42 |
| - BloomFilter filter = new BloomFilter(vectorSize, nbHash, |
43 |
| - Hash.MURMUR_HASH); |
44 |
| - |
45 |
| - // Open file for read |
46 |
| - |
47 |
| - System.out.println("Training Bloom filter of size " + vectorSize |
48 |
| - + " with " + nbHash + " hash functions, " + numMembers |
49 |
| - + " approximate number of records, and " + falsePosRate |
50 |
| - + " false positive rate"); |
51 |
| - |
52 |
| - String line = null; |
53 |
| - int numRecords = 0; |
54 |
| - for (FileStatus status : fs.listStatus(inputFile)) { |
55 |
| - BufferedReader rdr; |
56 |
| - // if file is gzipped, wrap it in a GZIPInputStream |
57 |
| - if (status.getPath().getName().endsWith(".gz")) { |
58 |
| - rdr = new BufferedReader(new InputStreamReader( |
59 |
| - new GZIPInputStream(fs.open(status.getPath())))); |
60 |
| - } else { |
61 |
| - rdr = new BufferedReader(new InputStreamReader(fs.open(status |
62 |
| - .getPath()))); |
63 |
| - } |
64 |
| - |
65 |
| - System.out.println("Reading " + status.getPath()); |
66 |
| - while ((line = rdr.readLine()) != null) { |
67 |
| - filter.add(new Key(line.getBytes())); |
68 |
| - ++numRecords; |
69 |
| - } |
70 |
| - |
71 |
| - rdr.close(); |
72 |
| - } |
73 |
| - |
74 |
| - System.out.println("Trained Bloom filter with " + numRecords |
75 |
| - + " entries."); |
76 |
| - |
77 |
| - System.out.println("Serializing Bloom filter to HDFS at " + bfFile); |
78 |
| - FSDataOutputStream strm = fs.create(bfFile); |
79 |
| - filter.write(strm); |
80 |
| - |
81 |
| - strm.flush(); |
82 |
| - strm.close(); |
83 |
| - |
84 |
| - System.out.println("Done training Bloom filter."); |
85 |
| - } |
86 |
| - |
87 |
| - public static int getOptimalBloomFilterSize(int numRecords, |
88 |
| - float falsePosRate) { |
89 |
| - int size = (int) (-numRecords * (float) Math.log(falsePosRate) / Math |
90 |
| - .pow(Math.log(2), 2)); |
91 |
| - return size; |
92 |
| - } |
93 |
| - |
94 |
| - public static int getOptimalK(float numMembers, float vectorSize) { |
95 |
| - return (int) Math.round(vectorSize / numMembers * Math.log(2)); |
96 |
| - } |
97 |
| -} |
| 1 | +package mrdp.appendixA; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.zip.GZIPInputStream; |
| 6 | + |
| 7 | +import org.apache.hadoop.conf.Configuration; |
| 8 | +import org.apache.hadoop.fs.FSDataOutputStream; |
| 9 | +import org.apache.hadoop.fs.FileStatus; |
| 10 | +import org.apache.hadoop.fs.FileSystem; |
| 11 | +import org.apache.hadoop.fs.Path; |
| 12 | +import org.apache.hadoop.util.GenericOptionsParser; |
| 13 | +import org.apache.hadoop.util.bloom.BloomFilter; |
| 14 | +import org.apache.hadoop.util.bloom.Key; |
| 15 | +import org.apache.hadoop.util.hash.Hash; |
| 16 | + |
| 17 | +public class BloomFilterDriver { |
| 18 | + |
| 19 | + public static void main(String[] args) throws Exception { |
| 20 | + Configuration conf = new Configuration(); |
| 21 | + String[] otherArgs = new GenericOptionsParser(conf, args) |
| 22 | + .getRemainingArgs(); |
| 23 | + if (otherArgs.length != 4) { |
| 24 | + System.err |
| 25 | + .println("Usage: BloomFilterWriter <inputfile> <nummembers> <falseposrate> <bfoutfile>"); |
| 26 | + System.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + FileSystem fs = FileSystem.get(new Configuration()); |
| 30 | + |
| 31 | + // Parse command line arguments |
| 32 | + Path inputFile = new Path(otherArgs[0]); |
| 33 | + int numMembers = Integer.parseInt(otherArgs[1]); |
| 34 | + float falsePosRate = Float.parseFloat(otherArgs[2]); |
| 35 | + Path bfFile = new Path(otherArgs[3]); |
| 36 | + |
| 37 | + // Calculate our vector size and optimal K value based on approximations |
| 38 | + int vectorSize = getOptimalBloomFilterSize(numMembers, falsePosRate); |
| 39 | + int nbHash = getOptimalK(numMembers, vectorSize); |
| 40 | + |
| 41 | + // create new Bloom filter |
| 42 | + BloomFilter filter = new BloomFilter(vectorSize, nbHash, |
| 43 | + Hash.MURMUR_HASH); |
| 44 | + |
| 45 | + // Open file for read |
| 46 | + |
| 47 | + System.out.println("Training Bloom filter of size " + vectorSize |
| 48 | + + " with " + nbHash + " hash functions, " + numMembers |
| 49 | + + " approximate number of records, and " + falsePosRate |
| 50 | + + " false positive rate"); |
| 51 | + |
| 52 | + String line = null; |
| 53 | + int numRecords = 0; |
| 54 | + for (FileStatus status : fs.listStatus(inputFile)) { |
| 55 | + BufferedReader rdr; |
| 56 | + // if file is gzipped, wrap it in a GZIPInputStream |
| 57 | + if (status.getPath().getName().endsWith(".gz")) { |
| 58 | + rdr = new BufferedReader(new InputStreamReader( |
| 59 | + new GZIPInputStream(fs.open(status.getPath())))); |
| 60 | + } else { |
| 61 | + rdr = new BufferedReader(new InputStreamReader(fs.open(status |
| 62 | + .getPath()))); |
| 63 | + } |
| 64 | + |
| 65 | + System.out.println("Reading " + status.getPath()); |
| 66 | + while ((line = rdr.readLine()) != null) { |
| 67 | + filter.add(new Key(line.getBytes())); |
| 68 | + ++numRecords; |
| 69 | + } |
| 70 | + |
| 71 | + rdr.close(); |
| 72 | + } |
| 73 | + |
| 74 | + System.out.println("Trained Bloom filter with " + numRecords |
| 75 | + + " entries."); |
| 76 | + |
| 77 | + System.out.println("Serializing Bloom filter to HDFS at " + bfFile); |
| 78 | + FSDataOutputStream strm = fs.create(bfFile); |
| 79 | + filter.write(strm); |
| 80 | + |
| 81 | + strm.flush(); |
| 82 | + strm.close(); |
| 83 | + |
| 84 | + System.out.println("Done training Bloom filter."); |
| 85 | + } |
| 86 | + |
| 87 | + public static int getOptimalBloomFilterSize(int numRecords, |
| 88 | + float falsePosRate) { |
| 89 | + int size = (int) (-numRecords * (float) Math.log(falsePosRate) / Math |
| 90 | + .pow(Math.log(2), 2)); |
| 91 | + return size; |
| 92 | + } |
| 93 | + |
| 94 | + public static int getOptimalK(float numMembers, float vectorSize) { |
| 95 | + return (int) Math.round(vectorSize / numMembers * Math.log(2)); |
| 96 | + } |
| 97 | +} |
0 commit comments