1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest;
21
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 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 KeyValue value = null;
86 RowModel rowModel = new RowModel(rk);
87
88 while ((value = generator.next()) != null) {
89 rowModel.addCell(new CellModel(value.getFamily(), value.getQualifier(),
90 value.getTimestamp(), value.getValue()));
91 }
92
93 model.addRow(rowModel);
94 }
95 servlet.getMetrics().incrementSucessfulGetRequests(1);
96 return Response.ok(model).build();
97 } catch (IOException e) {
98 servlet.getMetrics().incrementFailedGetRequests(1);
99 return Response.status(Response.Status.SERVICE_UNAVAILABLE)
100 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
101 .build();
102 }
103
104 }
105 }