forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request awsdocs#1660 from knabben/master
Added go v2 version of Kinesis PutRecord
- Loading branch information
Showing
7 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX - License - Identifier: Apache - 2.0 | ||
// snippet-start:[kinesis.go-v2.PutRecord] | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/kinesis" | ||
) | ||
|
||
// KinesisPutRecordAPI defines the interface for the PutRecord function. | ||
// We use this interface to test the function using a mocked service. | ||
type KinesisPutRecordAPI interface { | ||
PutRecord(ctx context.Context, | ||
params *kinesis.PutRecordInput, | ||
optFns ...func(*kinesis.Options)) (*kinesis.PutRecordOutput, error) | ||
} | ||
|
||
// MakePutRecord creates an Amazon Kinesis (Amazon Kinesis) stream record. | ||
// Inputs: | ||
// c is the context of the method call, which includes the AWS Region. | ||
// api is the interface that defines the method call. | ||
// input defines the input arguments to the service call. | ||
// Output: | ||
// If success, a PutRecordOutput object containing the result of the service call and nil. | ||
// Otherwise, nil and an error from the call to PutRecordOutput. | ||
func MakePutRecord(c context.Context, api KinesisPutRecordAPI, input *kinesis.PutRecordInput) (*kinesis.PutRecordOutput, error) { | ||
return api.PutRecord(c, input) | ||
} | ||
|
||
func main() { | ||
stream := flag.String("s", "", "The name of the stream") | ||
partition := flag.String("k", "", "The identifier of the partition key") | ||
payload := flag.String("p", "", "The payload") | ||
flag.Parse() | ||
|
||
cfg, err := config.LoadDefaultConfig(context.TODO()) | ||
if err != nil { | ||
panic("configuration error, " + err.Error()) | ||
} | ||
|
||
client := kinesis.NewFromConfig(cfg) | ||
|
||
input := &kinesis.PutRecordInput{ | ||
Data: []byte(*payload), | ||
PartitionKey: partition, | ||
StreamName: stream, | ||
} | ||
|
||
results, err := MakePutRecord(context.TODO(), client, input) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
fmt.Println(results.SequenceNumber) | ||
} | ||
|
||
// snippet-end:[kinesis.go-v2.PutRecord] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/kinesis" | ||
) | ||
|
||
type KinesisPutRecordImpl struct{} | ||
|
||
func (pr KinesisPutRecordImpl) PutRecord(ctx context.Context, | ||
params *kinesis.PutRecordInput, | ||
optFns ...func(*kinesis.Options)) (*kinesis.PutRecordOutput, error) { | ||
|
||
output := &kinesis.PutRecordOutput{ | ||
ShardId: aws.String("shard-01"), | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
type Config struct { | ||
StreamName string `json:"StreamName"` | ||
} | ||
|
||
var configFileName = "config.json" | ||
|
||
var globalConfig Config | ||
|
||
func populateConfiguration(t *testing.T) error { | ||
content, err := ioutil.ReadFile(configFileName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
text := string(content) | ||
|
||
err = json.Unmarshal([]byte(text), &globalConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if globalConfig.StreamName == "" { | ||
msg := "You must specify a value for StreamName in " + configFileName | ||
return errors.New(msg) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestPutRecord(t *testing.T) { | ||
thisTime := time.Now() | ||
nowString := thisTime.Format("2006-01-02 15:04:05 Monday") | ||
t.Log("Starting unit test at " + nowString) | ||
|
||
err := populateConfiguration(t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
input := kinesis.PutRecordInput{ | ||
StreamName: &globalConfig.StreamName, | ||
} | ||
|
||
api := &KinesisPutRecordImpl{} | ||
|
||
resp, err := MakePutRecord(context.Background(), *api, &input) | ||
if err != nil { | ||
t.Log("Got an error ...:") | ||
t.Log(err) | ||
return | ||
} | ||
|
||
t.Log("Put record on shard " + *resp.ShardId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"StreamName": "fakestream", | ||
"PartitionKey": "shard-0", | ||
"Data": "payload" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# AWS SDK for Go V2 code examples for Amazon Kinesis | ||
|
||
## Purpose | ||
|
||
These examples demonstrates how to perform several Kinesis operations | ||
using version 2 of the AWS SDK for Go. | ||
|
||
## Prerequisites | ||
|
||
You must have an AWS account, and have your default credentials and AWS Region | ||
configured as described in | ||
[Configuring the AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html) | ||
in the AWS SDK for Go Developer Guide. | ||
|
||
## Running the code | ||
|
||
### PutRecord/PutRecordv2.go | ||
|
||
This example creates an Amazon EC2 image. | ||
|
||
`go run PutRecordv2.go -s STREAM -k PARTITION-KEY -p PAYLOAD | ||
|
||
- _STREAM_ is the Kinesis stream name. | ||
- _PARTITION-KEY_ is the partition ID. | ||
- _PAYLOAD_ is the content to be published. | ||
|
||
The unit test accepts similar values in _config.json_. | ||
|
||
## Running the unit tests | ||
|
||
Unit tests should delete any resources they create. | ||
However, they might result in charges to your | ||
AWS account. | ||
|
||
To run a unit test, enter: | ||
|
||
`go test` | ||
|
||
You should see something like the following, | ||
where PATH is the path to the folder containing the Go files: | ||
|
||
```sh | ||
PASS | ||
ok PATH 6.593s | ||
``` | ||
|
||
If you want to see any log messages, enter: | ||
|
||
`go test -v` | ||
|
||
You should see some additional log messages. | ||
The last two lines should be similar to the previous output shown. | ||
|
||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
files: | ||
- path: PutRecord/PutRecordv2.go | ||
services: | ||
- kinesis | ||
- path: PutRecord/PutRecordv2_test.go | ||
services: | ||
- kinesis |