jp.sanix.analysisStaging.java Source code

Java tutorial

Introduction

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

Source

/*
 * 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.File;
import java.sql.SQLException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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 org.json.JSONException;
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 analysisStaging {

    public static final String BUCKET = "sanix-data-analysis";
    public static final String FOLDER = "fhRK0XGVb3cR1r1S3x9j3j3DRFGUyRYC/";

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

        //      if (args.length < 1) {
        //         System.out.println("Please specify at least an arguments that is pvs_serial_id.");
        //         System.out.println("analysis A9990004 // get today's data");
        //         System.out.println("analysis A9990004 2015/05/30 // get from 2015/05/30 to today's data");
        //         System.out.println("analysis A9990004 2015/05/30 2015/06/30");
        //         System.exit(-1);
        //      }

        //      String id = args[0];
        String uniq = "763ff280b16c61f524ae31b33401bcd6";

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        format.setTimeZone(TimeZone.getTimeZone("JST"));
        SimpleDateFormat normalformat = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
        normalformat.setTimeZone(TimeZone.getTimeZone("JST"));
        SimpleDateFormat pgformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        pgformat.setTimeZone(TimeZone.getTimeZone("UTC"));

        Calendar cal = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        cal.setTime(format.parse("2015/11/23"));
        end.setTime(format.parse("2015/11/23"));

        System.out.println(uniq);
        try {
            cal.setTime(format.parse(args[1]));
        } catch (ParseException e) {
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        try {
            end.setTime(format.parse(args[2]));
        } catch (ParseException e) {
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        end.add(Calendar.DAY_OF_MONTH, 1);

        AmazonS3 s3 = new AmazonS3Client();

        s3.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1));

        String bucketName = "pvdata-storage-staging";

        while (cal.before(end)) {
            System.out.println("Getting data of " + toDate(cal));

            /* AWS S3????? */
            //         String bucketName = "pvdata-storage-production";
            System.out.println("Get s3 data by key='" + bucketName + "/data/" + uniq + "/" + toDate(cal) + "/'");
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)
                    .withPrefix("data/" + uniq + "/" + toDate(cal) + "/"));

            /* get data from s3 */
            xlsSheet xls = null;
            do {
                Boolean first = true;
                for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                    String keyname = objectSummary.getKey();
                    S3Object object = s3.getObject(new GetObjectRequest(bucketName, keyname));
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(new GZIPInputStream(object.getObjectContent())));
                    while (reader.ready()) {
                        String line = reader.readLine();
                        try {
                            String json = line.substring(line.indexOf("{"));
                            if (first) {
                                xls = new xlsSheet(json);
                                first = false;
                            } else {
                                xls.putData(json);
                            }
                        } catch (NullPointerException e) {
                        }
                    }
                    reader.close();
                    object.close();
                }
                objectListing = s3.listNextBatchOfObjects(objectListing);
            } while (objectListing.getMarker() != null);

            System.out.println("Write Buffer");
            xls.writeBuffer();

            File file = File.createTempFile("temp", ".xlsx");
            file.deleteOnExit();
            xls.putFile(new FileOutputStream(file));
            System.out.println("Put S3");
            s3.putObject(new PutObjectRequest("sanix-data-analysis",
                    FOLDER + uniq + "-" + toDate(cal).replace("/", "-") + ".xlsx", file));
            System.out.println("Finished: " + toDate(cal));

            cal.add(Calendar.DAY_OF_MONTH, 1);
        }

    }

    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)));
    }

    public static Date parseDate(String sDate) {

        Date myDate = new Date();

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        format1.setTimeZone(TimeZone.getTimeZone("UST"));
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+09:00'");
        format2.setTimeZone(TimeZone.getTimeZone("JST"));

        try {
            myDate = format1.parse(sDate);
        } catch (ParseException e) {
            try {
                myDate = format2.parse(sDate);
            } catch (ParseException e1) {
            }
        }
        return myDate;
    }
}