jp.sanix.weatherData.java Source code

Java tutorial

Introduction

Here is the source code for jp.sanix.weatherData.java

Source

package jp.sanix;
/*
 * 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.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.amazonaws.AmazonServiceException;
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.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class weatherData {

    public static final String STORE_PATH = "fhRK0XGVb3cR1r1S3x9j3j3DRFGUyRYC/wheather-data/";

    public static void main(String[] args) throws IOException, SQLException, AmazonServiceException, JSONException,
            NullPointerException, ParseException {

        AmazonS3 s3 = new AmazonS3Client();
        s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1));

        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(new File("src/main/web/?.csv")), "SJIS"));

        String line = null;
        br.readLine(); // 1???
        while ((line = br.readLine()) != null) {

            String[] col = line.split(",");

            /* AWS S3????? */
            String bucketName = "weather-forecast";
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)
                    .withPrefix("day_insolation/" + col[0] + "/"));

            File file = File.createTempFile("temp", ".csv");
            file.deleteOnExit();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "SJIS"));
            bw.write(
                    ",?,?,???,,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23\n");

            System.out.println(col[0] + ":" + col[1] + col[2]);
            /* get data from s3 */
            int i = 0;
            do {
                for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                    String keyname = objectSummary.getKey();
                    S3Object object = s3.getObject(new GetObjectRequest(bucketName, keyname));
                    BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
                    StringBuilder sb = new StringBuilder();
                    String line2 = null;
                    System.out.print(String.valueOf(i++) + "\r");
                    while ((line2 = reader.readLine()) != null) {
                        sb.append(line2);
                    }
                    reader.close();
                    object.close();
                    try {
                        JSONObject json = new JSONObject(sb.toString());
                        bw.write(String.join(",", col) + "," + json.get("date").toString().replace("-", "/") + ",");
                        JSONArray jarr = json.getJSONArray("hour_data");
                        bw.write(jarr.join(",") + "\n");
                        //                  System.out.println(String.join(",", col) + "," + json.get("date").toString().replace("-", "/") + "," + jarr.join(","));
                    } catch (JSONException e) {
                        //                  System.exit(1);
                    }
                }
                objectListing = s3.listNextBatchOfObjects(objectListing);
            } while (objectListing.getMarker() != null);

            bw.flush();
            bw.close();
            if (i > 0) {
                s3.putObject(new PutObjectRequest("sanix-data-analysis",
                        STORE_PATH + col[1] + col[2] + "_insolation.csv", file));
            }
        }
        br.close();

        br = new BufferedReader(new InputStreamReader(
                new FileInputStream(new File("src/main/web/?.csv")), "SJIS"));
        br.readLine(); // 1???
        while ((line = br.readLine()) != null) {

            String[] col = line.split(",");

            /* AWS S3????? */
            String bucketName = "weather-forecast";
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)
                    .withPrefix("day_temperature/" + col[0] + "/"));

            File file = File.createTempFile("temp", ".csv");
            file.deleteOnExit();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "SJIS"));
            bw.write(
                    ",?,?,???,,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23\n");

            System.out.println(col[0] + ":" + col[1] + col[2]);
            /* get data from s3 */
            int i = 0;
            do {
                for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                    String keyname = objectSummary.getKey();
                    S3Object object = s3.getObject(new GetObjectRequest(bucketName, keyname));
                    BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
                    StringBuilder sb = new StringBuilder();
                    String line2 = null;
                    System.out.print(String.valueOf(i++) + "\r");
                    while ((line2 = reader.readLine()) != null) {
                        sb.append(line2);
                    }
                    reader.close();
                    object.close();
                    try {
                        JSONObject json = new JSONObject(sb.toString());
                        bw.write(String.join(",", col) + "," + json.get("date").toString().replace("-", "/") + ",");
                        JSONArray jarr = json.getJSONArray("hour_data");
                        bw.write(jarr.join(",") + "\n");
                        //                  System.out.println(String.join(",", col) + "," + json.get("date").toString().replace("-", "/") + "," + jarr.join(","));
                    } catch (JSONException e) {
                        //                  System.exit(1);
                    }
                }
                objectListing = s3.listNextBatchOfObjects(objectListing);
            } while (objectListing.getMarker() != null);

            bw.flush();
            bw.close();
            if (i > 0) {
                s3.putObject(new PutObjectRequest("sanix-data-analysis",
                        STORE_PATH + col[1] + col[2] + "_temperture.csv", file));
            }
        }
        br.close();
    }
}