OrangeSignalCSVを使ってAmazonの請求情報CSVをパースする

String S3_BUCKET_BILLING_CSV = "my.bucket.name";
String KEY_LINKED_ACCOUNT_ID = "LinkedAccountId";
String KEY_RECORD_TYPE = "RecordType";
String KEY_TOTAL_COST = "TotalCost";
String RECORD_TYPE_ACCOUNT_TOTAL = "AccountTotal";

String paymentAccountId = accountIds[0];
String bucketName = S3_BUCKET_BILLING_CSV;
String fileName = String.format("%s-aws-billing-csv-%s.csv",
    paymentAccountId, thisMonth);

double currentBillingValue = 0;
AWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY,
    SECRET_ACCESS_KEY);

AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);
S3Object csvObject = s3Client.getObject(bucketName, fileName);

BufferedReader csvReader = new BufferedReader(new InputStreamReader(
    csvObject.getObjectContent()));
try {
  CsvConfig csvConfig = new CsvConfig(',', '"', '"');
  List<Map<String, String>> list = Csv.load(csvReader,
      csvConfig, new ColumnNameMapListHandler());
  for (Map<String, String> csvLine : list) {
    if (csvLine.containsKey(KEY_RECORD_TYPE)) {
      String recordType = csvLine.get(KEY_RECORD_TYPE);
      if (recordType != null) {
        recordType = recordType.replaceAll("^\"|\"$", "");
      }
      if (RECORD_TYPE_ACCOUNT_TOTAL.equals(recordType)) {
        if (csvLine.containsKey(KEY_LINKED_ACCOUNT_ID)) {
          if (csvLine.containsKey(KEY_TOTAL_COST)) {
            double cost = 0.0;
            try {
              String csvCost = csvLine.get(KEY_TOTAL_COST);
              cost = Double.parseDouble(csvCost.replaceAll(
                  "^\"|\"$", ""));
            } catch (Exception e) {
              cost = 0.0;
            }
            System.out.println(cost);
          }
        }
      }
    }
  }
} finally {
  csvReader.close();
}