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