52 | | |
53 | | |
| 61 | * 所有的程式碼都必須包含這些引入函式庫、開啟關閉socket 的敘述 |
| 62 | |
| 63 | = 二、各種對hbase的操作 = |
| 64 | |
| 65 | == 2.1 列出hbase 裡的所有 table == |
| 66 | {{{ |
| 67 | !#php |
| 68 | echo( "listing tables...\n" ); |
| 69 | $tables = $client->getTableNames(); |
| 70 | sort( $tables ); |
| 71 | foreach ( $tables as $name ) { |
| 72 | echo( " found: {$name}\n" ); |
| 73 | } |
| 74 | } |
| 75 | }}} |
| 76 | == 2.2 刪除table == |
| 77 | {{{ |
| 78 | !#php |
| 79 | $name = "hbase table name"; |
| 80 | if ($client->isTableEnabled( $name )) { |
| 81 | echo( " disabling table: {$name}\n"); |
| 82 | $client->disableTable( $name ); |
| 83 | } |
| 84 | echo( " deleting table: {$name}\n" ); |
| 85 | $client->deleteTable( $name ); |
| 86 | } |
| 87 | }}} |
| 88 | == 2.3 新增table == |
| 89 | |
| 90 | * 我們先定義columns 的物件結構如下 |
| 91 | {{{ |
| 92 | !#php |
| 93 | $columns = array( |
| 94 | new ColumnDescriptor( array( |
| 95 | 'name' => 'entry:', |
| 96 | 'maxVersions' => 10 |
| 97 | ) ), |
| 98 | new ColumnDescriptor( array( |
| 99 | 'name' => 'unused:' |
| 100 | ) ) |
| 101 | ); |
| 102 | }}} |
| 103 | |
| 104 | * 將剛剛的column 放到table 內 |
| 105 | {{{ |
| 106 | !#php |
| 107 | $t = "table name"; |
| 108 | echo( "creating table: {$t}\n" ); |
| 109 | |
| 110 | try { |
| 111 | $client->createTable( $t, $columns ); |
| 112 | } catch ( AlreadyExists $ae ) { |
| 113 | echo( "WARN: {$ae->message}\n" ); |
| 114 | } |
| 115 | |
| 116 | }}} |
| 117 | == 2.4 列出 table內的家族成員 family == |
| 118 | {{{ |
| 119 | !#php |
| 120 | $t = "table name"; |
| 121 | echo( "column families in {$t}:\n" ); |
| 122 | |
| 123 | $descriptors = $client->getColumnDescriptors( $t ); |
| 124 | asort( $descriptors ); |
| 125 | foreach ( $descriptors as $col ) { |
| 126 | echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" ); |
| 127 | } |
| 128 | }}} |
| 129 | == 2.5 寫入資料 == |
| 130 | {{{ |
| 131 | !#php |
| 132 | $t = "table name"; |
| 133 | $row = "row name" |
| 134 | $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93"; |
| 135 | |
| 136 | $mutations = array( |
| 137 | new Mutation( array( |
| 138 | 'column' => 'entry:foo', |
| 139 | 'value' => $valid |
| 140 | ) ), |
| 141 | ); |
| 142 | |
| 143 | $client->mutateRow( $t, $row, $mutations ); |
| 144 | }}} |
| 145 | |
| 146 | == 2.6 讀取資料 == |
| 147 | === get 取得一個 column value === |
| 148 | * get 取得一個 column value 的用法 |
| 149 | |
| 150 | $table_name = 't1'; |
| 151 | $row_name = '1'; |
| 152 | $fam_col_name = 'f1:c1'; |
| 153 | |
| 154 | $arr = $client->get($table_name, $row_name , $fam_col_name); |
| 155 | // $arr = array |
| 156 | foreach ( $arr as $k=>$v ) { |
| 157 | // $k = TCell |
| 158 | echo ("value = {$v->value} , <br> "); |
| 159 | echo ("timestamp = {$v->timestamp} <br>"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | === getRow 取得一整個row === |
| 164 | * getRow($tableName, $row) 用法 |
| 165 | {{{ |
| 166 | #!php |
| 167 | $table_name = "table name"; |
| 168 | $row_name = "row name"; |
| 169 | |
| 170 | $arr = $client->getRow($table_name, $row_name); |
| 171 | // $client->getRow return a array |
| 172 | foreach ( $arr as $k=>$TRowResult ) { |
| 173 | // $k = 0 ; non-use |
| 174 | // $TRowResult = TRowResult |
| 175 | printTRowResult($TRowResult); |
| 176 | } |
| 177 | }}} |
| 178 | |
| 179 | === scan 一整個table === |
| 180 | |
| 181 | {{{ |
| 182 | !#php |
| 183 | $table_name = 't1'; |
| 184 | $start_row = ""; // 從row 的起點開始 |
| 185 | $family = array( "f1","f2","f3" ); |
| 186 | |
| 187 | |
| 188 | $scanner = $client->scannerOpen( $table_name, $start_row , $family ); |
| 189 | // $scanner 是一個遞增數字 for open socket |
| 190 | // scannerGet() 一次只抓一row,因此要用while迴圈不斷地抓 |
| 191 | while (true ){ |
| 192 | $get_arr = $client->scannerGet( $scanner ); |
| 193 | // get_arr is an array |
| 194 | |
| 195 | if($get_arr == null) break; |
| 196 | // 沒有回傳值代表已經沒有資料可抓,跳脫此無限迴圈 |
| 197 | |
| 198 | foreach ( $get_arr as $TRowResult ){ |
| 199 | // $TRowResult = TRowResult |
| 200 | echo (" row = {$TRowResult->row} ; <br> "); |
| 201 | $column = $TRowResult->columns; |
| 202 | foreach ($column as $family_column=>$Tcell){ |
| 203 | echo ("family:column = $family_column "); |
| 204 | // $family_column = family_column |
| 205 | // $Tcell = Tcell |
| 206 | echo (" value = {$Tcell->value} "); |
| 207 | echo (" timestamp = {$Tcell->timestamp} <br>"); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | echo( "<br> ----------------- " ); |
| 212 | echo( "<br> Scanner finished <br>" ); |
| 213 | $client->scannerClose( $scanner ); |
| 214 | |
| 215 | }}} |