| | 1 | {{{ |
| | 2 | #!html |
| | 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| | 4 | ITRI HBase 進階課程 |
| | 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| | 6 | HBase 範例 |
| | 7 | </big></big></div> |
| | 8 | }}} |
| | 9 | |
| | 10 | [wiki:waue/2011/0426 回課程大綱 << ] 第一關 [wiki:waue/2011/0426_4_2 > 下一關 ] |
| | 11 | |
| | 12 | = 注意 = |
| | 13 | |
| | 14 | * 請將以下給個 hbase 重要的檔複製到 hadoop lib 目錄下,並且重新啟動 hbase 與 hadoop |
| | 15 | |
| | 16 | {{{ |
| | 17 | $ cd /opt/hbase |
| | 18 | $ cp hbase-*.jar lib/zookeeper-3.2.2.jar contrib/transactional/hbase-0.20.6-transactional.jar /opt/hadoop/lib/ |
| | 19 | $ /opt/hbase/bin/stop-hbase.sh |
| | 20 | $ /opt/hadoop/bin/stop-all.sh |
| | 21 | $ /opt/hadoop/bin/start-all.sh |
| | 22 | $ /opt/hbase/bin/start-hbase.sh |
| | 23 | }}} |
| | 24 | |
| | 25 | * 若編譯以下範例的環境為console 端,請設定完整的classpath,詳細方法請看教材投影片 |
| | 26 | * 若開發是透過 eclipse ,請將 hbase-core.jar , zookeeper*.jar , hbase-*-transactional.jar 匯入到 project library 中,詳細方法請看教材投影片 |
| | 27 | |
| | 28 | * 注意: 若此程式執行於 eclipse ,則方便起見,請將程式的 eclipse only 的後兩行的註解取消,如: |
| | 29 | |
| | 30 | {{{ |
| | 31 | #!java |
| | 32 | // eclipse only |
| | 33 | String[] args = {"ex1Table","Detail"}; |
| | 34 | argv = args; |
| | 35 | }}} |
| | 36 | |
| | 37 | = 範例一:新增Table = |
| | 38 | |
| | 39 | {{{ |
| | 40 | bin/hadoop jar ItriMenu.jar CreateTable ex1Table Detail |
| | 41 | }}} |
| | 42 | |
| | 43 | = !CreateTable.java = |
| | 44 | {{{ |
| | 45 | #!java |
| | 46 | package itri; |
| | 47 | |
| | 48 | import java.io.IOException; |
| | 49 | |
| | 50 | import org.apache.hadoop.conf.Configuration; |
| | 51 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| | 52 | import org.apache.hadoop.hbase.HColumnDescriptor; |
| | 53 | import org.apache.hadoop.hbase.HTableDescriptor; |
| | 54 | import org.apache.hadoop.hbase.client.HBaseAdmin; |
| | 55 | import org.apache.hadoop.util.GenericOptionsParser; |
| | 56 | |
| | 57 | public class CreateTable { |
| | 58 | public static void createHBaseTable(String tablename, String family) |
| | 59 | throws IOException { |
| | 60 | // HTableDescriptor contains the name of an HTable, and its column |
| | 61 | // families |
| | 62 | // HTableDescriptor 用來描述table的屬性 |
| | 63 | HTableDescriptor htd = new HTableDescriptor(tablename); |
| | 64 | // HColumnDescriptor HColumnDescriptor contains information about a |
| | 65 | // column family such as the number of versions, compression settings, |
| | 66 | // etc. |
| | 67 | // HTableDescriptor 透過 add() 方法來加入Column family |
| | 68 | |
| | 69 | htd.addFamily(new HColumnDescriptor(family)); |
| | 70 | |
| | 71 | // HBaseConfiguration 能接收 hbase-site.xml 的設定值 |
| | 72 | HBaseConfiguration config = new HBaseConfiguration(); |
| | 73 | // 檔案的操作則使用 HBaseAdmin |
| | 74 | HBaseAdmin admin = new HBaseAdmin(config); |
| | 75 | // 檢查 |
| | 76 | if (admin.tableExists(tablename)) { |
| | 77 | System.out.println("Table: " + tablename + "Existed."); |
| | 78 | } else { |
| | 79 | System.out.println("create new table: " + tablename); |
| | 80 | // 建立 |
| | 81 | admin.createTable(htd); |
| | 82 | } |
| | 83 | } |
| | 84 | |
| | 85 | static public void main(String argv[]) throws IOException { |
| | 86 | // String[] argc = {"t1","f1"};argv = argc; |
| | 87 | String[] otherArgs = new GenericOptionsParser(new Configuration(), argv) |
| | 88 | .getRemainingArgs(); |
| | 89 | if (otherArgs.length < 2) { |
| | 90 | System.out.println("CreateTable <newTableName> <Family>"); |
| | 91 | return; |
| | 92 | } |
| | 93 | |
| | 94 | String tablename = otherArgs[0]; |
| | 95 | String family = otherArgs[1]; |
| | 96 | |
| | 97 | System.out.println("1. create table :" + tablename); |
| | 98 | |
| | 99 | createHBaseTable(tablename, family); |
| | 100 | } |
| | 101 | } |
| | 102 | |
| | 103 | }}} |
| | 104 | |
| | 105 | * 執行結果 |
| | 106 | |
| | 107 | {{{ |
| | 108 | 1. create table :ex1Table |
| | 109 | ex1Table created. |
| | 110 | }}} |