Add error checking for tmp file
This commit is contained in:
parent
d542fa996f
commit
509f68de89
1 changed files with 26 additions and 18 deletions
|
|
@ -2,7 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -13,13 +13,34 @@ import (
|
||||||
|
|
||||||
// Note: download file with test data
|
// Note: download file with test data
|
||||||
// curl https://s3.amazonaws.com/kinesis.test/users.txt -o /tmp/users.txt
|
// curl https://s3.amazonaws.com/kinesis.test/users.txt -o /tmp/users.txt
|
||||||
|
func putToS3(svc *kinesis.Kinesis, data string) {
|
||||||
|
params := &kinesis.PutRecordInput{
|
||||||
|
Data: []byte(data),
|
||||||
|
PartitionKey: aws.String("partitionKey"),
|
||||||
|
StreamName: aws.String("hw-test-stream"),
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := svc.PutRecord(params)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err.Error())
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Print(".")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
jobCh := make(chan string)
|
jobCh := make(chan string)
|
||||||
|
|
||||||
// read sample data
|
// read sample data
|
||||||
file, _ := os.Open("/tmp/users.txt")
|
file, err := os.Open("/tmp/users.txt")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Cannot open users.txt file")
|
||||||
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
|
|
@ -30,20 +51,7 @@ func main() {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
for data := range jobCh {
|
for data := range jobCh {
|
||||||
params := &kinesis.PutRecordInput{
|
putToS3(svc, data)
|
||||||
Data: []byte(data),
|
|
||||||
PartitionKey: aws.String("partitionKey"),
|
|
||||||
StreamName: aws.String("hw-test-stream"),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := svc.PutRecord(params)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
fmt.Print(".")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
@ -54,6 +62,6 @@ func main() {
|
||||||
jobCh <- data
|
jobCh <- data
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(".")
|
log.Println(".")
|
||||||
fmt.Println("Finished populating stream")
|
log.Println("Finished populating stream")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue