This project has retired. For details please refer to its
Attic page.
JSONLoader xref
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.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
39 StringBuffer contents = new StringBuffer();
40
41 try {
42
43
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 }