Skip to content

Instantly share code, notes, and snippets.

@leonardorifeli
Forked from coboshm/golang_kinesis.go
Created June 14, 2018 00:59
Show Gist options
  • Save leonardorifeli/3a9e860fa24c140521da0978a2b0b483 to your computer and use it in GitHub Desktop.
Save leonardorifeli/3a9e860fa24c140521da0978a2b0b483 to your computer and use it in GitHub Desktop.
Golang + Kinesis firehose
package main
import (
"log"
"encoding/json"
"fmt"
"os"
"math/rand"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/firehose"
)
// FakeEntity is just used for testing purposes
type FakeEntity struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
const maxUint = 4294967295
func main() {
log.Println("Begin")
streamName := "firehoseStream"
sess := session.Must(session.NewSession())
// Create a Firehose client with additional configuration
firehoseService := firehose.New(sess, aws.NewConfig().WithRegion("us-east-1"))
recordsBatchInput := &firehose.PutRecordBatchInput{}
recordsBatchInput = recordsBatchInput.SetDeliveryStreamName(streamName)
records := []*firehose.Record{}
for i := 0; i < 10; i++ {
data := FakeEntity{
ID: rand.Intn(maxUint),
Name: fmt.Sprintf("Name-%d", rand.Intn(maxUint)),
Description: fmt.Sprintf("Test-Description %d", rand.Intn(maxUint)),
}
b, err := json.Marshal(data)
if err != nil {
log.Printf("Error: %v", err)
os.Exit(1)
}
record := &firehose.Record{Data: b}
records = append(records, record)
}
recordsBatchInput = recordsBatchInput.SetRecords(records)
resp, err := firehoseService.PutRecordBatch(recordsBatchInput)
if err != nil {
fmt.Printf("PutRecordBatch err: %v\n", err)
} else {
fmt.Printf("PutRecordBatch: %v\n", resp)
}
log.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment