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 java.io.IOException;
22
23 import javax.ws.rs.GET;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.UriInfo;
29
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.Cell;
32 import org.apache.hadoop.hbase.CellUtil;
33 import org.apache.hadoop.hbase.rest.model.CellModel;
34 import org.apache.hadoop.hbase.rest.model.CellSetModel;
35 import org.apache.hadoop.hbase.rest.model.RowModel;
36
37 @InterfaceAudience.Private
38 public class MultiRowResource extends ResourceBase {
39 public static final String ROW_KEYS_PARAM_NAME = "row";
40
41 TableResource tableResource;
42 Integer versions = null;
43
44
45
46
47
48
49
50
51 public MultiRowResource(TableResource tableResource, String versions) throws IOException {
52 super();
53 this.tableResource = tableResource;
54
55 if (versions != null) {
56 this.versions = Integer.valueOf(versions);
57
58 }
59 }
60
61 @GET
62 @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
63 MIMETYPE_PROTOBUF_IETF})
64 public Response get(final @Context UriInfo uriInfo) {
65 MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
66
67 servlet.getMetrics().incrementRequests(1);
68 try {
69 CellSetModel model = new CellSetModel();
70 for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
71 RowSpec rowSpec = new RowSpec(rk);
72
73 if (this.versions != null) {
74 rowSpec.setMaxVersions(this.versions);
75 }
76
77 ResultGenerator generator =
78 ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null);
79 if (!generator.hasNext()) {
80 return Response.status(Response.Status.NOT_FOUND)
81 .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
82 .build();
83 }
84
85 Cell value = null;
86 RowModel rowModel = new RowModel(rk);
87
88 while ((value = generator.next()) != null) {
89 rowModel.addCell(new CellModel(CellUtil.cloneFamily(value),
90 CellUtil.cloneQualifier(value),
91 value.getTimestamp(), CellUtil.cloneValue(value)));
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 }