AWS Kinesis:确定指定名称的流是否存在

huangapple go评论75阅读模式
英文:

AWS Kinesis: determine whether a named stream exists

问题

我的目标是使用AWS Kinesis API创建一个具有特定名称的Kinesis流,如果该流不存在,则创建它,然后无论它是否已经存在,都向其写入数据。

到目前为止,我想到了以下方法。首先尝试创建流。如果返回的错误代码是400并且返回了一个请求ID,那么可能流已经存在。然后写入流以确保它存在。在Go语言中的代码如下:

k := kinesis.New(session.New())
_, err := k.CreateStream(&kinesis.CreateStreamInput{
    ShardCount: aws.Int64(2),
    StreamName: aws.String("stream"),
})
if err != nil {
    if reqerr, ok := err.(awserr.RequestFailure); ok {
        if reqerr.RequestID() == "" {
            log.Fatal("请求未被发送,因为它没有ID",
                reqerr.Code(),
                reqerr.Message(),
            )
        }
        if reqerr.StatusCode() != 400 {
            log.Fatal("意外的状态码", reqerr.StatusCode())
        }
    } else {
        log.Fatal(err)
    }
}

// 400错误码和请求ID并不一定意味着流已经存在
// 因此,写入流以确认其存在
_, err = k.PutRecord(&kinesis.PutRecordInput{
    Data:         []byte("Hello Kinesis"),
    PartitionKey: aws.String("partitionkey"),
    StreamName:   aws.String("stream"),
})
if err != nil {
    log.Fatal(err)
}

上述方法似乎有些复杂,更重要的是,我认为它并不能有效地匹配我期望的确切错误。对错误消息进行字符串比较也似乎不是一个好选择,因为错误消息可能会轻易改变。

我想知道是否有一种更可靠和直接的方法来实现这一目标?列出所有可用的流进行搜索很麻烦,因为这是一个线性搜索,并涉及多个带有新值的ExclusiveStartStreamName的请求。

英文:

My goal is to use the AWS Kinesis API to create a Kinesis stream with a particular name if it doesn't already exist and then write to it whether it was there in the first place or not.

This is what I've come up with so far. Attempt to create the stream. If it fails with code 400 and returns a request ID then maybe the stream already exists. Then write to the stream to make sure it's there. In Go:

k := kinesis.New(session.New())
_, err := k.CreateStream(&kinesis.CreateStreamInput{
	ShardCount: aws.Int64(2),
	StreamName: aws.String("stream"),
})
if err != nil {
	if reqerr, ok := err.(awserr.RequestFailure); ok {
		if reqerr.RequestID() == "" {
			log.Fatal("request was not delivered as it has no ID",
				reqerr.Code(),
				reqerr.Message(),
			)
		}
		if reqerr.StatusCode() != 400 {
			log.Fatal("unexpected status code", reqerr.StatusCode())
		}
	} else {
		log.Fatal(err)
	}
}
// Code 400 + requestID does not necessarily mean that the stream exists
// So write to the stream to confirm it exists
_, err = k.PutRecord(&kinesis.PutRecordInput{
	Data:         []byte("Hello Kinesis"),
	PartitionKey: aws.String("partitionkey"),
	StreamName:   aws.String("stream"),
})
if err != nil {
    log.Fatal(err)
}

The approach above seems convoluted and more importantly I don't think it effectively matches on the exact error I'm expecting. Doing a string compare on the error message seems like a bad choice too because that could easily change.

I'm wondering if there is a more reliable and straightforward way to achieve this? Listing all the available streams to search is a pain because it is a linear search and involves multiple requests with new values of ExclusiveStartStreamName.

答案1

得分: 1

描述流。如果流不存在,则创建流并自旋等待变为活动状态。

在创建流后,您将无法立即向流中推送数据。它将首先转换为CREATING状态,然后经过一段时间(几秒钟)转换为ACTIVE状态。

您还可以使用ListStreams快速查看所有流的状态:

https://docs.aws.amazon.com/sdk-for-go/api/service/kinesis/Kinesis.html#ListStreams-instance_method

英文:

Describe the stream. If stream is not there create the stream and spin waiting to become active.

You will not be able to push to the stream immediately after it's created. It's going to transition to CREATING first, and after a while (seconds) to ACTIVE.

https://docs.aws.amazon.com/sdk-for-go/api/service/kinesis/Kinesis.html#DescribeStream-instance_method

https://docs.aws.amazon.com/sdk-for-go/api/service/kinesis/Kinesis.html#CreateStream-instance_method

You can also use ListStreams to quickly peek at the state of all streams:

https://docs.aws.amazon.com/sdk-for-go/api/service/kinesis/Kinesis.html#ListStreams-instance_method

huangapple
  • 本文由 发表于 2016年2月26日 03:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/35636659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定