DeleteStream 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

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

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

DeleteStream 搭配 a AWS SDK 或 CLI 使用

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

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
AWS SDK for .NET
注意

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

using System; using System.Threading.Tasks; using Amazon.Kinesis; using Amazon.Kinesis.Model; /// <summary> /// Shows how to delete an Amazon Kinesis stream. /// </summary> public class DeleteStream { public static async Task Main() { IAmazonKinesis client = new AmazonKinesisClient(); string streamName = "AmazonKinesisStream"; var success = await DeleteStreamAsync(client, streamName); if (success) { Console.WriteLine($"Stream, {streamName} successfully deleted."); } else { Console.WriteLine("Stream not deleted."); } } /// <summary> /// Deletes a Kinesis stream. /// </summary> /// <param name="client">An initialized Kinesis client object.</param> /// <param name="streamName">The name of the string to delete.</param> /// <returns>A Boolean value representing the success of the operation.</returns> public static async Task<bool> DeleteStreamAsync(IAmazonKinesis client, string streamName) { // If EnforceConsumerDeletion is true, any consumers // of this stream will also be deleted. If it is set // to false and this stream has any consumers, the // call will fail with a ResourceInUseException. var request = new DeleteStreamRequest { StreamName = streamName, EnforceConsumerDeletion = true, }; var response = await client.DeleteStreamAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } }
  • 如需 API 詳細資訊,請參閱 DeleteStream AWS SDK for .NET 參考中的 API

CLI
AWS CLI

刪除資料串流

下列delete-stream範例會刪除指定的資料串流。

aws kinesis delete-stream \ --stream-name samplestream

此命令不會產生輸出。

如需詳細資訊,請參閱 Amazon Kinesis Data Streams 開發人員指南中的刪除串流

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteStream

Java
Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.kinesis.KinesisClient; import software.amazon.awssdk.services.kinesis.model.DeleteStreamRequest; import software.amazon.awssdk.services.kinesis.model.KinesisException; /** * 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 DeleteDataStream { public static void main(String[] args) { final String usage = """ Usage: <streamName> Where: streamName - The Amazon Kinesis data stream (for example, StockTradeStream) """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String streamName = args[0]; Region region = Region.US_EAST_1; KinesisClient kinesisClient = KinesisClient.builder() .region(region) .build(); deleteStream(kinesisClient, streamName); kinesisClient.close(); System.out.println("Done"); } public static void deleteStream(KinesisClient kinesisClient, String streamName) { try { DeleteStreamRequest delStream = DeleteStreamRequest.builder() .streamName(streamName) .build(); kinesisClient.deleteStream(delStream); } catch (KinesisException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • 如需 API 詳細資訊,請參閱 DeleteStream AWS SDK for Java 2.x 參考中的 API

PowerShell
for PowerShell 工具

範例 1:刪除指定的串流。在命令執行之前,系統會提示您進行確認。若要隱藏確認提示,請使用 -Force 切換。

Remove-KINStream -StreamName "mystream"
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DeleteStream

Python
SDK for Python (Boto3)
注意

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

class KinesisStream: """Encapsulates a Kinesis stream.""" def __init__(self, kinesis_client): """ :param kinesis_client: A Boto3 Kinesis client. """ self.kinesis_client = kinesis_client self.name = None self.details = None self.stream_exists_waiter = kinesis_client.get_waiter("stream_exists") def delete(self): """ Deletes a stream. """ try: self.kinesis_client.delete_stream(StreamName=self.name) self._clear() logger.info("Deleted stream %s.", self.name) except ClientError: logger.exception("Couldn't delete stream %s.", self.name) raise
  • 如需 API 詳細資訊,請參閱 DeleteStream AWS SDK for Python (Boto3) Word 參考中的 API

Rust
Rust 的 SDK
注意

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

async fn remove_stream(client: &Client, stream: &str) -> Result<(), Error> { client.delete_stream().stream_name(stream).send().await?; println!("Deleted stream."); Ok(()) }
  • 如需 API 詳細資訊,請參閱 DeleteStream AWS for Rust SDK 參考中的 API

SAP ABAP
SDK for SAP ABAP
注意

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

TRY. lo_kns->deletestream( iv_streamname = iv_stream_name ). MESSAGE 'Stream deleted.' TYPE 'I'. CATCH /aws1/cx_knslimitexceededex . MESSAGE 'The request processing has failed because of a limit exceed exception.' TYPE 'E'. CATCH /aws1/cx_knsresourceinuseex . MESSAGE 'The request processing has failed because the resource is in use.' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 DeleteStream for Word Word 參考中的 Word。 AWS SDK SAP ABAP API