Skip to content

Instantly share code, notes, and snippets.

@picadoh
Last active November 22, 2017 21:16
Show Gist options
  • Save picadoh/f0c9e66997f8c9a4987c04e83302c2a2 to your computer and use it in GitHub Desktop.
Save picadoh/f0c9e66997f8c9a4987c04e83302c2a2 to your computer and use it in GitHub Desktop.
Kafka Consumer for .NET Core
<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>
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