2016-05-01 23:54:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"flag"
|
2017-11-20 16:21:40 +00:00
|
|
|
"fmt"
|
2018-07-29 05:53:33 +00:00
|
|
|
"log"
|
2016-05-01 23:54:47 +00:00
|
|
|
"os"
|
2017-11-20 16:21:40 +00:00
|
|
|
"time"
|
2016-05-01 23:54:47 +00:00
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-05-01 23:54:47 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-09-02 14:25:07 +00:00
|
|
|
var (
|
|
|
|
|
streamName = flag.String("stream", "", "Stream name")
|
|
|
|
|
kinesisEndpoint = flag.String("endpoint", "http://localhost:4567", "Kinesis endpoint")
|
|
|
|
|
awsRegion = flag.String("region", "us-west-2", "AWS Region")
|
|
|
|
|
)
|
2017-11-20 16:21:40 +00:00
|
|
|
flag.Parse()
|
2016-05-01 23:54:47 +00:00
|
|
|
|
2018-12-30 15:28:18 +00:00
|
|
|
// open dummy user data
|
|
|
|
|
f, err := os.Open("users.txt")
|
2016-05-01 23:54:47 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("Cannot open users.txt file")
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2017-11-26 23:59:17 +00:00
|
|
|
var records []*kinesis.PutRecordsRequestEntry
|
2019-04-07 23:29:12 +00:00
|
|
|
|
2019-09-02 14:44:26 +00:00
|
|
|
var client = kinesis.New(session.Must(session.NewSession(
|
|
|
|
|
aws.NewConfig().
|
|
|
|
|
WithEndpoint(*kinesisEndpoint).
|
|
|
|
|
WithRegion(*awsRegion).
|
|
|
|
|
WithLogLevel(3),
|
|
|
|
|
)))
|
2019-09-02 14:25:07 +00:00
|
|
|
|
|
|
|
|
// create stream if doesn't exist
|
|
|
|
|
if err := createStream(client, streamName); err != nil {
|
|
|
|
|
log.Fatalf("create stream error: %v", err)
|
2019-04-07 23:29:12 +00:00
|
|
|
}
|
2017-11-20 16:21:40 +00:00
|
|
|
|
2016-05-01 23:54:47 +00:00
|
|
|
// loop over file data
|
|
|
|
|
b := bufio.NewScanner(f)
|
|
|
|
|
for b.Scan() {
|
2017-11-20 16:21:40 +00:00
|
|
|
records = append(records, &kinesis.PutRecordsRequestEntry{
|
|
|
|
|
Data: b.Bytes(),
|
|
|
|
|
PartitionKey: aws.String(time.Now().Format(time.RFC3339Nano)),
|
|
|
|
|
})
|
|
|
|
|
|
2017-11-26 23:59:17 +00:00
|
|
|
if len(records) > 250 {
|
2018-12-30 15:28:18 +00:00
|
|
|
putRecords(client, streamName, records)
|
2017-11-20 16:21:40 +00:00
|
|
|
records = nil
|
2016-05-01 23:54:47 +00:00
|
|
|
}
|
2017-11-26 23:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(records) > 0 {
|
2018-12-30 15:28:18 +00:00
|
|
|
putRecords(client, streamName, records)
|
2017-11-26 23:59:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-01 23:54:47 +00:00
|
|
|
|
2019-09-02 14:25:07 +00:00
|
|
|
func createStream(client *kinesis.Kinesis, streamName *string) error {
|
|
|
|
|
resp, err := client.ListStreams(&kinesis.ListStreamsInput{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("list streams error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, val := range resp.StreamNames {
|
|
|
|
|
if *streamName == *val {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = client.CreateStream(
|
|
|
|
|
&kinesis.CreateStreamInput{
|
|
|
|
|
StreamName: streamName,
|
|
|
|
|
ShardCount: aws.Int64(2),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 15:28:18 +00:00
|
|
|
func putRecords(client *kinesis.Kinesis, streamName *string, records []*kinesis.PutRecordsRequestEntry) {
|
|
|
|
|
_, err := client.PutRecords(&kinesis.PutRecordsInput{
|
2017-11-26 23:59:17 +00:00
|
|
|
StreamName: streamName,
|
|
|
|
|
Records: records,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2018-07-29 05:53:33 +00:00
|
|
|
log.Fatalf("error putting records: %v", err)
|
2017-11-20 16:21:40 +00:00
|
|
|
}
|
2019-09-02 14:44:26 +00:00
|
|
|
|
2017-11-26 23:59:17 +00:00
|
|
|
fmt.Print(".")
|
2016-05-01 23:54:47 +00:00
|
|
|
}
|