-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (58 loc) · 2.08 KB
/
main.go
File metadata and controls
69 lines (58 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/segmentio/kafka-go"
)
// VideoMetadata represents the structure of the metadata associated with a video.
type VideoMetadata struct {
ID string `json:"id"`
Title string `json:"title"`
Duration int `json:"duration"`
Resolution string `json:"resolution"`
Description string `json:"description"`
}
// main is the entry point for the video metadata processing application.
// It demonstrates consuming messages from a Kafka topic, transforming them,
// and logging the transformed metadata.
func main() {
// Kafka broker address
brokerAddress := "localhost:9092"
// Kafka topic from which the metadata is consumed
topic := "video-metadata"
// Create a new Kafka reader (consumer) with the specified configuration
reader := kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{brokerAddress},
Topic: topic,
Partition: 0,
MinBytes: 10e3, // 10KB
MaxBytes: 10e6, // 10MB
})
defer reader.Close()
// Context with timeout to ensure the program does not hang indefinitely
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for {
// Read messages from the Kafka topic
m, err := reader.ReadMessage(ctx)
if err != nil {
log.Fatalf("Failed to read message: %v", err)
}
// Process the received message by unmarshaling it into a VideoMetadata struct
var metadata VideoMetadata
if err := json.Unmarshal(m.Value, &metadata); err != nil {
log.Printf("Error unmarshaling message: %v", err)
continue
}
// Perform any necessary transformation on the metadata
// For example, here we log the transformed metadata
log.Printf("Processed video metadata: ID=%s, Title=%s, Duration=%d seconds, Resolution=%s, Description=%s",
metadata.ID, metadata.Title, metadata.Duration, metadata.Resolution, metadata.Description)
// Optional: Print the message on console
fmt.Printf("Video Metadata: %+v\n", metadata)
}
}
// Note: To test this program, a Kafka producer should be used to send video metadata messages to the specified Kafka topic.