View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.hicc.rest;
19  
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Set;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  import javax.ws.rs.DefaultValue;
27  import javax.ws.rs.GET;
28  import javax.ws.rs.Path;
29  import javax.ws.rs.PathParam;
30  import javax.ws.rs.QueryParam;
31  import javax.ws.rs.Produces;
32  import javax.ws.rs.WebApplicationException;
33  import javax.ws.rs.core.Context;
34  import javax.ws.rs.core.Response;
35  
36  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
37  import org.apache.hadoop.chukwa.hicc.TimeHandler;
38  import org.apache.hadoop.chukwa.hicc.bean.Series;
39  import org.json.simple.JSONArray;
40  
41  @Path("/metrics")
42  public class MetricsController {
43  
44    @GET
45    @Path("series/{table}/{family}/{column}/rowkey/{rkey}")
46    @Produces("application/json")
47    public String getSeries(@Context HttpServletRequest request, @PathParam("table") String table, @PathParam("family") String family, @PathParam("column") String column, @PathParam("rkey") String rkey, @QueryParam("start") String start, @QueryParam("end") String end) {
48      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
49      String buffer = "";
50      Series series;
51      long startTime = 0;
52      long endTime = 0;
53      TimeHandler time = new TimeHandler(request);
54      try {
55        if(start!=null) {
56          startTime = sdf.parse(start).getTime();
57        } else {
58          startTime = time.getStartTime();
59        }
60        if(end!=null) {
61          endTime = sdf.parse(end).getTime();
62        } else {
63          endTime = time.getEndTime();
64        }
65        if(rkey!=null) {
66          series = ChukwaHBaseStore.getSeries(table, rkey, family, column, startTime, endTime, true);
67          buffer = series.toString();
68        } else {
69          throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
70              .entity("No row key defined.").build());
71        }
72      } catch (ParseException e) {
73        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
74            .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());
75      }
76      return buffer;
77    }
78  
79    @GET
80    @Path("series/{table}/{column}/session/{sessionKey}")
81    @Produces("application/json")
82    public String getSeriesBySessionAttribute(@Context HttpServletRequest request, @PathParam("table") String table, @PathParam("column") String column, @PathParam("sessionKey") String skey, @QueryParam("start") String start, @QueryParam("end") String end) {
83      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
84      String buffer = "";
85      long startTime = 0;
86      long endTime = 0;
87      TimeHandler time = new TimeHandler(request);
88      String family = column.split(":")[0];
89      String qualifier = column.split(":")[1];
90      try {
91        if(start!=null) {
92          startTime = sdf.parse(start).getTime();
93        } else {
94          startTime = time.getStartTime();
95        }
96        if(end!=null) {
97          endTime = sdf.parse(end).getTime();
98        } else {
99          endTime = time.getEndTime();
100       }
101       if(skey!=null) {
102           HttpSession session = request.getSession();
103           String[] rkeys = (session.getAttribute(skey).toString()).split(",");
104           JSONArray seriesList = new JSONArray();
105           for(String rowKey : rkeys) {
106             Series output = ChukwaHBaseStore.getSeries(table, rowKey, family, qualifier, startTime, endTime, true);
107             seriesList.add(output.toJSONObject());
108           }
109           buffer = seriesList.toString();
110       } else {
111         throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
112             .entity("No session attribute key defined.").build());
113       }
114     } catch (ParseException e) {
115       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
116           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());
117     }
118     return buffer;
119   }
120 
121   @GET
122   @Path("schema")
123   @Produces("application/json")
124   public String getTables() {
125     Set<String> tableNames = ChukwaHBaseStore.getTableNames();
126     JSONArray tables = new JSONArray();
127     for(String table : tableNames) {
128       tables.add(table);
129     }
130     return tables.toString();
131   }
132   
133   @GET
134   @Path("schema/{table}")
135   @Produces("application/json")
136   public String getFamilies(@PathParam("table") String tableName) {
137     Set<String> familyNames = ChukwaHBaseStore.getFamilyNames(tableName);
138     JSONArray families = new JSONArray();
139     for(String family : familyNames) {
140       families.add(family);
141     }
142     return families.toString();
143   }
144   
145   @GET
146   @Path("schema/{table}/{family}")
147   @Produces("application/json")
148   public String getColumnNames(@Context HttpServletRequest request, @PathParam("table") String tableName, @PathParam("family") String family, @QueryParam("start") String start, @QueryParam("end") String end, @DefaultValue("false") @QueryParam("fullScan") boolean fullScan) {
149     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
150     long startTime = 0;
151     long endTime = 0;
152     TimeHandler time = new TimeHandler(request);
153     try {
154       if(start!=null) {
155         startTime = sdf.parse(start).getTime();
156       } else {
157         startTime = time.getStartTime();
158       }
159       if(end!=null) {
160         endTime = sdf.parse(end).getTime();
161       } else {
162         endTime = time.getEndTime();
163       }
164     } catch(ParseException e) {
165       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
166           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());      
167     }
168     Set<String> columnNames = ChukwaHBaseStore.getColumnNames(tableName, family, startTime, endTime, fullScan);
169     JSONArray columns = new JSONArray();
170     for(String column : columnNames) {
171       columns.add(column);
172     }
173     return columns.toString();
174   }
175   
176   @GET
177   @Path("rowkey/{table}/{family}/{column}")
178   @Produces("application/json")
179   public String getRowNames(@Context HttpServletRequest request, @PathParam("table") String tableName, @PathParam("family") String family, @PathParam("column") String column, @QueryParam("start") String start, @QueryParam("end") String end, @QueryParam("fullScan") @DefaultValue("false") boolean fullScan) {
180     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
181     long startTime = 0;
182     long endTime = 0;
183     TimeHandler time = new TimeHandler(request);
184     try {
185       if(start!=null) {
186         startTime = sdf.parse(start).getTime();
187       } else {
188         startTime = time.getStartTime();
189       }
190       if(end!=null) {
191         endTime = sdf.parse(end).getTime();
192       } else {
193         endTime = time.getEndTime();
194       }
195     } catch(ParseException e) {
196       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
197           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());      
198     }
199     Set<String> columnNames = ChukwaHBaseStore.getRowNames(tableName, family, column, startTime, endTime, fullScan);
200     JSONArray rows = new JSONArray();
201     for(String row : columnNames) {
202       rows.add(row);
203     }
204     return rows.toString();
205   }
206 }