Changes between Version 1 and Version 2 of waue/2010/0125
- Timestamp:
- Jan 25, 2010, 5:13:29 PM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
waue/2010/0125
v1 v2 184 184 package hbase020; 185 185 186 // from hbase website 186 187 188 // 說明: 189 // 此程式首先建立table, 然後put一個資料後 190 // 用get 方法取得資料,最後用scan方法取出整個資料表 191 // 192 // 參考: 程式碼改編自 187 193 // http://hadoop.apache.org/hbase/docs/current/api/org/apache/.接. 188 194 // hadoop/hbase/client/package-summary.html#package_description … … 205 211 // Does a Put, Get and a Scan against an hbase table. 206 212 public class MyLittleHBaseClient { 213 214 private static String table_name = "mytable"; 215 216 private static String the_row = "myLittleRow"; 217 218 private static String column_family = "family"; 219 220 private static String column_qualifier = "someQualifier"; 221 222 private static String cell_value = "somvalue"; 223 224 225 207 226 public static void createHBaseTable(String tablename) throws IOException { 208 227 // HTableDescriptor contains the name of an HTable, and its column … … 214 233 // etc. 215 234 // HTableDescriptor 透過 add() 方法來加入Column family 216 htd.addFamily(new HColumnDescriptor( "content:"));235 htd.addFamily(new HColumnDescriptor(column_family+":")); 217 236 // HBaseConfiguration 能接收 hbase-site.xml 的設定值 218 237 HBaseConfiguration config = new HBaseConfiguration(); … … 230 249 admin.createTable(htd); 231 250 } 251 232 252 public static void main(String[] args) throws IOException { 233 253 // You need a configuration object to tell the client where to connect. 234 254 // When you create a HBaseConfiguration, it reads in whatever you've set 235 255 // into your hbase-site.xml and in hbase-default.xml, as long as these 236 // can 237 // be found on the CLASSPATH256 // can be found on the CLASSPATH 257 238 258 HBaseConfiguration config = new HBaseConfiguration(); 239 259 260 // create table automatically 261 createHBaseTable(table_name); 262 240 263 // This instantiates an HTable object that connects you to 241 264 // the "myLittleHBaseTable" table. 242 HTable table = new HTable(config, "myLittleHBaseTable"); 265 HTable table = new HTable(config, table_name); 266 243 267 244 268 // To add to a row, use Put. A Put constructor takes the name of the row … … 254 278 // the timestamp to use in your update, etc.If no timestamp, the server 255 279 // applies current time to the edits. 256 Put p = new Put(Bytes.toBytes( "myLittleRow"));280 Put p = new Put(Bytes.toBytes(the_row)); 257 281 258 282 // To set the value you'd like to update in the row 'myRow', specify the … … 264 288 // hbase is all about byte arrays. Lets pretend the table 265 289 // 'myLittleHBaseTable' was created with a family 'myLittleFamily'. 266 p.add(Bytes.toBytes( "myLittleFamily"), Bytes.toBytes("someQualifier"),267 Bytes.toBytes( "Some Value"));290 p.add(Bytes.toBytes(column_family), Bytes.toBytes(column_qualifier), 291 Bytes.toBytes(cell_value)); 268 292 269 293 // Once you've adorned your Put instance with all the updates you want … … 280 304 // up 281 305 // the hbase return into the form you find most palatable. 282 Get g = new Get(Bytes.toBytes( "myLittleRow"));306 Get g = new Get(Bytes.toBytes(the_row)); 283 307 Result r = table.get(g); 284 byte[] value = r.getValue(Bytes.toBytes( "myLittleFamily"), Bytes285 .toBytes( "someQualifier"));308 byte[] value = r.getValue(Bytes.toBytes(column_family), Bytes 309 .toBytes(column_qualifier)); 286 310 // If we convert the value bytes, we should get back 'Some Value', the 287 311 // value we inserted at this location. … … 296 320 // and a Get, create a Scan. Adorn it with column names, etc. 297 321 Scan s = new Scan(); 298 s.addColumn(Bytes.toBytes( "myLittleFamily"), Bytes299 .toBytes( "someQualifier"));322 s.addColumn(Bytes.toBytes(column_family), Bytes 323 .toBytes(column_qualifier)); 300 324 ResultScanner scanner = table.getScanner(s); 301 325 try { … … 303 327 // Now, for the actual iteration. One way is to use a while loop 304 328 // like so: 305 for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {329 for (Result rr : scanner) { 306 330 // print out the row we found and the columns we were looking 307 331 // for