Advertisement
Guest User

Untitled

a guest
Jul 18th, 2011
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1.  
  2. public class Hello extends HttpServlet {
  3.  
  4. private static final long serialVersionUID = -7640993703289314548L;
  5.  
  6. private HTablePool htablePool;
  7.  
  8. @Override
  9. public void init(){
  10. Configuration config = HBaseConfiguration.create();
  11. config.set("hbase.zookeeper.quorum", "img648:2181");
  12. config.set("hbase.zookeeper.property.clientPort", "2181");
  13. htablePool = new HTablePool(config, 10);
  14. }
  15.  
  16. @Override
  17. public void destroy(){
  18. htablePool.closeTablePool("service_user_data_idx_name");
  19. }
  20.  
  21. public void doGet(HttpServletRequest request, HttpServletResponse response)
  22. throws IOException, ServletException {
  23.  
  24. response.setContentType("application/json");
  25. PrintWriter writer = response.getWriter();
  26.  
  27. String prefix = request.getParameter("prefix");
  28.  
  29. HTableInterface table = htablePool.getTable("service_user_data_idx_name");
  30. // HTable table = new HTable(config, "service_user_data_idx_name");
  31. Scan s = new Scan();
  32. s.addFamily(Bytes.toBytes("att"));
  33. s.setStartRow(Bytes.toBytes(prefix));
  34. Filter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes(prefix)));
  35. // Filter pageFilter = new PageFilter(10);
  36. // List<Filter> filters = Arrays.asList(rowFilter, pageFilter);
  37. // FilterList filterList = new FilterList(Operator.MUST_PASS_ALL, filters);
  38. // s.setFilter(filterList);
  39. s.setFilter(rowFilter);
  40. ResultScanner scanner = table.getScanner(s);
  41. Result r;
  42. String name,location;
  43. int i=0;
  44. List<Person> persons = new ArrayList<Person>();
  45. try{
  46. while((r=scanner.next())!=null && ++i<10){
  47. name = Bytes.toString(r.getRow());
  48. location = Bytes.toString(r.getValue(Bytes.toBytes("att"), Bytes.toBytes("location")));
  49. persons.add(new Person(name,location));
  50. }
  51. }finally{
  52. if(scanner!=null)
  53. scanner.close();
  54. if(table!=null)
  55. htablePool.putTable(table);
  56. // table.close();
  57. }
  58.  
  59. for(i=0; i<10; i++)
  60. persons.add(new Person("name"+i,"location"+i));
  61. Gson gson = new Gson();
  62. String json = gson.toJson(persons);
  63. writer.println(json);
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement