GithubHelp home page GithubHelp logo

jaquiel / delphikafkaclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from norgepaul/delphikafkaclient

0.0 1.0 0.0 12.63 MB

Apache Kafka Client for Delphi

License: MIT License

Pascal 100.00%

delphikafkaclient's Introduction

DelphiKafkaClient

DelphiKafkaClient is a cross platform Delphi client/wrapper for Apache Kafka. Windows (i386/x64) and Linux (x64) are supported. Tested on Delphi 10.4, but should work with all modern Delphi releases.

Disclaimer

While it appears to work as expected, DelphiKafkaClient is a proof of concept and has never been tested in production.

Installation

You will need to install Apache Kafka - https://kafka.apache.org/quickstart

Linux

Install the librdkafka libray.

sudo apt install librdkafka-dev

Once unstalled, right click the Linux platform item under the KafkaDemo project and select Edit SDK. Click Update Local File Cache and wait for the operation to complete.

Windows

Copy the .dll files from the lib folder into the application output directory.

Code

All Kafka functionality can be accessed via the header translations in Kafka.Lib. To make life easier, many of the common Kafka functions are wrapped in the Kafka.Helper.TKafkaHelper class. In addition there are two additional interfaced classes dedicated to Producing and Consuming messages.

IKafkaProducer

uses
  Kafka.Lib, Kafka.Factory, Kafka.Interfaces, Kafka.Helper, Kafka.Types;
... 
  FKafkaProducer: IKafkaProducer;
... 
procedure TKafkaDemo.SendTestMessages(const Count: Integer);
var
  Msgs: TArray<String>;
  i: Integer;
begin
  // Create a new producer if required 
  if FKafkaProducer = nil then
  begin
    FKafkaProducer := TKafkaFactory.NewProducer(
      ['bootstrap.servers'],
      ['127.0.0.1:9092']);
  end;

  SetLength(Msgs, Trunc(edtMessageCount.Value));

  for i := 0 to pred(Trunc(edtMessageCount.Value)) do
  begin
    Msgs[i] := 'This is a test message' + ' - ' + DateTimeToStr(now) + '.' + MilliSecondOf(now).ToString.PadLeft(3, '0');
  end;

  FKafkaProducer.Produce(
    'test',
    Msgs,
    nil,
    0,
    RD_KAFKA_PARTITION_UA,
    RD_KAFKA_MSG_F_COPY,
    nil);

  TKafkaHelper.Flush(FKafkaProducer.KafkaHandle);      
end;

IKafkaConsumer

uses
  Kafka.Lib, Kafka.Factory, Kafka.Interfaces, Kafka.Helper, Kafka.Types;
...  
  FKafkaConsumer: IKafkaConsumer;
... 
procedure TKafkaDemo.StartConsuming;
begin
  if FKafkaConsumer = nil then
  begin
    FKafkaConsumer := TKafkaFactory.NewConsumer(
      ['group.id'],
      ['GroupID'],
      ['auto.offset.reset'],
      ['earliest'],
      '127.0.0.1:9092',
      ['test'],
      [0],
      procedure(const Msg: prd_kafka_message_t)
      begin
        // This is called from the consumer thread, but TKafka.Log is threadsafe
        TKafkaHelper.Log(format('Message received - %s', [TKafkaHelper.PointerToStr(Msg.payload, Msg.len)]), TKafkaLogType.kltConsumer);
      end);
  end;
end;

Thanks

Shouts go out to @Zhikter and @HeZiHang for some initial inspiration and code examples.

delphikafkaclient's People

Contributors

norgepaul 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.