Giter Club home page Giter Club logo

kafka-protocol-rs's Introduction

Kafka-Protocol Build crates.io docs.rs

Rust implementation of the Kafka wire protocol.

Unlike other Kafka protocol implementations, this project uses code generation to cover the entire Kafka API surface, including different protocol versions. See Kafka's repo for an example of protocol schema.

Versioning

Protocol messages are generated against a recent stable Kafka release, currently 3.7.0.

Although the Kafka protocol remains relatively stable and strives to be backwards compatible, new fields are occasionally added. In order to ensure forward compatibility with the protocol, this crate marks all exported items as #[non-exhaustive]. Protocol messages can be constructed using either Default::default or their provided builder.

Working with messages

Using Default::default:

use kafka_protocol::messages::{ApiKey, MetadataRequest, RequestHeader};
use kafka_protocol::protocol::StrBytes;

let mut header = RequestHeader::default();
header.client_id = Some(StrBytes::from_static_str("my-client"));
header.request_api_key = ApiKey::MetadataKey as i16;
header.request_api_version = 12;

let mut request = MetadataRequest::default();
request.topics = None;
request.allow_auto_topic_creation = true;

Using kafka_protocol::protocol::Builder:

use kafka_protocol::messages::{ApiKey, MetadataRequest, RequestHeader};
use kafka_protocol::protocol::{Builder, StrBytes};

let header = RequestHeader::builder()
    .client_id(Some(StrBytes::from_static_str("my-client")))
    .request_api_key(ApiKey::MetadataKey as i16)
    .request_api_version(12)
    .build();
!
let request = MetadataRequest::builder()
    .topics(None)
    .allow_auto_topic_creation(true)
    .build();

Serialization

Once a message has been created, it can be serialized using Encodable, writing the struct to a provided bytes::BytesMut. The API version for the given message matching the version specified in the request header must be provided.

use bytes::BytesMut;
use kafka_protocol::messages::MetadataRequest;
use kafka_protocol::protocol::Encodable;

let mut bytes = BytesMut::new();
let request = MetadataRequest::default();
request.encode(&mut bytes, 12).unwrap();

Deserialization

Messages can be decoded using Decodable and providing the matching API version from their corresponding request.

use bytes::Bytes;
use kafka_protocol::messages::ApiVersionsRequest;
use kafka_protocol::protocol::Decodable;

let bytes: [u8; 25] = [
        0x12, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2d,
        0x6b, 0x61, 0x66, 0x6b, 0x61, 0x2d, 0x6a, 0x61,
        0x76, 0x61, 0x06, 0x32, 0x2e, 0x38, 0x2e, 0x30,
        0x00
];
 
let res = ApiVersionsRequest::decode(&mut Bytes::from(bytes.to_vec()), 3).unwrap();

Development

Run cargo run -p protocol_codegen in the root path of this repo to generate/update the Rust codes via the latest Kafka protocol schema.

Originally implemented by @Diggsey in a minimal Kafka client implementation Franz

kafka-protocol-rs's People

Contributors

belltoy avatar bkirwi avatar davide-baldo avatar diggsey avatar etorreborre avatar gleyba avatar iamazy avatar rukai avatar thedodd avatar tychedelia avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.