-
Notifications
You must be signed in to change notification settings - Fork 95
feature: support kafka schema registry for Protobuf and Avro. #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ec1194e
Extracted KafkaSchemaRegistry out from AvroRowInputFormat
5c4a735
Schema registry support for Protobuf
3b348ef
refactored for auth support
af74188
fixed issues
5b13508
small cleanup
ba0661a
fixed and enabled Avro
20ed030
allow Avro data format to be used with schemar registry
1ed7a65
do not allow format_schema and schema_registry_url used at the same time
b64f437
small refactors
3c3994d
unwanted changes
dc46803
fixed format
866a3ea
fixed smoke tests
b696bb0
fixed per review comments
91eaae2
fixed a race condition
4cb8aa2
review comment
aa4c34f
fixed format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
| #include <Formats/KafkaSchemaRegistry.h> | ||
| #include <IO/HTTPCommon.h> | ||
| #include <IO/ReadHelpers.h> | ||
| #include <format> | ||
|
|
||
| #include <Poco/JSON/Parser.h> | ||
| #include <Poco/Net/HTTPBasicCredentials.h> | ||
| #include <boost/algorithm/string/predicate.hpp> | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
||
| namespace ErrorCodes | ||
| { | ||
| extern const int INCORRECT_DATA; | ||
| } | ||
|
|
||
| KafkaSchemaRegistry::KafkaSchemaRegistry(const String & base_url_, const String & credentials_) | ||
| : base_url(base_url_) | ||
| , logger(&Poco::Logger::get("KafkaSchemaRegistry")) | ||
| { | ||
| assert(!base_url.empty()); | ||
|
|
||
| if (auto pos = credentials_.find(':'); pos == credentials_.npos) | ||
| credentials.setUsername(credentials_); | ||
| else | ||
| { | ||
| credentials.setUsername(credentials_.substr(0, pos)); | ||
| credentials.setPassword(credentials_.substr(pos + 1)); | ||
| } | ||
| } | ||
|
|
||
| String KafkaSchemaRegistry::fetchSchema(UInt32 id) | ||
| { | ||
| try | ||
| { | ||
| try | ||
| { | ||
| Poco::URI url(base_url, std::format("/schemas/ids/{}", id)); | ||
| LOG_TRACE(logger, "Fetching schema id = {}", id); | ||
|
|
||
| /// One second for connect/send/receive. Just in case. | ||
| ConnectionTimeouts timeouts({1, 0}, {1, 0}, {1, 0}); | ||
|
|
||
| Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, url.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1); | ||
| request.setHost(url.getHost()); | ||
|
|
||
| if (!credentials.empty()) | ||
| credentials.authenticate(request); | ||
|
|
||
| auto session = makePooledHTTPSession(url, timeouts, 1); | ||
| std::istream * response_body{}; | ||
| try | ||
| { | ||
| session->sendRequest(request); | ||
|
|
||
| Poco::Net::HTTPResponse response; | ||
| response_body = receiveResponse(*session, request, response, false); | ||
| } | ||
| catch (const Poco::Exception & e) | ||
| { | ||
| /// We use session data storage as storage for exception text | ||
| /// Depend on it we can deduce to reconnect session or reresolve session host | ||
| session->attachSessionData(e.message()); | ||
| throw; | ||
| } | ||
| Poco::JSON::Parser parser; | ||
| auto json_body = parser.parse(*response_body).extract<Poco::JSON::Object::Ptr>(); | ||
| auto schema = json_body->getValue<std::string>("schema"); | ||
| LOG_TRACE(logger, "Successfully fetched schema id = {}\n{}", id, schema); | ||
| return schema; | ||
| } | ||
| catch (const Exception &) | ||
| { | ||
| throw; | ||
| } | ||
| catch (const Poco::Exception & e) | ||
| { | ||
| throw Exception(Exception::CreateFromPocoTag{}, e); | ||
| } | ||
| } | ||
| catch (Exception & e) | ||
| { | ||
| e.addMessage(std::format("while fetching schema with id {}", id)); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| UInt32 KafkaSchemaRegistry::readSchemaId(ReadBuffer & in) | ||
| { | ||
| uint8_t magic; | ||
| uint32_t schema_id; | ||
|
|
||
| try | ||
| { | ||
| readBinaryBigEndian(magic, in); | ||
| readBinaryBigEndian(schema_id, in); | ||
| } | ||
| catch (const Exception & e) | ||
| { | ||
| if (e.code() == ErrorCodes::CANNOT_READ_ALL_DATA) | ||
| /// empty or incomplete message without magic byte or schema id | ||
| throw Exception(ErrorCodes::INCORRECT_DATA, "Missing magic byte or schema identifier."); | ||
| else | ||
| throw; | ||
| } | ||
|
|
||
| if (magic != 0x00) | ||
| throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid magic byte before schema identifier." | ||
| " Must be zero byte, found 0x{:x} instead", magic); | ||
|
|
||
| return schema_id; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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,29 @@ | ||
| #pragma once | ||
|
|
||
| #include <IO/ReadBuffer.h> | ||
|
|
||
| #include <Poco/Net/HTTPBasicCredentials.h> | ||
| #include <Poco/URI.h> | ||
|
|
||
| namespace DB | ||
| { | ||
|
|
||
| /// A helper class helps working with Kafka schema registry. | ||
| class KafkaSchemaRegistry final | ||
| { | ||
| public: | ||
| static UInt32 readSchemaId(ReadBuffer & in); | ||
|
|
||
| /// \param credentials_ is expected to be formatted in "<username>:<password>". | ||
| KafkaSchemaRegistry(const String & base_url_, const String & credentials_); | ||
|
|
||
| String fetchSchema(UInt32 id); | ||
|
|
||
| private: | ||
| Poco::URI base_url; | ||
| Poco::Net::HTTPBasicCredentials credentials; | ||
|
|
||
| Poco::Logger* logger; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.