1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.hicc;
20
21
22 import java.net.*;
23 import java.io.*;
24 import org.json.*;
25 import org.apache.log4j.Logger;
26 import org.apache.hadoop.chukwa.util.ExceptionUtil;
27
28 public class JSONLoader {
29 public JSONArray jsonData;
30 private static Logger log = Logger.getLogger(JSONLoader.class);
31
32 static public String getContents(String source) {
33
34 StringBuffer contents = new StringBuffer();
35
36 try {
37
38
39 URL yahoo = new URL(source);
40 BufferedReader in = new BufferedReader(new InputStreamReader(yahoo
41 .openStream()));
42
43 String inputLine;
44
45 while ((inputLine = in.readLine()) != null) {
46 contents.append(inputLine);
47 contents.append(System.getProperty("line.separator"));
48 }
49 in.close();
50 } catch (IOException ex) {
51 ex.printStackTrace();
52 }
53
54 return contents.toString();
55 }
56
57 public JSONLoader(String source) {
58 String buffer = getContents(source);
59 try {
60 JSONObject rows = new JSONObject(buffer);
61 jsonData = new JSONArray(rows.get("rows").toString());
62 } catch (JSONException e) {
63 log.debug(ExceptionUtil.getStackTrace(e));
64 }
65 }
66
67 public String getTS(int i) {
68 String ts = null;
69 try {
70 ts = ((JSONObject) jsonData.get(i)).get("ts").toString();
71 } catch (JSONException e) {
72 log.debug(ExceptionUtil.getStackTrace(e));
73 }
74 return ts;
75 }
76
77 public String getTags(int i) {
78 String tags = null;
79 try {
80 tags = ((JSONObject) jsonData.get(i)).get("tags")
81 .toString();
82 } catch (JSONException e) {
83 log.debug(ExceptionUtil.getStackTrace(e));
84 }
85 return tags;
86 }
87
88 public String getValue(int i) {
89 String value = null;
90 try {
91 value = ((JSONObject) jsonData.get(i)).get("value")
92 .toString();
93 } catch (JSONException e) {
94 log.debug(ExceptionUtil.getStackTrace(e));
95 }
96 return value;
97 }
98
99 public int length() {
100 return jsonData.length();
101 }
102 }