使用 SDK for Java 2.x 的遷移中樞範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

使用 SDK for Java 2.x 的遷移中樞範例

下列程式碼範例示範如何搭配 AWS SDK for Java 2.x Migration Hub 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.DeleteProgressUpdateStreamRequest; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; /** * 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 DeleteProgressStream { public static void main(String[] args) { final String usage = """ Usage: <progressStream>\s Where: progressStream - the name of a progress stream to delete.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String progressStream = args[0]; Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); deleteStream(migrationClient, progressStream); migrationClient.close(); } public static void deleteStream(MigrationHubClient migrationClient, String streamName) { try { DeleteProgressUpdateStreamRequest deleteProgressUpdateStreamRequest = DeleteProgressUpdateStreamRequest .builder() .progressUpdateStreamName(streamName) .build(); migrationClient.deleteProgressUpdateStream(deleteProgressUpdateStreamRequest); System.out.println(streamName + " is deleted"); } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.DescribeApplicationStateRequest; import software.amazon.awssdk.services.migrationhub.model.DescribeApplicationStateResponse; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; /** * 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 DescribeAppState { public static void main(String[] args) { final String usage = """ Usage: DescribeAppState <appId>\s Where: appId - the application id value.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String appId = args[0]; Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); describeApplicationState(migrationClient, appId); migrationClient.close(); } public static void describeApplicationState(MigrationHubClient migrationClient, String appId) { try { DescribeApplicationStateRequest applicationStateRequest = DescribeApplicationStateRequest.builder() .applicationId(appId) .build(); DescribeApplicationStateResponse applicationStateResponse = migrationClient .describeApplicationState(applicationStateRequest); System.out.println("The application status is " + applicationStateResponse.applicationStatusAsString()); } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.DescribeMigrationTaskRequest; import software.amazon.awssdk.services.migrationhub.model.DescribeMigrationTaskResponse; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; /** * 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 DescribeMigrationTask { public static void main(String[] args) { final String usage = """ Usage: DescribeMigrationTask <migrationTask> <progressStream>\s Where: migrationTask - the name of a migration task.\s progressStream - the name of a progress stream.\s """; if (args.length < 2) { System.out.println(usage); System.exit(1); } String migrationTask = args[0]; String progressStream = args[1]; Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); describeMigTask(migrationClient, migrationTask, progressStream); migrationClient.close(); } public static void describeMigTask(MigrationHubClient migrationClient, String migrationTask, String progressStream) { try { DescribeMigrationTaskRequest migrationTaskRequestRequest = DescribeMigrationTaskRequest.builder() .progressUpdateStream(progressStream) .migrationTaskName(migrationTask) .build(); DescribeMigrationTaskResponse migrationTaskResponse = migrationClient .describeMigrationTask(migrationTaskRequestRequest); System.out.println("The name is " + migrationTaskResponse.migrationTask().migrationTaskName()); } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.CreateProgressUpdateStreamRequest; import software.amazon.awssdk.services.migrationhub.model.ImportMigrationTaskRequest; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; /** * 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 ImportMigrationTask { public static void main(String[] args) { final String usage = """ Usage: <migrationTask> <progressStream>\s Where: migrationTask - the name of a migration task.\s progressStream - the name of a progress stream.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String migrationTask = args[0]; String progressStream = args[1]; Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); importMigrTask(migrationClient, migrationTask, progressStream); migrationClient.close(); } public static void importMigrTask(MigrationHubClient migrationClient, String migrationTask, String progressStream) { try { CreateProgressUpdateStreamRequest progressUpdateStreamRequest = CreateProgressUpdateStreamRequest.builder() .progressUpdateStreamName(progressStream) .dryRun(false) .build(); migrationClient.createProgressUpdateStream(progressUpdateStreamRequest); ImportMigrationTaskRequest migrationTaskRequest = ImportMigrationTaskRequest.builder() .migrationTaskName(migrationTask) .progressUpdateStream(progressStream) .dryRun(false) .build(); migrationClient.importMigrationTask(migrationTaskRequest); } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.ApplicationState; import software.amazon.awssdk.services.migrationhub.model.ListApplicationStatesRequest; import software.amazon.awssdk.services.migrationhub.model.ListApplicationStatesResponse; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; 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 ListApplications { public static void main(String[] args) { Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); listApps(migrationClient); migrationClient.close(); } public static void listApps(MigrationHubClient migrationClient) { try { ListApplicationStatesRequest applicationStatesRequest = ListApplicationStatesRequest.builder() .maxResults(10) .build(); ListApplicationStatesResponse response = migrationClient.listApplicationStates(applicationStatesRequest); List<ApplicationState> apps = response.applicationStateList(); for (ApplicationState appState : apps) { System.out.println("App Id is " + appState.applicationId()); System.out.println("The status is " + appState.applicationStatus().toString()); } } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }
  • 如需 API 詳細資訊,請參閱 ListApplications AWS SDK for Java 2.x 參考中的 API

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.CreatedArtifact; import software.amazon.awssdk.services.migrationhub.model.ListCreatedArtifactsRequest; import software.amazon.awssdk.services.migrationhub.model.ListCreatedArtifactsResponse; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; import java.util.List; /** * To run this Java V2 code example, ensure that you have setup your development * environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListCreatedArtifacts { public static void main(String[] args) { Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); listArtifacts(migrationClient); migrationClient.close(); } public static void listArtifacts(MigrationHubClient migrationClient) { try { ListCreatedArtifactsRequest listCreatedArtifactsRequest = ListCreatedArtifactsRequest.builder() .maxResults(10) .migrationTaskName("SampleApp5") .progressUpdateStream("ProgressSteamB") .build(); ListCreatedArtifactsResponse response = migrationClient.listCreatedArtifacts(listCreatedArtifactsRequest); List<CreatedArtifact> apps = response.createdArtifactList(); for (CreatedArtifact artifact : apps) { System.out.println("APp Id is " + artifact.description()); System.out.println("The name is " + artifact.name()); } } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }

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

Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.migrationhub.MigrationHubClient; import software.amazon.awssdk.services.migrationhub.model.ListMigrationTasksRequest; import software.amazon.awssdk.services.migrationhub.model.ListMigrationTasksResponse; import software.amazon.awssdk.services.migrationhub.model.MigrationTaskSummary; import software.amazon.awssdk.services.migrationhub.model.MigrationHubException; 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 ListMigrationTasks { public static void main(String[] args) { Region region = Region.US_WEST_2; MigrationHubClient migrationClient = MigrationHubClient.builder() .region(region) .build(); listMigrTasks(migrationClient); migrationClient.close(); } public static void listMigrTasks(MigrationHubClient migrationClient) { try { ListMigrationTasksRequest listMigrationTasksRequest = ListMigrationTasksRequest.builder() .maxResults(10) .build(); ListMigrationTasksResponse response = migrationClient.listMigrationTasks(listMigrationTasksRequest); List<MigrationTaskSummary> migrationList = response.migrationTaskSummaryList(); for (MigrationTaskSummary migration : migrationList) { System.out.println("Migration task name is " + migration.migrationTaskName()); System.out.println("The Progress update stream is " + migration.progressUpdateStream()); } } catch (MigrationHubException e) { System.out.println(e.getMessage()); System.exit(1); } } }
  • 如需 API 詳細資訊,請參閱 ListMigrationTasks AWS SDK for Java 2.x 參考中的 API