1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import org.apache.hadoop.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.KeyValue;
23 import org.apache.hadoop.hbase.rest.ResourceBase;
24 import org.apache.hadoop.hbase.rest.RowSpec;
25 import org.apache.hadoop.hbase.rest.TableResource;
26 import org.apache.hadoop.hbase.rest.model.CellModel;
27 import org.apache.hadoop.hbase.rest.model.CellSetModel;
28 import org.apache.hadoop.hbase.rest.model.RowModel;
29
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriInfo;
36 import java.io.IOException;
37
38 @InterfaceAudience.Private
39 public class MultiRowResource extends ResourceBase {
40 public static final String ROW_KEYS_PARAM_NAME = "row";
41
42 TableResource tableResource;
43 Integer versions = null;
44
45
46
47
48
49
50
51
52 public MultiRowResource(TableResource tableResource, String versions) throws IOException {
53 super();
54 this.tableResource = tableResource;
55
56 if (versions != null) {
57 this.versions = Integer.valueOf(versions);
58
59 }
60 }
61
62 @GET
63 @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
64 MIMETYPE_PROTOBUF_IETF})
65 public Response get(final @Context UriInfo uriInfo) {
66 MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
67
68 servlet.getMetrics().incrementRequests(1);
69 try {
70 CellSetModel model = new CellSetModel();
71 for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
72 RowSpec rowSpec = new RowSpec(rk);
73
74 if (this.versions != null) {
75 rowSpec.setMaxVersions(this.versions);
76 }
77
78 ResultGenerator generator =
79 ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null);
80 if (!generator.hasNext()) {
81 return Response.status(Response.Status.NOT_FOUND)
82 .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
83 .build();
84 }
85
86 KeyValue value = null;
87 RowModel rowModel = new RowModel(rk);
88
89 while ((value = generator.next()) != null) {
90 rowModel.addCell(new CellModel(value.getFamily(), value.getQualifier(),
91 value.getTimestamp(), value.getValue()));
92 }
93
94 model.addRow(rowModel);
95 }
96 servlet.getMetrics().incrementSucessfulGetRequests(1);
97 return Response.ok(model).build();
98 } catch (IOException e) {
99 servlet.getMetrics().incrementFailedGetRequests(1);
100 return Response.status(Response.Status.SERVICE_UNAVAILABLE)
101 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
102 .build();
103 }
104
105 }
106 }