Java tutorial
/* * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package jp.sanix; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.zip.GZIPInputStream; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import java.util.TreeMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectSummary; public class Rawdata { public static final String BUCKET = "pvdata-storage-production"; public static final String PG_CON = "jdbc:postgresql://sanix-eye-rawdata-r01.cpoo4hhfnsex.ap-northeast-1.rds.amazonaws.com:5432/sanix_eye_rawdata_production"; public static final String PG_CON_LOCAL = "jdbc:postgresql://localhost:19000/sanix_eye_rawdata_production"; public static final String PG_USER = "sanix_eye_rawdat"; public static final String PG_PASS = "HFKbK_4whYj2"; public String getUniqueCode() { return uniqueCode; } public void setUniqueCode(String uniqueCode) { this.uniqueCode = uniqueCode; } private String id = null; private String uniqueCode = null; private Date date = new Date(); public Rawdata(String id, String uniqueCode) { super(); this.id = id; this.uniqueCode = uniqueCode; } public TreeMap<Date, JSONObject> get() throws IOException, SQLException, ParseException { TreeMap<Date, JSONObject> data = new TreeMap<Date, JSONObject>(); AmazonS3 s3 = new AmazonS3Client(); s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); Calendar cal = Calendar.getInstance(); cal.setTime(this.getDate()); /* AWS S3????? */ String bucketName = "pvdata-storage-production"; ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName) .withPrefix("data/" + this.getUniqueCode() + "/" + toDate(cal) + "/")); /* get data from s3 */ do { for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { String keyname = objectSummary.getKey(); S3Object object = s3.getObject(new GetObjectRequest(BUCKET, keyname)); BufferedReader reader = new BufferedReader( new InputStreamReader(new GZIPInputStream(object.getObjectContent()))); while (reader.ready()) { String line = reader.readLine(); int pos; try { pos = line.indexOf("{"); } catch (NullPointerException e) { continue; } try { String jsons = line.substring(pos); JSONObject json = new JSONObject(jsons); JSONArray arr = json.getJSONObject("data").getJSONArray("sample"); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); Date date = fmt.parse(obj.getString("time")); for (int j = 0; j < obj.getJSONArray("powcon").length(); j++) { if (obj.getJSONArray("powcon").getJSONObject(j).get("genKwh") != JSONObject.NULL) { Double genkwh; genkwh = obj.getJSONArray("powcon").getJSONObject(j).getDouble("genKwh"); JSONObject jobj; if (data.containsKey(date)) { jobj = data.get(date); } else { jobj = new JSONObject(); } if (genkwh != null) { jobj.put(String.valueOf(j), genkwh); } if (jobj.length() > 0) { data.put(date, jobj); } } } } } catch (JSONException e) { e.printStackTrace(); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); } } reader.close(); object.close(); } objectListing = s3.listNextBatchOfObjects(objectListing); } while (objectListing.getMarker() != null); Calendar today = Calendar.getInstance(); today.setTimeZone(TimeZone.getTimeZone("JST")); if (toDate(cal).equals(toDate(today))) { SimpleDateFormat pgfmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); pgfmt.setTimeZone(TimeZone.getTimeZone("UTC")); Calendar recent = Calendar.getInstance(); recent.setTimeZone(TimeZone.getTimeZone("UST")); recent.add(Calendar.HOUR, -2); Connection db; if (System.getProperty("user.name").toString().equals("ec2-user")) { db = DriverManager.getConnection(PG_CON, PG_USER, PG_PASS); } else { db = DriverManager.getConnection(PG_CON_LOCAL, PG_USER, PG_PASS); } Statement st = db.createStatement(); String sql = "SELECT data FROM data WHERE pvs_unique_code='" + this.getUniqueCode() + "' AND created_at > '" + pgfmt.format(recent.getTime()) + "';"; ResultSet rs = st.executeQuery(sql); while (rs.next()) { String jsons = rs.getString(1); try { JSONObject json = new JSONObject(jsons); JSONArray arr = json.getJSONObject("data").getJSONArray("sample"); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); Date date = fmt.parse(obj.getString("time")); for (int j = 0; j < obj.getJSONArray("powcon").length(); j++) { if (obj.getJSONArray("powcon").getJSONObject(j).get("genKwh") != JSONObject.NULL) { Double genkwh; genkwh = obj.getJSONArray("powcon").getJSONObject(j).getDouble("genKwh"); JSONObject jobj; if (data.containsKey(date)) { jobj = data.get(date); } else { jobj = new JSONObject(); } if (genkwh != null) { jobj.put(String.valueOf(j), genkwh); } if (jobj.length() > 0) { data.put(date, jobj); } } } } } catch (JSONException e) { e.printStackTrace(); } } rs.close(); db.close(); } return data; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public static String toDate(Calendar cal) { return (String.format("%04d/%02d/%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))); } }