-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathschema_registry.cc
More file actions
108 lines (92 loc) · 3 KB
/
schema_registry.cc
File metadata and controls
108 lines (92 loc) · 3 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2024 Redpanda Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <redpanda/transform_sdk.h>
#include <iostream>
#include <string>
#include <string_view>
#include <system_error>
namespace {
namespace sr = redpanda::sr;
std::error_code
do_transform(redpanda::write_event event, redpanda::record_writer* writer);
std::unique_ptr<sr::schema_registry_client> sr_client;
constexpr std::string_view schema_def = R"({
"type": "record",
"name": "Example",
"fields": [
{"name": "a", "type": "long", "default": 0},
{"name": "b", "type": "string", "default": ""}
]
})";
} // namespace
int main() {
sr_client = sr::new_client();
if (
auto res = sr_client->create_schema(
"avro-value",
sr::schema::new_avro(
std::string{schema_def.data(), schema_def.size()}));
!res.has_value()) {
return 0;
}
redpanda::on_record_written(do_transform);
}
namespace {
std::error_code
do_transform(redpanda::write_event event, redpanda::record_writer* writer) {
auto rec_bytes = event.record.value;
if (!rec_bytes.has_value()) {
return std::make_error_code(std::errc::illegal_byte_sequence);
}
std::optional<sr::schema_id> id;
if (auto id_e = sr::decode_schema_id(rec_bytes.value()); id_e.has_value()) {
id.emplace(id_e.value().first);
rec_bytes = id_e.value().second;
} else {
return id_e.error();
}
std::optional<sr::schema> raw_schema;
if (
auto rs_e = sr_client->lookup_schema_by_id(id.value());
rs_e.has_value()) {
raw_schema.emplace(std::move(rs_e).value());
} else {
return rs_e.error();
}
// ?????
// auto schema = avro::schema::parse_str(raw_schema.value().schema());
std::optional<sr::subject_schema> latest_schema;
if (
auto latest_e = sr_client->lookup_latest_schema("avro-value");
latest_e.has_value()) {
latest_schema.emplace(std::move(latest_e).value());
} else {
return latest_e.error();
}
std::optional<sr::subject_schema> latest_direct;
if (
auto latest_e = sr_client->lookup_schema_by_version(
"avro-value", latest_schema->version);
latest_e.has_value()) {
latest_direct.emplace(std::move(latest_e).value());
} else {
return latest_e.error();
}
if (latest_schema != latest_direct) {
return std::make_error_code(std::errc::io_error);
}
std::cerr << "ALL IS GOOD" << std::endl;
return writer->write(event.record);
}
} // namespace