· 6 years ago · Apr 14, 2019, 06:14 PM
1package bdpuh.Assignment9a;
2import org.apache.hadoop.conf.Configuration;
3import org.apache.hadoop.hbase.*;
4import org.apache.hadoop.hbase.client.*;
5import org.apache.hadoop.hbase.util.Bytes;
6import java.io.IOException;
7import java.text.DateFormat;
8import java.text.SimpleDateFormat;
9import java.time.LocalDateTime;
10import java.time.format.DateTimeFormatter;
11import java.util.Date;
12
13
14
15
16public class Helper {
17
18 //initialize variable
19 Configuration conf;
20 HTable tbl;
21
22 //date and time formats for inputs
23 DateFormat dateFormat = new SimpleDateFormat("YYYY/MM/DD");
24 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:MM:SS");
25
26 //set up method to create table
27 public void setup() throws IOException{
28
29 //hbase configuration table
30 conf = HBaseConfiguration.create();
31 HBaseAdmin admin = new HBaseAdmin(conf);
32 tbl = new HTable(conf, "User");
33
34 //checks to see if table exists
35 if(!admin.tableExists("User")) {
36
37 //creates table with these column family
38 HTableDescriptor tblDesc = new HTableDescriptor(TableName.valueOf("User"));
39 tblDesc.addFamily(new HColumnDescriptor("creds"));
40 tblDesc.addFamily(new HColumnDescriptor("prefs"));
41 tblDesc.addFamily(new HColumnDescriptor("lastlogin"));
42
43 admin.createTable(tblDesc);
44
45 }
46
47 }
48
49
50 //add method
51 public void add(String[] inputs) {
52
53 //creating a new row
54 Put p = new Put(Bytes.toBytes(inputs[1]));
55 //adding user email
56 p.add(Bytes.toBytes("creds"),
57 Bytes.toBytes("email"),Bytes.toBytes(inputs[2]));
58 //adding user password
59 p.add(Bytes.toBytes("creds"),
60 Bytes.toBytes("password"),Bytes.toBytes(inputs[3]));
61 //adding status info
62 if(inputs[4].equals("married")||
63 inputs[4].equals("single") ){
64 p.add(Bytes.toBytes("prefs"),
65 Bytes.toBytes("status"),Bytes.toBytes(inputs[4]));
66 }else
67 {
68 System.out.println("status info not in correct format");
69 }
70
71
72 //adding date of birht info
73 p.add(Bytes.toBytes("prefs"),
74 Bytes.toBytes("date_of_birth"),Bytes.toBytes(inputs[5]));
75
76 //adding date of security info
77 p.add(Bytes.toBytes("prefs"),
78 Bytes.toBytes("security_question"),Bytes.toBytes(inputs[6]));
79
80 //adding date of security answer
81 p.add(Bytes.toBytes("prefs"),
82 Bytes.toBytes("security_answer"),Bytes.toBytes(inputs[7]));
83 try {
84 tbl.put(p);
85 } catch(IOException e){
86 e.printStackTrace();
87 }
88 try {
89 tbl.close();
90 }catch (IOException e){
91 e.printStackTrace();
92 }
93
94 }
95
96 //delete method
97 public void delete(String rowId){
98
99 //Delete row id
100 Delete del = new Delete(Bytes.toBytes(rowId));
101
102 try{
103 //add the deletion to the hbase table
104 tbl.delete(del);
105 } catch(IOException e){
106 e.printStackTrace();
107 }
108
109 }
110
111 //show method
112 public void show(String rowId) throws IOException, Exception {
113
114 //get the rowid info
115 Get g = new Get(Bytes.toBytes(rowId));
116
117
118 //include in the result object
119 Result disp = tbl.get(g);
120
121 System.out.println("RowId="+ Bytes.toString(disp.getRow()));
122
123 // go through the family and names and print values
124 for(Cell cell : disp.listCells()) {
125
126 String qual = Bytes.toString(CellUtil.cloneQualifier(cell));
127 String val = Bytes.toString(CellUtil.cloneValue(cell));
128 System.out.printf(" "+ qual+": " + val);
129
130 }
131
132 }
133
134 //list all values
135 public void listAll() throws Exception {
136
137 Scan scn = new Scan();
138 ResultScanner resScanner = tbl.getScanner(scn);
139 System.out.println("Listing Everything for : "+ tbl.getName());
140 for(Result result: resScanner){
141 show(Bytes.toString(result.getRow()));
142 }
143
144
145 }
146
147 //adds login information
148 public void login (String[] args) throws Exception {
149 Get g = new Get(Bytes.toBytes(args[1]));
150
151 Result disp = tbl.get(g);
152
153 byte[] pass = disp.getValue(Bytes.toBytes("creds"), Bytes.toBytes("password"));
154
155 if (args[2].equals(Bytes.toString(pass))) {
156
157 Put p = new Put(Bytes.toBytes(args[1]));
158
159 p.add(Bytes.toBytes("lastlogin"), Bytes.toBytes("ip"), Bytes.toBytes(args[3]));
160
161 Date date = new Date();
162 p.add(Bytes.toBytes("lastlogin"), Bytes.toBytes("date"),
163 Bytes.toBytes(dateFormat.format(date)));
164
165 LocalDateTime now = LocalDateTime.now();
166
167 p.add(Bytes.toBytes("lastlogin"), Bytes.toBytes("time"),
168 Bytes.toBytes(dtf.format(now)));
169
170 p.add(Bytes.toBytes("lastlogin"), Bytes.toBytes("success"),
171 Bytes.toBytes("success"));
172 try {
173 tbl.put(p);
174 } catch (IOException e) {
175 e.printStackTrace();
176 }
177 try {
178 tbl.close();
179 } catch (IOException e) {
180 e.printStackTrace();
181 }
182
183 } else {
184 System.out.println("Password is not correct");
185 }
186 }
187}