CreateDataSet.java - AWS Code Sample

CreateDataSet.java

CreateDataSet.java demonstrates how to create a dataset for the Amazon Forecast service.

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is 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/ 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 com.example.forecast; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.CreateDatasetRequest; import software.amazon.awssdk.services.forecast.model.Schema; import software.amazon.awssdk.services.forecast.model.SchemaAttribute; import software.amazon.awssdk.services.forecast.model.CreateDatasetResponse; import software.amazon.awssdk.services.forecast.model.ForecastException; import java.util.ArrayList; import java.util.List; public class CreateDataSet { public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateDataSet <name> \n\n" + "Where:\n" + " name - the name of the dataset \n\n" + "Example:\n" + " CreateDataSet MyDataSet\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String name = args[0]; // Create a Forecast client Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); String myDataSetARN = createForecastDataSet(forecast, name); System.out.println("The ARN of the new dataset is "+myDataSetARN) ; } public static String createForecastDataSet(ForecastClient forecast, String name) { try { // Create a Schema object, required for the dataset Schema schema = Schema.builder() .attributes(getSchema()) .build(); CreateDatasetRequest datasetRequest = CreateDatasetRequest.builder() .datasetName(name) .domain("CUSTOM") .datasetType("RELATED_TIME_SERIES") .dataFrequency("D") .schema(schema) .build(); CreateDatasetResponse response = forecast.createDataset(datasetRequest); return response.datasetArn(); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return "" ; } // Create a SchemaAttribute list, required to create a dataset private static List<SchemaAttribute> getSchema() { List<SchemaAttribute> schemaList = new ArrayList(); SchemaAttribute att1 = SchemaAttribute.builder() .attributeName("item_id") .attributeType("string") .build(); SchemaAttribute att2 = SchemaAttribute.builder() .attributeName("timestamp") .attributeType("timestamp") .build(); SchemaAttribute att3 = SchemaAttribute.builder() .attributeName("target_value") .attributeType("float") .build(); // Push the SchemaAttribute objects to the List schemaList.add(att1); schemaList.add(att2); schemaList.add(att3); return schemaList; } }

Sample Details

Service: Amazon Forecast

Last tested: 5/4/2020

Author: scmacdon-aws

Type: full-example