-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9816 from michael-redpanda/expand-serde-coverage
Expand serde coverage
- Loading branch information
Showing
24 changed files
with
2,002 additions
and
41 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
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 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 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,19 @@ | ||
export PATH := $(PATH):$(GOPATH)/bin | ||
|
||
all: payload.pb.go go-kafka-serde | ||
|
||
clean: | ||
rm -rf payload.pb.go go-kafka-serde | ||
|
||
go-kafka-serde: payload.pb.go | ||
go build | ||
|
||
PROTOC_GEN_GO := $(GOPATH)/bin/protoc-gen-go | ||
|
||
$(PROTOC_GEN_GO): | ||
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.30.0 | ||
|
||
payload.pb.go: payload.proto | $(PROTOC_GEN_GO) | ||
protoc --proto_path=. --go_out=. --go_opt=Mpayload.proto=. payload.proto | ||
|
||
compile: payload.pb.go |
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,44 @@ | ||
# Golang Kafka Serde Client | ||
|
||
This directory contains a client that can be used to validate the operation of Redpanda's Schema Registry via | ||
Confluent's Golang client. | ||
|
||
This application will create either a simple AVRO or PROTOBUF schema, serialize a number of messages | ||
|
||
## Building | ||
|
||
### Pre-requisites | ||
|
||
* Protobuf Compiler | ||
* Golang compiler | ||
|
||
### Build Instructions | ||
|
||
It is important to set `$GOPATH` either within your environment or on the command line when you run `make`: | ||
|
||
```shell | ||
GOPATH=$HOME/go make clean all | ||
``` | ||
|
||
## Usage | ||
|
||
```shell | ||
❯ ./go-kafka-serde -help | ||
Usage of ./go-kafka-serde: | ||
-brokers string | ||
comma delimited list of brokers (default "localhost:9092") | ||
-consumer-group string | ||
Consumer group to use (default "db5b01db-7f48-4d7d-8ba6-eb512788341e") | ||
-count int | ||
Number of messages to produce and consume (default 1) | ||
-debug | ||
Enable verbose logging | ||
-protocol string | ||
Protocol to use. Must be AVRO or PROTOBUF (default "AVRO") | ||
-schema-registry string | ||
URL of schema registry (default "http://127.0.0.1:8081") | ||
-security string | ||
Security settings | ||
-topic string | ||
topic to produce/consume from (default "3aa75595-1222-4daa-b407-afe43c8496d5") | ||
``` |
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,76 @@ | ||
/* | ||
* Copyright 2023 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry" | ||
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/serde" | ||
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/serde/avro" | ||
) | ||
|
||
type AvroSerializer struct { | ||
ser *avro.GenericSerializer | ||
deser *avro.GenericDeserializer | ||
} | ||
|
||
type payload struct { | ||
Value int `json:"val"` | ||
} | ||
|
||
func NewAvroSerializer(srClient *schemaregistry.Client) (s *AvroSerializer, err error) { | ||
ser, err := avro.NewGenericSerializer(*srClient, serde.ValueSerde, avro.NewSerializerConfig()) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deser, err := avro.NewGenericDeserializer(*srClient, serde.ValueSerde, avro.NewDeserializerConfig()) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s = new(AvroSerializer) | ||
|
||
s.ser = ser | ||
s.deser = deser | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *AvroSerializer) CreateSerializedData(msgNum int, topic *string) ([]byte, error) { | ||
log.Debugf("Creating message with value %v", msgNum) | ||
val := payload{ | ||
Value: msgNum, | ||
} | ||
|
||
return s.ser.Serialize(*topic, &val) | ||
} | ||
|
||
func (s *AvroSerializer) DeserializeAndCheck(msgNum int, topic *string, buf []byte) (valid bool, err error) { | ||
val := payload{} | ||
err = s.deser.DeserializeInto(*topic, buf, &val) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
valid = (msgNum == val.Value) | ||
|
||
if !valid { | ||
log.Errorf("Mismatch: %v != %v", msgNum, val.Value) | ||
} | ||
|
||
return valid, nil | ||
} |
Oops, something went wrong.