2017-11-20 16:21:40 +00:00
# Golang Kinesis Consumer
2014-07-25 06:03:41 +00:00
2017-11-20 19:14:39 +00:00
Kinesis consumer applications written in Go. This library is intended to be a lightweight wrapper around the Kinesis API to read records, save checkpoints (with swappable backends), and gracefully recover from network errors.
2017-11-20 17:37:30 +00:00
2017-11-22 18:50:09 +00:00
__NOTE(1):__ With the release of [Kinesis to Firehose ](http://docs.aws.amazon.com/firehose/latest/dev/writing-with-kinesis-streams.html ) it's possible to archive data directly to S3, Redshift, or Elasticsearch without running a consumer application.
2016-02-03 05:04:22 +00:00
2017-11-22 18:50:09 +00:00
__NOTE(2):__ To avoid managing checkpoints and running any infrastructure it's also possible to [Process Kinensis Streams with Golang and AWS Lambda ](https://medium.com/@harlow/processing-kinesis-streams-w-aws-lambda-and-golang-264efc8f979a )._
2017-11-20 18:29:30 +00:00
2017-11-20 16:21:40 +00:00
## Installation
2015-05-23 15:52:08 +00:00
2017-11-20 16:21:40 +00:00
Get the package source:
$ go get github.com/harlow/kinesis-consumer
2015-05-23 23:18:10 +00:00
2014-07-25 06:03:41 +00:00
## Overview
2014-07-25 06:03:41 +00:00
2017-11-20 16:21:40 +00:00
The consumer leverages a handler func that accepts a Kinesis record. The `Scan` method will consume all shards concurrently and call the callback func as it receives records from the stream.
2016-02-03 05:04:22 +00:00
2016-02-09 03:42:26 +00:00
```go
2017-11-21 16:58:16 +00:00
import(
// ...
consumer "github.com/harlow/kinesis-consumer"
checkpoint "github.com/harlow/kinesis-consumer/checkpoint/redis"
)
2017-11-20 16:21:40 +00:00
2016-02-03 05:04:22 +00:00
func main() {
2017-11-20 16:21:40 +00:00
var (
2017-11-21 16:58:16 +00:00
app = flag.String("app", "", "App name")
2017-11-20 16:21:40 +00:00
stream = flag.String("stream", "", "Stream name")
)
flag.Parse()
2017-11-21 16:58:16 +00:00
// new checkpoint
ck, err := checkpoint.New(*app, *stream)
if err != nil {
log.Fatalf("checkpoint error: %v", err)
}
// new consumer
c, err := consumer.New(ck, *app, *stream)
2017-11-20 16:21:40 +00:00
if err != nil {
2017-11-20 19:45:30 +00:00
log.Fatalf("consumer error: %v", err)
2017-11-20 16:21:40 +00:00
}
2017-11-21 16:58:16 +00:00
// scan stream
err = c.Scan(context.TODO(), func(r *consumer.Record) bool {
2017-11-20 16:21:40 +00:00
fmt.Println(string(r.Data))
return true // continue scanning
})
2017-11-20 19:45:30 +00:00
if err != nil {
log.Fatalf("scan error: %v", err)
}
// Note: If you need to aggregate based on a specific shard the `ScanShard`
// method should be leverged instead.
2016-02-03 05:04:22 +00:00
}
```
2014-07-25 06:03:41 +00:00
2017-11-22 18:46:39 +00:00
## Checkpoint
2016-12-04 08:08:06 +00:00
2017-11-21 16:58:16 +00:00
To record the progress of the consumer in the stream we use a checkpoint to store the last sequence number the consumer has read from a particular shard.
2016-12-04 08:08:06 +00:00
2017-11-21 16:58:16 +00:00
This will allow consumers to re-launch and pick up at the position in the stream where they left off.
2017-11-20 16:21:40 +00:00
2017-11-21 16:58:16 +00:00
The uniq identifier for a consumer is `[appName, streamName, shardID]`
2017-11-20 16:21:40 +00:00
2017-11-21 17:04:39 +00:00
< img width = "722" alt = "kinesis-checkpoints" src = "https://user-images.githubusercontent.com/739782/33085867-d8336122-ce9a-11e7-8c8a-a8afeb09dff1.png" >
2017-11-20 16:21:40 +00:00
2017-11-21 17:04:39 +00:00
There are currently two storage types for checkpoints:
2017-11-20 16:21:40 +00:00
2017-11-22 18:46:39 +00:00
### Redis Checkpoint
2017-11-20 19:06:46 +00:00
2017-11-21 16:58:16 +00:00
The Redis checkpoint requries App Name, and Stream Name:
2017-11-20 19:06:46 +00:00
2017-11-21 16:58:16 +00:00
```go
import checkpoint "github.com/harlow/kinesis-consumer/checkpoint/redis"
2017-11-20 19:06:46 +00:00
2017-11-21 16:58:16 +00:00
// redis checkpoint
ck, err := checkpoint.New(appName, streamName)
if err != nil {
log.Fatalf("new checkpoint error: %v", err)
}
2016-12-04 08:08:06 +00:00
```
2017-11-22 18:46:39 +00:00
### DynamoDB Checkpoint
2017-11-20 17:55:43 +00:00
2017-11-21 16:58:16 +00:00
The DynamoDB checkpoint requires Table Name, App Name, and Stream Name:
2017-11-20 17:37:30 +00:00
```go
2017-11-21 16:58:16 +00:00
import checkpoint "github.com/harlow/kinesis-consumer/checkpoint/ddb"
2017-11-20 17:55:43 +00:00
// ddb checkpoint
ck, err := checkpoint.New(tableName, appName, streamName)
2017-11-20 17:37:30 +00:00
if err != nil {
log.Fatalf("new checkpoint error: %v", err)
}
2017-11-21 16:58:16 +00:00
```
To leverage the DDB checkpoint we'll also need to create a table:
< img width = "659" alt = "screen shot 2017-11-20 at 9 16 14 am" src = "https://user-images.githubusercontent.com/739782/33033316-db85f848-cdd8-11e7-941a-0a87d8ace479.png" >
2017-11-20 17:37:30 +00:00
2017-11-22 18:46:39 +00:00
## Options
2017-11-21 16:58:16 +00:00
The consumer allows the following optional overrides:
* Kinesis Client
* Logger
```go
// new kinesis client
svc := kinesis.New(session.New(aws.NewConfig()))
// new consumer with custom client
2017-11-20 17:37:30 +00:00
c, err := consumer.New(
2017-11-21 16:58:16 +00:00
consumer,
2017-11-20 17:37:30 +00:00
streamName,
2017-11-21 16:58:16 +00:00
consumer.WithClient(svc),
2017-11-20 17:37:30 +00:00
)
```
2017-11-20 16:21:40 +00:00
2017-11-22 18:46:39 +00:00
## Logging
2016-05-01 19:20:44 +00:00
2017-11-22 18:50:09 +00:00
The package defaults to `ioutil.Discard` which will silence log output. This can be overridden with the preferred logging strategy:
2016-05-01 19:20:44 +00:00
```go
func main() {
2017-11-22 18:46:39 +00:00
// ...
2016-05-01 19:20:44 +00:00
2017-11-22 18:46:39 +00:00
// logger
logger := log.New(os.Stdout, "consumer-example: ", log.LstdFlags)
2016-05-01 19:40:30 +00:00
2017-11-22 18:46:39 +00:00
// consumer
c, err := consumer.New(checkpoint, appName, streamName, consumer.WithLogger(logger))
}
2016-05-01 19:40:30 +00:00
```
2015-05-23 23:18:10 +00:00
## Contributing
2015-05-23 15:52:08 +00:00
2015-05-23 22:22:58 +00:00
Please see [CONTRIBUTING.md] for more information. Thank you, [contributors]!
2015-05-23 15:52:08 +00:00
[LICENSE]: /MIT-LICENSE
[CONTRIBUTING.md]: /CONTRIBUTING.md
2015-05-23 23:18:10 +00:00
## License
2015-05-23 15:52:08 +00:00
Copyright (c) 2015 Harlow Ward. It is free software, and may
be redistributed under the terms specified in the [LICENSE] file.
[contributors]: https://github.com/harlow/kinesis-connectors/graphs/contributors
2016-05-01 19:45:27 +00:00
> [www.hward.com](http://www.hward.com) ·
> GitHub [@harlow](https://github.com/harlow) ·
> Twitter [@harlow_ward](https://twitter.com/harlow_ward)