| 6 | package tsmc; |
| 7 | |
| 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 | public class CreateTable { |
| 16 | public static void createHBaseTable(String tablename, String family) |
| 17 | throws IOException { |
| 18 | // HTableDescriptor contains the name of an HTable, and its column |
| 19 | // families |
| 20 | // HTableDescriptor 用來描述table的屬性 |
| 21 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| 22 | // HColumnDescriptor HColumnDescriptor contains information about a |
| 23 | // column family such as the number of versions, compression settings, |
| 24 | // etc. |
| 25 | // HTableDescriptor 透過 add() 方法來加入Column family |
| 26 | |
| 27 | htd.addFamily(new HColumnDescriptor(family)); |
| 28 | |
| 29 | // HBaseConfiguration 能接收 hbase-site.xml 的設定值 |
| 30 | HBaseConfiguration config = new HBaseConfiguration(); |
| 31 | // 檔案的操作則使用 HBaseAdmin |
| 32 | HBaseAdmin admin = new HBaseAdmin(config); |
| 33 | // 檢查 |
| 34 | if (admin.tableExists(tablename)) { |
| 35 | System.out.println("Table: " + tablename + "Existed."); |
| 36 | } else { |
| 37 | System.out.println("create new table: " + tablename); |
| 38 | // 建立 |
| 39 | admin.createTable(htd); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static public void main(String argv[]) throws IOException { |
| 44 | |
| 45 | String tablename = "tsmc"; |
| 46 | String family = "Detail"; |
| 47 | |
| 48 | System.out.println("1. create table :" + tablename); |
| 49 | |
| 50 | createHBaseTable(tablename, family); |
| 51 | } |
| 52 | } |