001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.rest; 021 022import javax.ws.rs.GET; 023import javax.ws.rs.Produces; 024import javax.ws.rs.core.CacheControl; 025import javax.ws.rs.core.Context; 026import javax.ws.rs.core.Response; 027import javax.ws.rs.core.Response.ResponseBuilder; 028import javax.ws.rs.core.UriInfo; 029import java.io.IOException; 030import java.util.List; 031 032import org.apache.hadoop.hbase.MetaTableAccessor; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.TableNotFoundException; 036import org.apache.hadoop.hbase.client.Connection; 037import org.apache.hadoop.hbase.client.ConnectionFactory; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.rest.model.TableInfoModel; 040import org.apache.hadoop.hbase.rest.model.TableRegionModel; 041import org.apache.hadoop.hbase.util.Pair; 042import org.apache.yetus.audience.InterfaceAudience; 043import org.slf4j.Logger; 044import org.slf4j.LoggerFactory; 045 046@InterfaceAudience.Private 047public class RegionsResource extends ResourceBase { 048 private static final Logger LOG = LoggerFactory.getLogger(RegionsResource.class); 049 050 static CacheControl cacheControl; 051 static { 052 cacheControl = new CacheControl(); 053 cacheControl.setNoCache(true); 054 cacheControl.setNoTransform(false); 055 } 056 057 TableResource tableResource; 058 059 /** 060 * Constructor 061 * @param tableResource 062 * @throws IOException 063 */ 064 public RegionsResource(TableResource tableResource) throws IOException { 065 super(); 066 this.tableResource = tableResource; 067 } 068 069 @GET 070 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, 071 MIMETYPE_PROTOBUF_IETF}) 072 public Response get(final @Context UriInfo uriInfo) { 073 if (LOG.isTraceEnabled()) { 074 LOG.trace("GET " + uriInfo.getAbsolutePath()); 075 } 076 servlet.getMetrics().incrementRequests(1); 077 try { 078 TableName tableName = TableName.valueOf(tableResource.getName()); 079 TableInfoModel model = new TableInfoModel(tableName.getNameAsString()); 080 081 Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration()); 082 List<Pair<RegionInfo, ServerName>> regions = MetaTableAccessor 083 .getTableRegionsAndLocations(connection, tableName); 084 connection.close(); 085 for (Pair<RegionInfo,ServerName> e: regions) { 086 RegionInfo hri = e.getFirst(); 087 ServerName addr = e.getSecond(); 088 model.add( 089 new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(), 090 hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort())); 091 } 092 ResponseBuilder response = Response.ok(model); 093 response.cacheControl(cacheControl); 094 servlet.getMetrics().incrementSucessfulGetRequests(1); 095 return response.build(); 096 } catch (TableNotFoundException e) { 097 servlet.getMetrics().incrementFailedGetRequests(1); 098 return Response.status(Response.Status.NOT_FOUND) 099 .type(MIMETYPE_TEXT).entity("Not found" + CRLF) 100 .build(); 101 } catch (IOException e) { 102 servlet.getMetrics().incrementFailedGetRequests(1); 103 return Response.status(Response.Status.SERVICE_UNAVAILABLE) 104 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF) 105 .build(); 106 } 107 } 108}