|
| 1 | +package jaglion; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.UUID; |
| 5 | + |
| 6 | +import org.apache.commons.codec.digest.DigestUtils; |
| 7 | + |
| 8 | +import org.apache.pig.EvalFunc; |
| 9 | +import org.apache.pig.data.Tuple; |
| 10 | + |
| 11 | +import org.apache.hadoop.conf.Configuration; |
| 12 | +import org.apache.hadoop.hbase.HBaseConfiguration; |
| 13 | +import org.apache.hadoop.hbase.HTableDescriptor; |
| 14 | +import org.apache.hadoop.hbase.HColumnDescriptor; |
| 15 | +import org.apache.hadoop.hbase.MasterNotRunningException; |
| 16 | +import org.apache.hadoop.hbase.ZooKeeperConnectionException; |
| 17 | +import org.apache.hadoop.hbase.client.HBaseAdmin; |
| 18 | +import org.apache.hadoop.hbase.client.HTable; |
| 19 | +import org.apache.hadoop.hbase.client.Put; |
| 20 | +import org.apache.hadoop.hbase.util.Bytes; |
| 21 | + |
| 22 | +public class ANONYMIZE extends EvalFunc<String> |
| 23 | +{ |
| 24 | + public Configuration config; |
| 25 | + public String fmap = "FowardMap"; |
| 26 | + public String rmap = "ReversMap"; |
| 27 | + |
| 28 | + public String exec(Tuple input) throws IOException { |
| 29 | + |
| 30 | + // As long as we have a reasonable inputs |
| 31 | + if (input.size() != 2 || input.get(0) == null || input.get(1) == null) |
| 32 | + return null; |
| 33 | + |
| 34 | + // Connect to HBASE |
| 35 | + config = HBaseConfiguration.create(); |
| 36 | + HBaseAdmin hba = new HBaseAdmin(config); |
| 37 | + HTableDescriptor tableDescriptor = null; |
| 38 | + HColumnDescriptor columnDescriptor = null; |
| 39 | + |
| 40 | + // Check and optionally create the forward mapping table |
| 41 | + if (hba.tableExists(fmap) == false) { |
| 42 | + tableDescriptor = new HTableDescriptor(fmap); |
| 43 | + columnDescriptor = new HColumnDescriptor("AnonymousValue"); |
| 44 | + tableDescriptor.addFamily(columnDescriptor); |
| 45 | + hba.createTable(tableDescriptor); |
| 46 | + } |
| 47 | + |
| 48 | + // Check and optionally create the reverse map table |
| 49 | + if (hba.tableExists(rmap) == false) { |
| 50 | + tableDescriptor = new HTableDescriptor(rmap); |
| 51 | + columnDescriptor = new HColumnDescriptor("ActualValue"); |
| 52 | + tableDescriptor.addFamily(columnDescriptor); |
| 53 | + hba.createTable(tableDescriptor); |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + // Read inputs |
| 58 | + String id = (String)input.get(0); |
| 59 | + int unique = (int)input.get(1); |
| 60 | + System.out.println("Value: " + id + " Unique: " + unique); |
| 61 | + Put p = null; |
| 62 | + |
| 63 | + // Generate value for input id |
| 64 | + String uuid = UUID.randomUUID().toString(); |
| 65 | + String hash = DigestUtils.shaHex(id); |
| 66 | + |
| 67 | + System.out.println("UUID: " + uuid + " Hash: " + hash); |
| 68 | + |
| 69 | + // Store the value forward and backward |
| 70 | + HTable forward = new HTable(config, fmap); |
| 71 | + HTable backward = new HTable(config, rmap); |
| 72 | + |
| 73 | + // Forward Map |
| 74 | + p = new Put(Bytes.toBytes(id)); |
| 75 | + p.add(Bytes.toBytes("AnonymousValue"), Bytes.toBytes("hash"), Bytes.toBytes(hash)); |
| 76 | + p.add(Bytes.toBytes("AnonymousValue"), Bytes.toBytes("uuid"), Bytes.toBytes(uuid)); |
| 77 | + forward.put(p); |
| 78 | + |
| 79 | + System.out.println("Stored forward map."); |
| 80 | + |
| 81 | + // Reverse Map - Hash |
| 82 | + p = new Put(Bytes.toBytes(hash)); |
| 83 | + p.add(Bytes.toBytes("ActualValue"), Bytes.toBytes(""), Bytes.toBytes(id)); |
| 84 | + backward.put(p); |
| 85 | + |
| 86 | + System.out.println("Stored reverse map - hash."); |
| 87 | + |
| 88 | + // Reverse Map - UUID |
| 89 | + p = new Put(Bytes.toBytes(uuid)); |
| 90 | + p.add(Bytes.toBytes("ActualValue"), Bytes.toBytes(""), Bytes.toBytes(id)); |
| 91 | + backward.put(p); |
| 92 | + |
| 93 | + System.out.println("Stored reverse map - uuid."); |
| 94 | + |
| 95 | + // If the request was for a unique value return the uuid |
| 96 | + if (unique == 1) { |
| 97 | + return uuid; |
| 98 | + } else { |
| 99 | + // otherwise return the hashed value |
| 100 | + return hash; |
| 101 | + } |
| 102 | + } catch(Exception e) { |
| 103 | + System.out.println(input); |
| 104 | + throw new IOException("Caught exception processing input row ", e); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments