CreateDataset 搭配 使用 AWS SDK - Amazon Forecast

Amazon Forecast 不再提供給新客戶。Amazon Forecast 的現有客戶可以繼續正常使用服務。進一步了解"

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateDataset 搭配 使用 AWS SDK

下列程式碼範例示範如何使用 CreateDataset

Java
SDK 適用於 Java 2.x
注意

還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateDataSet { public static void main(String[] args) { final String usage = """ Usage: <name>\s Where: name - The name of the data set.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String name = args[0]; 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 data set is " + myDataSetARN); forecast.close(); } public static String createForecastDataSet(ForecastClient forecast, String name) { try { 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 data set. 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; } }
  • 如需API詳細資訊,請參閱 AWS SDK for Java 2.x API 參考CreateDataset中的 。

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 搭配 AWS SDK 使用 Forecast。本主題也包含入門的相關資訊,以及先前SDK版本的詳細資訊。