| | 1 | {{{ |
| | 2 | #!java |
| | 3 | package tsmc; |
| | 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 | // TSMC serial program number 1 |
| | 17 | |
| | 18 | // 1. create table then read file to insert into hbase |
| | 19 | // tablename: tsmc |
| | 20 | // family : Detail, Products, Turnover |
| | 21 | // 2. edit a text file |
| | 22 | // format of "/tmp/tsmc/store.txt" is as: |
| | 23 | // T01;GunLong; 01;20;40;30;50 |
| | 24 | // T02;Esing; 02;50 |
| | 25 | // T03;SunDon; 03;40;30 |
| | 26 | // T04;StarBucks; 04;50;50;20 |
| | 27 | // |
| | 28 | public class TSMC1LoadFile { |
| | 29 | |
| | 30 | void loadFile2HBase(String file_in, String table_name) throws IOException { |
| | 31 | |
| | 32 | BufferedReader fi = new BufferedReader( |
| | 33 | new FileReader(new File(file_in))); |
| | 34 | |
| | 35 | String line; |
| | 36 | while ((line = fi.readLine()) != null) { |
| | 37 | |
| | 38 | String[] str = line.split(";"); |
| | 39 | int length = str.length; |
| | 40 | PutData.putData(table_name, str[0].trim(), "Detail", "Name", str[1] |
| | 41 | .trim()); |
| | 42 | PutData.putData(table_name, str[0].trim(), "Detail", "Locate", |
| | 43 | str[2].trim()); |
| | 44 | for (int i = 3; i < length; i++) { |
| | 45 | PutData.putData(table_name, str[0], "Products", "P" + (i - 2), |
| | 46 | str[i]); |
| | 47 | } |
| | 48 | System.out.println(); |
| | 49 | } |
| | 50 | fi.close(); |
| | 51 | |
| | 52 | } |
| | 53 | |
| | 54 | public void createHBaseTable(String tablename, String[] family) |
| | 55 | throws IOException { |
| | 56 | |
| | 57 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| | 58 | |
| | 59 | for (String fa : family) { |
| | 60 | htd.addFamily(new HColumnDescriptor(fa)); |
| | 61 | } |
| | 62 | |
| | 63 | HBaseConfiguration config = new HBaseConfiguration(); |
| | 64 | |
| | 65 | HBaseAdmin admin = new HBaseAdmin(config); |
| | 66 | |
| | 67 | if (admin.tableExists(tablename)) { |
| | 68 | System.out.println("Table: " + tablename + "Existed."); |
| | 69 | } else { |
| | 70 | System.out.println("create new table: " + tablename); |
| | 71 | |
| | 72 | admin.createTable(htd); |
| | 73 | } |
| | 74 | } |
| | 75 | |
| | 76 | static public void main(String args[]) throws IOException { |
| | 77 | String tablename = "tsmc"; |
| | 78 | String[] family = { "Detail", "Products", "Turnover" }; |
| | 79 | String loadfile = "/tmp/tsmc/store.txt"; |
| | 80 | // run |
| | 81 | TSMC1LoadFile lf = new TSMC1LoadFile(); |
| | 82 | lf.createHBaseTable(tablename, family); |
| | 83 | lf.loadFile2HBase(loadfile, tablename); |
| | 84 | } |
| | 85 | } |
| | 86 | |
| | 87 | }}} |