-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
183 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"go.formatTool": "goimports" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
##################################### | ||
# STEP 1 build executable binary # | ||
##################################### | ||
FROM golang:alpine AS builder | ||
|
||
# Install git. | ||
# Git is required for fetching the dependencies. | ||
RUN apk update && apk add --no-cache git | ||
|
||
WORKDIR /app | ||
|
||
COPY go.mod . | ||
|
||
RUN go mod download | ||
|
||
COPY . . | ||
|
||
# Build the binary. | ||
RUN CGO_ENABLED=0 GOOS=linux go build -o main | ||
|
||
##################################### | ||
# STEP 2 build a small image # | ||
##################################### | ||
FROM scratch | ||
|
||
# Copy our static executable. | ||
COPY --from=builder /app/main /app/main | ||
|
||
# Run the hello binary. | ||
ENTRYPOINT ["/app/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
apiVersion: extensions/v1beta1 | ||
kind: Deployment | ||
metadata: | ||
name: epiphany-kafka-elk-connector | ||
spec: | ||
replicas: 1 | ||
strategy: | ||
type: RollingUpdate | ||
rollingUpdate: | ||
maxSurge: 1 | ||
maxUnavailable: 1 | ||
minReadySeconds: 5 | ||
template: | ||
metadata: | ||
labels: | ||
app: epiphany-kafka-elk-connector | ||
spec: | ||
containers: | ||
- name: epiphany-kafka-elk-connector | ||
image: toszo/kafka-elk-connector:0.1.1 | ||
imagePullPolicy: Always | ||
env: | ||
- name: KAFKA_URL | ||
value: "10.0.4.6:9092" | ||
- name: TOPIC | ||
value: "iot-data" | ||
- name: GROUP_ID | ||
value: "my-consumer-group" | ||
- name: ELASTIC_URL | ||
value: "http://10.0.4.9:9200" | ||
- name: INDEX_NAME | ||
value: "timeseries" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/toszo/go-kafka-elk | ||
|
||
require ( | ||
github.com/google/uuid v1.1.0 | ||
github.com/segmentio/kafka-go v0.2.2 | ||
github.com/olivere/elastic v6.2.17 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
"strconv" | ||
//"github.com/google/uuid" | ||
kafka "github.com/segmentio/kafka-go" | ||
"encoding/json" | ||
"math/rand" | ||
) | ||
|
||
type IoTData struct { | ||
ObjectId string `json:"objectId"` | ||
TimeStamp time.Time `json:"timeStamp,omitempty"` | ||
Variable string `json:"variable,omitempty"` | ||
Model string `json:"model,omitempty"` | ||
Value float64 `json:"value,omitempty"` | ||
Quality int `json:"quality,omitempty"` | ||
} | ||
|
||
func newKafkaWriter(kafkaURL, topic string) *kafka.Writer { | ||
return kafka.NewWriter(kafka.WriterConfig{ | ||
Brokers: []string{kafkaURL}, | ||
Topic: topic, | ||
Balancer: &kafka.LeastBytes{}, | ||
}) | ||
} | ||
|
||
func main() { | ||
// get kafka writer using environment variables. | ||
// kafkaURL := os.Getenv("kafkaURL") | ||
// topic := os.Getenv("topic") | ||
kafkaURL := "10.0.4.6:9092" | ||
topic := "iot-data" | ||
|
||
writer := newKafkaWriter(kafkaURL, topic) | ||
defer writer.Close() | ||
fmt.Println("start producing ... !!") | ||
for i := 0; ; i++ { | ||
t := time.Now() | ||
fmt.Println(t.Format("20060102150405")) | ||
|
||
for j := 0; j < 20 ; j++ { | ||
data := IoTData{strconv.Itoa(j), t, "variable", "my.model.co", rand.Float64(), 1} | ||
fmt.Println(data) | ||
jsonData, merr := json.Marshal(data) | ||
if merr != nil { | ||
fmt.Println(merr) | ||
} | ||
|
||
msg := kafka.Message{ | ||
Key: []byte(t.Format("20060102150405")), | ||
Value: jsonData, | ||
} | ||
|
||
err := writer.WriteMessages(context.Background(), msg) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
//time.Sleep(1 * time.Second) | ||
} | ||
} |