Changes between Version 2 and Version 3 of table_example1


Ignore:
Timestamp:
09/14/2012 12:01:16 PM (14 years ago)
Author:
liaojiaohe
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • table_example1

    v2 v3  
    5252} 
    5353}}} 
     54 
     55进行分页查询 
     56{{{ 
     57public static List<Action> getUserActions(int userid, int offset, int limit) 
     58throws Exception { 
     59  // Initialize counter and List to return 
     60  int count = 0; 
     61  List<Action> actions = new ArrayList<Action>(limit); 
     62  
     63  // Initialize startRow, stopRow, and columns to match 
     64  byte [] startRow = makeActionRow(userid,0,0); 
     65  byte [] stopRow = makeActionRow(userid,Long.MAX_VALUE,Integer.MAX_VALUE); 
     66  byte [][] columns = {Bytes.toBytes("content:name")}; 
     67  
     68  // Open Scanner 
     69  HTable ht = new HTable("useractions"); 
     70  Scanner s = ht.getScanner(columns,startRow,stopRow); 
     71  RowResult res = null; 
     72  
     73  // Iterate over Scanner 
     74  while((res = s.next()) != null) { 
     75    // Check if past offset 
     76    if(++count <= offset) continue; 
     77  
     78    // Get data from RowResult 
     79    byte [] row = res.getRow(); 
     80    byte [] value = res.get(columns[0]).getValue(); 
     81  
     82    // Build Action 
     83    Action action = readActionRow(row); 
     84    String name = Bytes.toString(value); 
     85    action.setName(name); 
     86    actions.add(action); 
     87  
     88    // Check limit 
     89    if(count == offset + limit) break; 
     90  } 
     91  // Cleanup and return 
     92  s.close(); 
     93  return actions; 
     94} 
     95}}}