1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import org.apache.hadoop.hbase.TableNotFoundException;
44 import org.apache.hadoop.hbase.filter.Filter;
45 import org.apache.hadoop.hbase.rest.model.ScannerModel;
46
47 public class ScannerResource extends ResourceBase {
48
49 private static final Log LOG = LogFactory.getLog(ScannerResource.class);
50
51 static final Map<String,ScannerInstanceResource> scanners =
52 Collections.synchronizedMap(new HashMap<String,ScannerInstanceResource>());
53
54 TableResource tableResource;
55
56
57
58
59
60
61 public ScannerResource(TableResource tableResource)throws IOException {
62 super();
63 this.tableResource = tableResource;
64 }
65
66 static void delete(final String id) {
67 ScannerInstanceResource instance = scanners.remove(id);
68 if (instance != null) {
69 instance.generator.close();
70 }
71 }
72
73 Response update(final ScannerModel model, final boolean replace,
74 final UriInfo uriInfo) {
75 servlet.getMetrics().incrementRequests(1);
76 if (servlet.isReadOnly()) {
77 throw new WebApplicationException(Response.Status.FORBIDDEN);
78 }
79 byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
80 RowSpec spec = new RowSpec(model.getStartRow(), endRow,
81 model.getColumns(), model.getStartTime(), model.getEndTime(), 1);
82 try {
83 Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
84 String tableName = tableResource.getName();
85 ScannerResultGenerator gen =
86 new ScannerResultGenerator(tableName, spec, filter);
87 String id = gen.getID();
88 ScannerInstanceResource instance =
89 new ScannerInstanceResource(tableName, id, gen, model.getBatch());
90 scanners.put(id, instance);
91 if (LOG.isDebugEnabled()) {
92 LOG.debug("new scanner: " + id);
93 }
94 UriBuilder builder = uriInfo.getAbsolutePathBuilder();
95 URI uri = builder.path(id).build();
96 return Response.created(uri).build();
97 } catch (IOException e) {
98 throw new WebApplicationException(e,
99 Response.Status.SERVICE_UNAVAILABLE);
100 } catch (RuntimeException e) {
101 if (e.getCause() instanceof TableNotFoundException) {
102 throw new WebApplicationException(e, Response.Status.NOT_FOUND);
103 }
104 throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
105 } catch (Exception e) {
106 throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
107 }
108 }
109
110 @PUT
111 @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
112 public Response put(final ScannerModel model,
113 final @Context UriInfo uriInfo) {
114 if (LOG.isDebugEnabled()) {
115 LOG.debug("PUT " + uriInfo.getAbsolutePath());
116 }
117 return update(model, true, uriInfo);
118 }
119
120 @POST
121 @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
122 public Response post(final ScannerModel model,
123 final @Context UriInfo uriInfo) {
124 if (LOG.isDebugEnabled()) {
125 LOG.debug("POST " + uriInfo.getAbsolutePath());
126 }
127 return update(model, false, uriInfo);
128 }
129
130 @Path("{scanner: .+}")
131 public ScannerInstanceResource getScannerInstanceResource(
132 final @PathParam("scanner") String id) {
133 ScannerInstanceResource instance = scanners.get(id);
134 if (instance == null) {
135 throw new WebApplicationException(Response.Status.NOT_FOUND);
136 }
137 return instance;
138 }
139 }