· 8 years ago · Mar 08, 2018, 10:54 PM
1import java.io.IOException;
2import java.text.SimpleDateFormat;
3import java.util.Calendar;
4import java.util.Date;
5
6import org.apache.hadoop.conf.Configuration;
7import org.apache.hadoop.hbase.HBaseConfiguration;
8import org.apache.hadoop.hbase.HColumnDescriptor;
9import org.apache.hadoop.hbase.HTableDescriptor;
10import org.apache.hadoop.hbase.client.Get;
11import org.apache.hadoop.hbase.client.HBaseAdmin;
12import org.apache.hadoop.hbase.client.HTable;
13import org.apache.hadoop.hbase.client.Put;
14import org.apache.hadoop.hbase.client.Result;
15import org.apache.hadoop.hbase.client.ResultScanner;
16import org.apache.hadoop.hbase.client.Scan;
17import org.apache.hadoop.hbase.util.Bytes;
18
19public class Populator {
20
21 public static void main(String[] args) throws IOException {
22 System.out.println("HBase Start");
23
24 // The configuration object tells the client where to connect.
25 // It reads from hbase-site.xml and hbase-default.xml (from hbase-0.94.3/conf)
26 Configuration config = HBaseConfiguration.create();
27 //config.set("hbase.zookeeper.quorum", "localhost"); // Here we are running zookeeper locally
28
29 //gotta create the table before we do anything
30 HBaseAdmin admin = new HBaseAdmin(config);
31 HTableDescriptor messageTable = new HTableDescriptor(Bytes.toBytes("messages"));
32
33 //defining the column families of the table
34 HColumnDescriptor idsFamily = new HColumnDescriptor(Bytes.toBytes("ids"));
35 messageTable.addFamily(idsFamily);
36 HColumnDescriptor timeFamily = new HColumnDescriptor(Bytes.toBytes("timestamp"));
37 messageTable.addFamily(timeFamily);
38 HColumnDescriptor msgFamily = new HColumnDescriptor(Bytes.toBytes("text"));
39 messageTable.addFamily(msgFamily);
40
41 admin.createTable(messageTable);
42
43 // We initialize an HTable object to connect to our messages table.
44 HTable table = new HTable(config, "messages");
45
46 //----Now the table is created, everything under here involves populating the table----
47
48 //create 500 users and 10 different emails
49 String[] users = new String[500];
50 String[] emails = {"@gmail.com",
51 "@hotmail.com",
52 "@aol.com",
53 "@yahoo.com",
54 "@msn.com",
55 "@protonmail.com",
56 "@live.com",
57 "@asu.edu",
58 "@mail.com",
59 "@inbox.com",};
60
61 //every 10 users will have the same email domain (just to keep track & to throw in a little variety)
62 for (int i=0;i<500;i++)
63 {
64 users[i] = "human" + i + emails[i%10];
65 }
66
67 //the various date formats we will use
68 SimpleDateFormat sdfKey = new SimpleDateFormat(":yyyyMMddHHmmssSSS");
69 SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
70 SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss.SSS");
71 Date resultdate = new Date(System.currentTimeMillis());
72
73
74 //for every user, that user will send out 10 emails to the next 10 users on the array (so user 10 will send to users 11-20)
75 //%500 is used here so it wraps back - human495 will send to 496,497,498,499,1,2,3,4,5
76 for (int i=0;i<500;i++)
77 {
78 for (int j=0;j<10;j++)
79 {
80 //redefine resultdate
81 //set row key (user[i]+timestamp)
82 //do all the put stuff :)
83 resultdate = new Date(System.currentTimeMillis()); //reassign resultdate
84 System.out.println(users[i] + " Sent to " + users[(i+j+1)%500] + sdfKey.format(resultdate) + " Email #" + j);
85 }
86
87 }
88
89
90
91 //Defining our row keys: the ID number and the current system time in milliseconds to make it unique
92 //when scanning we can just scan for id+*
93 resultdate = new Date(System.currentTimeMillis());
94 byte[] rowKey1 = Bytes.add(Bytes.toBytes("1234"), Bytes.toBytes(sdfKey.format(resultdate).toString()));
95 byte[] rowKey2 = Bytes.add(Bytes.toBytes("7890"), Bytes.toBytes(sdfKey.format(resultdate).toString()));
96
97 Put p = new Put(Bytes.toBytes(Bytes.toString(rowKey1))); //value here is the row key(title)
98 p.add(Bytes.toBytes("ids"), Bytes.toBytes("toID"), Bytes.toBytes("1234"));
99 p.add(Bytes.toBytes("ids"), Bytes.toBytes("fromID"), Bytes.toBytes("7890"));
100 resultdate = new Date(System.currentTimeMillis());
101 String currentDate = sdfDate.format(resultdate).toString();
102 String currentTime = sdfTime.format(resultdate).toString();
103 p.add(Bytes.toBytes("timestamp"), Bytes.toBytes("date"), Bytes.toBytes(currentDate));
104 p.add(Bytes.toBytes("timestamp"), Bytes.toBytes("time"), Bytes.toBytes(currentTime));
105 p.add(Bytes.toBytes("text"), Bytes.toBytes("title"), Bytes.toBytes("Pressing Information"));
106 p.add(Bytes.toBytes("text"), Bytes.toBytes("body"), Bytes.toBytes("This is regarding your dog. He's a good boy."));
107 table.put(p);
108
109 // Adding row post3
110 p = new Put(Bytes.toBytes(Bytes.toString(rowKey2)));
111 p.add(Bytes.toBytes("ids"), Bytes.toBytes("toID"), Bytes.toBytes("7890"));
112 p.add(Bytes.toBytes("ids"), Bytes.toBytes("fromID"), Bytes.toBytes("1234"));
113 p.add(Bytes.toBytes("timestamp"), Bytes.toBytes("date"), Bytes.toBytes("Feb 3"));
114 p.add(Bytes.toBytes("timestamp"), Bytes.toBytes("time"), Bytes.toBytes("14:54"));
115 p.add(Bytes.toBytes("text"), Bytes.toBytes("title"), Bytes.toBytes("Inquiry"));
116 p.add(Bytes.toBytes("text"), Bytes.toBytes("body"), Bytes.toBytes("r u dum lol"));
117 table.put(p);
118
119 // We can retrieve the data of a row using Get. Get returns an array of byte
120 Get g = new Get(Bytes.toBytes("1234"));
121 Result r = table.get(g);
122 byte [] value1 = r.getValue(Bytes.toBytes("ids"), Bytes.toBytes("fromID"));
123 byte [] value2 = r.getValue(Bytes.toBytes("text"), Bytes.toBytes("title"));
124
125 // We convert to Strings
126 String valueStr1 = Bytes.toString(value1);
127 String valueStr2 = Bytes.toString(value2);
128 System.out.println("GET: " + valueStr1 + " " + valueStr2);
129
130 // We can retrieve the data of multiple rows using Scan
131 //Retrieving only column title of all the rows
132 Scan s = new Scan();
133 s.addColumn(Bytes.toBytes("text"), Bytes.toBytes("title"));
134 ResultScanner scanner = table.getScanner(s);
135 try {
136 for (Result rr : scanner)
137 System.out.println("Row data: " + rr);
138 }
139 finally {
140 scanner.close();
141 }
142
143 //Retrieving all columns of all the rows
144 /*
145 s = new Scan();
146 s.addFamily(Bytes.toBytes("post"));
147 scanner = table.getScanner(s);
148 try {
149 for (Result rr : scanner)
150 System.out.println("Row data: " + rr);
151 }
152 finally {
153 scanner.close();
154 }
155 */
156
157 table.close();//Releases held resources
158 System.out.println("HBase End");
159 }
160}