| 1 | {{{ |
| 2 | #!java |
| 3 | package itri; |
| 4 | |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.File; |
| 7 | import java.io.FileReader; |
| 8 | import java.io.IOException; |
| 9 | |
| 10 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 11 | import org.apache.hadoop.hbase.HColumnDescriptor; |
| 12 | import org.apache.hadoop.hbase.HTableDescriptor; |
| 13 | import org.apache.hadoop.hbase.client.HBaseAdmin; |
| 14 | |
| 15 | |
| 16 | // ITRI serial program number 1 |
| 17 | |
| 18 | // 1. create table then read file to insert into hbase |
| 19 | // tablename: itri |
| 20 | // family : Detail, Products, Turnover |
| 21 | // 2. edit a text file |
| 22 | // format of "/tmp/itri/store.txt" is as |
| 23 | // T01package itri; |
| 24 | |
| 25 | import java.io.BufferedReader; |
| 26 | import java.io.File; |
| 27 | import java.io.FileReader; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 31 | import org.apache.hadoop.hbase.HColumnDescriptor; |
| 32 | import org.apache.hadoop.hbase.HTableDescriptor; |
| 33 | import org.apache.hadoop.hbase.client.HBaseAdmin; |
| 34 | |
| 35 | |
| 36 | // ITRI serial program number 1 |
| 37 | |
| 38 | // 1. create table then read file to insert into hbase |
| 39 | // tablename: itri |
| 40 | // family : Detail, Products, Turnover |
| 41 | // 2. edit a text file |
| 42 | // format of "/tmp/itri/store.txt" is as |
| 43 | // T01;GunLong; 01;20;40;30;50 |
| 44 | // T02;Esing; 02;50 |
| 45 | // T03;SunDon; 03;40;30 |
| 46 | // T04;StarBucks; 04;50;50;20 |
| 47 | // |
| 48 | public class Itri1LoadFile { |
| 49 | |
| 50 | void loadFile2HBase(String file_in, String table_name) throws IOException { |
| 51 | |
| 52 | BufferedReader fi = new BufferedReader( |
| 53 | new FileReader(new File(file_in))); |
| 54 | |
| 55 | String line; |
| 56 | while ((line = fi.readLine()) != null) { |
| 57 | |
| 58 | String[] str = line.split(";"); |
| 59 | int length = str.length; |
| 60 | PutData.putData(table_name, str[0].trim(), "Detail", "Name", str[1] |
| 61 | .trim()); |
| 62 | PutData.putData(table_name, str[0].trim(), "Detail", "Locate", |
| 63 | str[2].trim()); |
| 64 | for (int i = 3; i < length; i++) { |
| 65 | PutData.putData(table_name, str[0], "Products", "P" + (i - 2), |
| 66 | str[i]); |
| 67 | } |
| 68 | System.out.println(); |
| 69 | } |
| 70 | fi.close(); |
| 71 | |
| 72 | }package itri; |
| 73 | |
| 74 | import java.io.BufferedReader; |
| 75 | import java.io.File; |
| 76 | import java.io.FileReader; |
| 77 | import java.io.IOException; |
| 78 | |
| 79 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 80 | import org.apache.hadoop.hbase.HColumnDescriptor; |
| 81 | import org.apache.hadoop.hbase.HTableDescriptor; |
| 82 | import org.apache.hadoop.hbase.client.HBaseAdmin; |
| 83 | |
| 84 | |
| 85 | // ITRI serial program number 1 |
| 86 | |
| 87 | // 1. create table then read file to insert into hbase |
| 88 | // tablename: itri |
| 89 | // family : Detail, Products, Turnover |
| 90 | // 2. edit a text file |
| 91 | // format of "/tmp/itri/store.txt" is as |
| 92 | // T01;GunLong; 01;20;40;30;50 |
| 93 | // T02;Esing; 02;50 |
| 94 | // T03;SunDon; 03;40;30 |
| 95 | // T04;StarBucks; 04;50;50;20 |
| 96 | // |
| 97 | public class Itri1LoadFile { |
| 98 | |
| 99 | void loadFile2HBase(String file_in, String table_name) throws IOException { |
| 100 | |
| 101 | BufferedReader fi = new BufferedReader( |
| 102 | new FileReader(new File(file_in))); |
| 103 | |
| 104 | String line; |
| 105 | while ((line = fi.readLine()) != null) { |
| 106 | |
| 107 | String[] str = line.split(";"); |
| 108 | int length = str.length; |
| 109 | PutData.putData(table_name, str[0].trim(), "Detail", "Name", str[1] |
| 110 | .trim()); |
| 111 | PutData.putData(table_name, str[0].trim(), "Detail", "Locate", |
| 112 | str[2].trim()); |
| 113 | for (int i = 3; i < length; i++) { |
| 114 | PutData.putData(table_name, str[0], "Products", "P" + (i - 2), |
| 115 | str[i]); |
| 116 | } |
| 117 | System.out.println(); |
| 118 | } |
| 119 | fi.close(); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | public void createHBaseTable(String tablename, String[] family) |
| 124 | throws IOException { |
| 125 | |
| 126 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| 127 | |
| 128 | for (String fa : family) { |
| 129 | htd.addFamily(new HColumnDescriptor(fa)); |
| 130 | } |
| 131 | |
| 132 | HBaseConfiguration config = new HBaseConfiguration(); |
| 133 | |
| 134 | HBaseAdmin admin = new HBaseAdmin(config); |
| 135 | |
| 136 | if (admin.tableExists(tablename)) { |
| 137 | System.out.println("Table: " + tablename + "Existed."); |
| 138 | } else { |
| 139 | System.out.println("create new table: " + tablename); |
| 140 | |
| 141 | admin.createTable(htd); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static public void main(String args[]) throws IOException { |
| 146 | String tablename = "itri"; |
| 147 | String[] family = { "Detail", "Products", "Turnover" }; |
| 148 | String loadfile = "/tmp/itri/store.txt"; |
| 149 | // run |
| 150 | Itri1LoadFile lf = new Itri1LoadFile(); |
| 151 | lf.createHBaseTable(tablename, family); |
| 152 | lf.loadFile2HBase(loadfile, tablename); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public void createHBaseTable(String tablename, String[] family) |
| 157 | throws IOException { |
| 158 | |
| 159 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| 160 | |
| 161 | for (String fa : family) { |
| 162 | htd.addFamily(new HColumnDescriptor(fa)); |
| 163 | } |
| 164 | |
| 165 | HBaseConfiguration config = new HBaseConfiguration(); |
| 166 | |
| 167 | HBaseAdmin admin = new HBaseAdmin(config); |
| 168 | |
| 169 | if (admin.tableExists(tablename)) { |
| 170 | System.out.println("Table: " + tablename + "Existed."); |
| 171 | } else { |
| 172 | System.out.println("create new table: " + tablename); |
| 173 | |
| 174 | admin.createTable(htd); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static public void main(String args[]) throws IOException { |
| 179 | String tablename = "itri"; |
| 180 | String[] family = { "Detail", "Products", "Turnover" }; |
| 181 | String loadfile = "/tmp/itri/store.txt"; |
| 182 | // run |
| 183 | Itri1LoadFile lf = new Itri1LoadFile(); |
| 184 | lf.createHBaseTable(tablename, family); |
| 185 | lf.loadFile2HBase(loadfile, tablename); |
| 186 | } |
| 187 | };GunLong; 01;20;40;30;50 |
| 188 | // T02;Esing; 02;50 |
| 189 | // T03;SunDon; 03;40;30 |
| 190 | // T04;StarBucks; 04;50;50;20 |
| 191 | // |
| 192 | public class Itri1LoadFile { |
| 193 | |
| 194 | void loadFile2HBase(String file_in, String table_name) throws IOException { |
| 195 | |
| 196 | BufferedReader fi = new BufferedReader( |
| 197 | new FileReader(new File(file_in))); |
| 198 | |
| 199 | String line; |
| 200 | while ((line = fi.readLine()) != null) { |
| 201 | |
| 202 | String[] str = line.split(";"); |
| 203 | int length = str.length; |
| 204 | PutData.putData(table_name, str[0].trim(), "Detail", "Name", str[1] |
| 205 | .trim()); |
| 206 | PutData.putData(table_name, str[0].trim(), "Detail", "Locate", |
| 207 | str[2].trim()); |
| 208 | for (int i = 3; i < length; i++) { |
| 209 | PutData.putData(table_name, str[0], "Products", "P" + (i - 2), |
| 210 | str[i]); |
| 211 | } |
| 212 | System.out.println(); |
| 213 | } |
| 214 | fi.close(); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | public void createHBaseTable(String tablename, String[] family) |
| 219 | throws IOException { |
| 220 | |
| 221 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| 222 | |
| 223 | for (String fa : family) { |
| 224 | htd.addFamily(new HColumnDescriptor(fa)); |
| 225 | } |
| 226 | |
| 227 | HBaseConfiguration config = new HBaseConfiguration(); |
| 228 | |
| 229 | HBaseAdmin admin = new HBaseAdmin(config); |
| 230 | |
| 231 | if (admin.tableExists(tablename)) { |
| 232 | System.out.println("Table: " + tablename + "Existed."); |
| 233 | } else { |
| 234 | System.out.println("create new table: " + tablename); |
| 235 | |
| 236 | admin.createTable(htd); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static public void main(String args[]) throws IOException { |
| 241 | String tablename = "itri"; |
| 242 | String[] family = { "Detail", "Products", "Turnover" }; |
| 243 | String loadfile = "/tmp/itri/store.txt"; |
| 244 | // run |
| 245 | Itri1LoadFile lf = new Itri1LoadFile(); |
| 246 | lf.createHBaseTable(tablename, family); |
| 247 | lf.loadFile2HBase(loadfile, tablename); |
| 248 | } |
| 249 | } |
| 250 | }}} |