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         	if (rowKey == null || rowKey.equals("")) {
107         		continue;
108         	}
109             Series output = ChukwaHBaseStore.getSeries(table, rowKey, family, qualifier, startTime, endTime, true);
110             seriesList.add(output.toJSONObject());
111           }
112           buffer = seriesList.toString();
113       } else {
114         throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
115             .entity("No session attribute key defined.").build());
116       }
117     } catch (ParseException e) {
118       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
119           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());
120     }
121     return buffer;
122   }
123 
124   @GET
125   @Path("schema")
126   @Produces("application/json")
127   public String getTables() {
128     Set<String> tableNames = ChukwaHBaseStore.getTableNames();
129     JSONArray tables = new JSONArray();
130     for(String table : tableNames) {
131       tables.add(table);
132     }
133     return tables.toString();
134   }
135   
136   @GET
137   @Path("schema/{table}")
138   @Produces("application/json")
139   public String getFamilies(@PathParam("table") String tableName) {
140     Set<String> familyNames = ChukwaHBaseStore.getFamilyNames(tableName);
141     JSONArray families = new JSONArray();
142     for(String family : familyNames) {
143       families.add(family);
144     }
145     return families.toString();
146   }
147   
148   @GET
149   @Path("schema/{table}/{family}")
150   @Produces("application/json")
151   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) {
152     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
153     long startTime = 0;
154     long endTime = 0;
155     TimeHandler time = new TimeHandler(request);
156     try {
157       if(start!=null) {
158         startTime = sdf.parse(start).getTime();
159       } else {
160         startTime = time.getStartTime();
161       }
162       if(end!=null) {
163         endTime = sdf.parse(end).getTime();
164       } else {
165         endTime = time.getEndTime();
166       }
167     } catch(ParseException e) {
168       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
169           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());      
170     }
171     Set<String> columnNames = ChukwaHBaseStore.getColumnNames(tableName, family, startTime, endTime, fullScan);
172     JSONArray columns = new JSONArray();
173     for(String column : columnNames) {
174       columns.add(column);
175     }
176     return columns.toString();
177   }
178   
179   @GET
180   @Path("rowkey/{table}/{family}/{column}")
181   @Produces("application/json")
182   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) {
183     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
184     long startTime = 0;
185     long endTime = 0;
186     TimeHandler time = new TimeHandler(request);
187     try {
188       if(start!=null) {
189         startTime = sdf.parse(start).getTime();
190       } else {
191         startTime = time.getStartTime();
192       }
193       if(end!=null) {
194         endTime = sdf.parse(end).getTime();
195       } else {
196         endTime = time.getEndTime();
197       }
198     } catch(ParseException e) {
199       throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
200           .entity("Start/End date parse error.  Format: yyyyMMddHHmmss.").build());      
201     }
202     Set<String> columnNames = ChukwaHBaseStore.getRowNames(tableName, family, column, startTime, endTime, fullScan);
203     JSONArray rows = new JSONArray();
204     for(String row : columnNames) {
205       rows.add(row);
206     }
207     return rows.toString();
208   }
209 }