View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  import java.util.Map;
24  
25  import javax.ws.rs.Consumes;
26  import javax.ws.rs.DELETE;
27  import javax.ws.rs.GET;
28  import javax.ws.rs.POST;
29  import javax.ws.rs.PUT;
30  import javax.ws.rs.Produces;
31  import javax.ws.rs.core.CacheControl;
32  import javax.ws.rs.core.Context;
33  import javax.ws.rs.core.Response;
34  import javax.ws.rs.core.Response.ResponseBuilder;
35  import javax.ws.rs.core.UriInfo;
36  import javax.xml.namespace.QName;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.hadoop.hbase.HColumnDescriptor;
41  import org.apache.hadoop.hbase.HTableDescriptor;
42  import org.apache.hadoop.hbase.TableExistsException;
43  import org.apache.hadoop.hbase.TableName;
44  import org.apache.hadoop.hbase.TableNotEnabledException;
45  import org.apache.hadoop.hbase.TableNotFoundException;
46  import org.apache.hadoop.hbase.classification.InterfaceAudience;
47  import org.apache.hadoop.hbase.client.Admin;
48  import org.apache.hadoop.hbase.client.Table;
49  import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
50  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
51  
52  @InterfaceAudience.Private
53  public class SchemaResource extends ResourceBase {
54    private static final Log LOG = LogFactory.getLog(SchemaResource.class);
55  
56    static CacheControl cacheControl;
57    static {
58      cacheControl = new CacheControl();
59      cacheControl.setNoCache(true);
60      cacheControl.setNoTransform(false);
61    }
62  
63    TableResource tableResource;
64  
65    /**
66     * Constructor
67     * @param tableResource
68     * @throws IOException
69     */
70    public SchemaResource(TableResource tableResource) throws IOException {
71      super();
72      this.tableResource = tableResource;
73    }
74  
75    private HTableDescriptor getTableSchema() throws IOException,
76        TableNotFoundException {
77      Table table = servlet.getTable(tableResource.getName());
78      try {
79        return table.getTableDescriptor();
80      } finally {
81        table.close();
82      }
83    }
84  
85    @GET
86    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
87      MIMETYPE_PROTOBUF_IETF})
88    public Response get(final @Context UriInfo uriInfo) {
89      if (LOG.isTraceEnabled()) {
90        LOG.trace("GET " + uriInfo.getAbsolutePath());
91      }
92      servlet.getMetrics().incrementRequests(1);
93      try {
94        ResponseBuilder response =
95          Response.ok(new TableSchemaModel(getTableSchema()));
96        response.cacheControl(cacheControl);
97        servlet.getMetrics().incrementSucessfulGetRequests(1);
98        return response.build();
99      } catch (Exception e) {
100       servlet.getMetrics().incrementFailedGetRequests(1);
101       return processException(e);
102     }
103   }
104 
105   private Response replace(final TableName name, final TableSchemaModel model,
106       final UriInfo uriInfo, final Admin admin) {
107     if (servlet.isReadOnly()) {
108       return Response.status(Response.Status.FORBIDDEN)
109         .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
110         .build();
111     }
112     try {
113       HTableDescriptor htd = new HTableDescriptor(name);
114       for (Map.Entry<QName,Object> e: model.getAny().entrySet()) {
115         htd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
116       }
117       for (ColumnSchemaModel family: model.getColumns()) {
118         HColumnDescriptor hcd = new HColumnDescriptor(family.getName());
119         for (Map.Entry<QName,Object> e: family.getAny().entrySet()) {
120           hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
121         }
122         htd.addFamily(hcd);
123       }
124       if (admin.tableExists(name)) {
125         admin.disableTable(name);
126         admin.modifyTable(name, htd);
127         admin.enableTable(name);
128         servlet.getMetrics().incrementSucessfulPutRequests(1);
129       } else try {
130         admin.createTable(htd);
131         servlet.getMetrics().incrementSucessfulPutRequests(1);
132       } catch (TableExistsException e) {
133         // race, someone else created a table with the same name
134         return Response.status(Response.Status.NOT_MODIFIED)
135           .type(MIMETYPE_TEXT).entity("Not modified" + CRLF)
136           .build();
137       }
138       return Response.created(uriInfo.getAbsolutePath()).build();
139     } catch (Exception e) {
140       servlet.getMetrics().incrementFailedPutRequests(1);
141       return processException(e);
142     }
143   }
144 
145   private Response update(final TableName name, final TableSchemaModel model,
146       final UriInfo uriInfo, final Admin admin) {
147     if (servlet.isReadOnly()) {
148       return Response.status(Response.Status.FORBIDDEN)
149         .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
150         .build();
151     }
152     try {
153       HTableDescriptor htd = admin.getTableDescriptor(name);
154       admin.disableTable(name);
155       try {
156         for (ColumnSchemaModel family: model.getColumns()) {
157           HColumnDescriptor hcd = new HColumnDescriptor(family.getName());
158           for (Map.Entry<QName,Object> e: family.getAny().entrySet()) {
159             hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
160           }
161           if (htd.hasFamily(hcd.getName())) {
162             admin.modifyColumn(name, hcd);
163           } else {
164             admin.addColumn(name, hcd);
165           }
166         }
167       } catch (IOException e) {
168         return Response.status(Response.Status.SERVICE_UNAVAILABLE)
169           .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
170           .build();
171       } finally {
172         admin.enableTable(TableName.valueOf(tableResource.getName()));
173       }
174       servlet.getMetrics().incrementSucessfulPutRequests(1);
175       return Response.ok().build();
176     } catch (Exception e) {
177       servlet.getMetrics().incrementFailedPutRequests(1);
178       return processException(e);
179     }
180   }
181 
182   private Response update(final TableSchemaModel model, final boolean replace,
183       final UriInfo uriInfo) {
184     try {
185       TableName name = TableName.valueOf(tableResource.getName());
186       Admin admin = servlet.getAdmin();
187       if (replace || !admin.tableExists(name)) {
188         return replace(name, model, uriInfo, admin);
189       } else {
190         return update(name, model, uriInfo, admin);
191       }
192     } catch (Exception e) {
193       servlet.getMetrics().incrementFailedPutRequests(1);
194       return processException(e);
195     }
196   }
197 
198   @PUT
199   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
200     MIMETYPE_PROTOBUF_IETF})
201   public Response put(final TableSchemaModel model,
202       final @Context UriInfo uriInfo) {
203     if (LOG.isTraceEnabled()) {
204       LOG.trace("PUT " + uriInfo.getAbsolutePath());
205     }
206     servlet.getMetrics().incrementRequests(1);
207     return update(model, true, uriInfo);
208   }
209 
210   @POST
211   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
212     MIMETYPE_PROTOBUF_IETF})
213   public Response post(final TableSchemaModel model,
214       final @Context UriInfo uriInfo) {
215     if (LOG.isTraceEnabled()) {
216       LOG.trace("PUT " + uriInfo.getAbsolutePath());
217     }
218     servlet.getMetrics().incrementRequests(1);
219     return update(model, false, uriInfo);
220   }
221 
222   @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DE_MIGHT_IGNORE",
223       justification="Expected")
224   @DELETE
225   public Response delete(final @Context UriInfo uriInfo) {
226     if (LOG.isTraceEnabled()) {
227       LOG.trace("DELETE " + uriInfo.getAbsolutePath());
228     }
229     servlet.getMetrics().incrementRequests(1);
230     if (servlet.isReadOnly()) {
231       return Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT)
232           .entity("Forbidden" + CRLF).build();
233     }
234     try {
235       Admin admin = servlet.getAdmin();
236       try {
237         admin.disableTable(TableName.valueOf(tableResource.getName()));
238       } catch (TableNotEnabledException e) { /* this is what we want anyway */ }
239       admin.deleteTable(TableName.valueOf(tableResource.getName()));
240       servlet.getMetrics().incrementSucessfulDeleteRequests(1);
241       return Response.ok().build();
242     } catch (Exception e) {
243       servlet.getMetrics().incrementFailedDeleteRequests(1);
244       return processException(e);
245     }
246   }
247 }