Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Hello extends HttpServlet {
- private static final long serialVersionUID = -7640993703289314548L;
- private HTablePool htablePool;
- @Override
- public void init(){
- Configuration config = HBaseConfiguration.create();
- config.set("hbase.zookeeper.quorum", "img648:2181");
- config.set("hbase.zookeeper.property.clientPort", "2181");
- htablePool = new HTablePool(config, 10);
- }
- @Override
- public void destroy(){
- htablePool.closeTablePool("service_user_data_idx_name");
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- response.setContentType("application/json");
- PrintWriter writer = response.getWriter();
- String prefix = request.getParameter("prefix");
- HTableInterface table = htablePool.getTable("service_user_data_idx_name");
- // HTable table = new HTable(config, "service_user_data_idx_name");
- Scan s = new Scan();
- s.addFamily(Bytes.toBytes("att"));
- s.setStartRow(Bytes.toBytes(prefix));
- Filter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes(prefix)));
- // Filter pageFilter = new PageFilter(10);
- // List<Filter> filters = Arrays.asList(rowFilter, pageFilter);
- // FilterList filterList = new FilterList(Operator.MUST_PASS_ALL, filters);
- // s.setFilter(filterList);
- s.setFilter(rowFilter);
- ResultScanner scanner = table.getScanner(s);
- Result r;
- String name,location;
- int i=0;
- List<Person> persons = new ArrayList<Person>();
- try{
- while((r=scanner.next())!=null && ++i<10){
- name = Bytes.toString(r.getRow());
- location = Bytes.toString(r.getValue(Bytes.toBytes("att"), Bytes.toBytes("location")));
- persons.add(new Person(name,location));
- }
- }finally{
- if(scanner!=null)
- scanner.close();
- if(table!=null)
- htablePool.putTable(table);
- // table.close();
- }
- for(i=0; i<10; i++)
- persons.add(new Person("name"+i,"location"+i));
- Gson gson = new Gson();
- String json = gson.toJson(persons);
- writer.println(json);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement