This project has retired. For details please refer to its Attic page.
JSONLoader xref
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  
19  package org.apache.hadoop.chukwa.hicc;
20  
21  
22  import java.net.*;
23  import java.nio.charset.Charset;
24  import java.text.ParseException;
25  import java.io.*;
26  
27  import org.json.simple.JSONArray;
28  import org.json.simple.JSONObject;
29  import org.json.simple.JSONValue;
30  import org.apache.log4j.Logger;
31  import org.apache.hadoop.chukwa.util.ExceptionUtil;
32  
33  public class JSONLoader {
34    public JSONArray jsonData;
35    private static Logger log = Logger.getLogger(JSONLoader.class);
36  
37    static public String getContents(String source) {
38      // ...checks on aFile are elided
39      StringBuffer contents = new StringBuffer();
40  
41      try {
42        // use buffering, reading one line at a time
43        // FileReader always assumes default encoding is OK!
44        URL yahoo = new URL(source);
45        BufferedReader in = new BufferedReader(new InputStreamReader(yahoo
46            .openStream(), Charset.forName("UTF-8")));
47  
48        String inputLine;
49  
50        while ((inputLine = in.readLine()) != null) {
51          contents.append(inputLine);
52          contents.append(System.getProperty("line.separator"));
53        }
54        in.close();
55      } catch (IOException ex) {
56        ex.printStackTrace();
57      }
58  
59      return contents.toString();
60    }
61  
62    public JSONLoader(String source) {
63      String buffer = getContents(source);
64      try {
65        JSONObject rows = (JSONObject) JSONValue.parse(buffer);
66        jsonData = (JSONArray) JSONValue.parse(rows.get("rows").toString());
67      } catch (Exception e) {
68        log.debug(ExceptionUtil.getStackTrace(e)); 
69      }
70    }
71  
72    public String getTS(int i) {
73      String ts = null;
74      try {
75        ts = ((JSONObject) jsonData.get(i)).get("ts").toString();
76      } catch (Exception e) {
77        log.debug(ExceptionUtil.getStackTrace(e)); 
78      }
79      return ts;
80    }
81  
82    public String getTags(int i) {
83      String tags = null;
84      try {
85        tags = ((JSONObject) jsonData.get(i)).get("tags")
86            .toString();
87      } catch (Exception e) {
88        log.debug(ExceptionUtil.getStackTrace(e)); 
89      }
90      return tags;
91    }
92  
93    public String getValue(int i) {
94      String value = null;
95      try {
96        value = ((JSONObject) jsonData.get(i)).get("value")
97            .toString();
98      } catch (Exception e) {
99        log.debug(ExceptionUtil.getStackTrace(e)); 
100     }
101     return value;
102   }
103 
104   public int length() {
105     return jsonData.size();
106   }
107 }