Last active
November 22, 2017 21:16
-
-
Save picadoh/f0c9e66997f8c9a4987c04e83302c2a2 to your computer and use it in GitHub Desktop.
Kafka Consumer for .NET Core
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Confluent.Kafka" Version="0.11.2" /> | |
</ItemGroup> | |
</Project> |
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
namespace KafkaConsumer | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using Confluent.Kafka; | |
using Confluent.Kafka.Serialization; | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = new Dictionary<string, object> | |
{ | |
{ "group.id", "sample-consumer" }, | |
{ "bootstrap.servers", "localhost:9092" }, | |
{ "enable.auto.commit", "false"} | |
}; | |
using (var consumer = new Consumer<Null, string>(config, null, new StringDeserializer(Encoding.UTF8))) | |
{ | |
consumer.Subscribe(new string[]{"hello-topic"}); | |
consumer.OnMessage += (_, msg) => | |
{ | |
Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}"); | |
consumer.CommitAsync(msg); | |
}; | |
while (true) | |
{ | |
consumer.Poll(100); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment