-
Notifications
You must be signed in to change notification settings - Fork 1
/
kinsumer.go
86 lines (77 loc) · 2.93 KB
/
kinsumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dynamodbkinsumer
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/aws/aws-sdk-go/service/dynamodbstreams"
"github.com/aws/aws-sdk-go/service/dynamodbstreams/dynamodbstreamsiface"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/twitchscience/kinsumer"
)
// DynamoDBStreamsKinsumer is a Kinsumer for DynamoDB Streams
type DynamoDBStreamsKinsumer struct {
*kinsumer.Kinsumer
}
// New returns a DynamoDBStreamsKinsumer
func New(tableName, partitionKey, applicationName, clientName string, config kinsumer.Config) (*DynamoDBStreamsKinsumer, error) {
s, err := session.NewSession()
if err != nil {
return nil, err
}
return NewWithSession(s, tableName, partitionKey, applicationName, clientName, config)
}
// NewWithSession should be used if you want to override the Kinesis and DynamoDB instances with a non-default aws session
func NewWithSession(session *session.Session, tableName, partitionKey, applicationName, clientName string, config kinsumer.Config) (*DynamoDBStreamsKinsumer, error) {
s := dynamodbstreams.New(session)
k := &DynamoDBStreamsKinesisAdapter{
streamsAPI: s,
partitionKeyAttribute: partitionKey,
}
d := dynamodb.New(session)
return NewWithInterfaces(k, d, s, tableName, applicationName, clientName, config)
}
// NewWithInterfaces allows you to override the Kinesis, DynamoDB Streams and DynamoDB instances for mocking or using a local set of servers
func NewWithInterfaces(kinesis kinesisiface.KinesisAPI, dynamodb dynamodbiface.DynamoDBAPI, streamsAPI dynamodbstreamsiface.DynamoDBStreamsAPI, tableName, applicationName, clientName string, config kinsumer.Config) (*DynamoDBStreamsKinsumer, error) {
listStreamsOutput, err := streamsAPI.ListStreams(&dynamodbstreams.ListStreamsInput{
TableName: &tableName,
})
if err != nil {
return nil, err
}
if len(listStreamsOutput.Streams) == 0 {
err = fmt.Errorf("no streams found for table: %s", tableName)
return nil, err
}
// streamName is first stream for table
streamName := *listStreamsOutput.Streams[0].StreamArn
k, err := kinsumer.NewWithInterfaces(kinesis, dynamodb, streamName, applicationName, clientName, config)
if err != nil {
return nil, err
}
return &DynamoDBStreamsKinsumer{
k,
}, nil
}
// Next calls Kinsumer.Next() and will return a StreamRecord
func (ddbsk *DynamoDBStreamsKinsumer) Next() (streamRecord *StreamRecord, err error) {
return ddbsk.next(ddbsk.Kinsumer)
}
type kinsumerNext interface {
Next() (data []byte, err error)
}
// next is a helper method which takes explicit kinsumerNext interface for testing
func (ddbsk *DynamoDBStreamsKinsumer) next(k kinsumerNext) (streamRecord *StreamRecord, err error) {
data, err := k.Next()
if err != nil {
return
}
sr := StreamRecord{}
err = json.Unmarshal(data, &sr)
if err != nil {
return
}
streamRecord = &sr
return
}