2017-11-20 16:21:40 +00:00
# Golang Kinesis Consumer
2014-07-25 06:03:41 +00:00
2017-11-20 16:21:40 +00:00
__Kinesis consumer applications written in Go__
2014-07-25 06:03:41 +00:00
2017-11-20 16:21:40 +00:00
> With the new release of Kinesis Firehose I'd recommend using the [kinesis to firehose](http://docs.aws.amazon.com/firehose/latest/dev/writing-with-kinesis-streams.html) functionality for writing data directly to S3, Redshift, or Elasticsearch.
2016-02-03 05:04:22 +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-20 16:21:40 +00:00
import consumer "github.com/harlow/kinesis-consumer"
2016-02-03 05:04:22 +00:00
func main() {
2017-11-20 16:21:40 +00:00
log.SetHandler(text.New(os.Stderr))
log.SetLevel(log.DebugLevel)
var (
app = flag.String("app", "", "App name") // name of consumer group
stream = flag.String("stream", "", "Stream name")
)
flag.Parse()
c, err := consumer.New(*app, *stream)
if err != nil {
log.Fatalf("new consumer error: %v", err)
}
c.Scan(context.TODO(), func(r *kinesis.Record) bool {
fmt.Println(string(r.Data))
return true // continue scanning
})
2016-02-03 05:04:22 +00:00
}
```
2014-07-25 06:03:41 +00:00
2017-11-20 16:21:40 +00:00
Note: If you need to aggregate based on a specific shard the `ScanShard` method should be leverged instead.
### Configuration
2016-12-04 08:08:06 +00:00
2017-11-20 16:21:40 +00:00
The consumer requires the following config:
2016-12-04 08:08:06 +00:00
2017-11-20 16:21:40 +00:00
* App Name (used for checkpoints)
* Stream Name (kinesis stream name)
It also accepts the following optional overrides:
* Kinesis Client
* Logger
* Checkpoint
```go
svc := kinesis.New(session.New(aws.NewConfig()))
c, err := consumer.New(
appName,
streamName,
consumer.WithClient(svc),
)
2016-12-04 08:08:06 +00:00
```
2017-11-20 16:21:40 +00:00
### Checkpoint
The default checkpoint uses Redis on localhost; to set a custom Redis URL use ENV vars:
```
REDIS_URL=redis.example.com:6379
2016-12-04 08:08:06 +00:00
```
2017-11-20 16:21:40 +00:00
* [Add DDB as a checkpoint option ](https://github.com/harlow/kinesis-consumer/issues/26 )
2016-05-01 19:20:44 +00:00
### Logging
2016-05-08 01:15:55 +00:00
[Apex Log ](https://medium.com/@tjholowaychuk/apex-log-e8d9627f4a9a#.5x1uo1767 ) is used for logging Info. Override the logs format with other [Log Handlers ](https://github.com/apex/log/tree/master/_examples ). For example using the "json" log handler:
2016-05-01 19:20:44 +00:00
```go
import(
2016-05-08 01:15:55 +00:00
"github.com/apex/log"
"github.com/apex/log/handlers/json"
2016-05-01 19:20:44 +00:00
)
func main() {
// ...
2016-05-08 01:15:55 +00:00
log.SetHandler(json.New(os.Stderr))
log.SetLevel(log.DebugLevel)
2016-05-01 19:20:44 +00:00
}
```
2016-05-01 19:40:30 +00:00
Which will producde the following logs:
```
INFO[0000] processing app=test shard=shardId-000000000000 stream=test
2017-11-20 16:21:40 +00:00
INFO[0008] checkpoint app=test shard=shardId-000000000000 stream=test
INFO[0012] checkpoint app=test shard=shardId-000000000000 stream=test
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)