-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Azure/kafka-support
Added kafka-support related changes
- Loading branch information
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
25 changes: 25 additions & 0 deletions
25
Sample-Code-Snippets/dotnet/EventHubs-Emulator-Kafka-Demo/EventHubs-Emulator-Kafka-Demo.sln
This file contains 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,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.11.35327.3 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHubs-Emulator-Kafka-Demo", "EventHubs-Emulator-Kafka-Demo\EventHubs-Emulator-Kafka-Demo.csproj", "{43E62434-CA3C-4FC1-8CED-FC11ECD65910}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{43E62434-CA3C-4FC1-8CED-FC11ECD65910}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{43E62434-CA3C-4FC1-8CED-FC11ECD65910}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{43E62434-CA3C-4FC1-8CED-FC11ECD65910}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{43E62434-CA3C-4FC1-8CED-FC11ECD65910}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {69FBA9D2-A274-41C3-BC6E-D90284DFD869} | ||
EndGlobalSection | ||
EndGlobal |
14 changes: 14 additions & 0 deletions
14
...bs-Emulator-Kafka-Demo/EventHubs-Emulator-Kafka-Demo/EventHubs-Emulator-Kafka-Demo.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<RootNamespace>EventHubs_Emulator_Kafka_Demo</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Confluent.Kafka" Version="2.5.2" /> | ||
</ItemGroup> | ||
</Project> |
78 changes: 78 additions & 0 deletions
78
...de-Snippets/dotnet/EventHubs-Emulator-Kafka-Demo/EventHubs-Emulator-Kafka-Demo/Program.cs
This file contains 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,78 @@ | ||
using Confluent.Kafka; | ||
|
||
class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
// Configuration | ||
var kafkaBootstrapServers = "localhost:9092"; | ||
var eventHubsConnectionString = "Endpoint=sb://localhost;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"; | ||
string eventHubName = "eh1"; | ||
|
||
|
||
// Producer | ||
int produceCount = 100; | ||
var producerConfig = new ProducerConfig | ||
{ | ||
BootstrapServers = kafkaBootstrapServers, | ||
SecurityProtocol = SecurityProtocol.SaslPlaintext, | ||
SaslMechanism = SaslMechanism.Plain, | ||
SaslUsername = "$ConnectionString", | ||
SaslPassword = eventHubsConnectionString | ||
}; | ||
using (var producer = new ProducerBuilder<Null, string>(producerConfig).Build()) | ||
{ | ||
for (int i = 0; i < produceCount; i++) | ||
{ | ||
var message = new Message<Null, string> { Value = $"Message {i}" }; | ||
var deliveryResult = await producer.ProduceAsync(eventHubName, message); | ||
Console.WriteLine($"Delivered '{deliveryResult.Value}' to '{deliveryResult.TopicPartitionOffset}'"); | ||
} | ||
} | ||
|
||
|
||
// Consumer | ||
int receiveCount = 0; | ||
string consumerGroupId = "cg1"; | ||
var consumerConfig = new ConsumerConfig | ||
{ | ||
BootstrapServers = kafkaBootstrapServers, | ||
SecurityProtocol = SecurityProtocol.SaslPlaintext, | ||
SaslMechanism = SaslMechanism.Plain, | ||
SaslUsername = "$ConnectionString", | ||
SaslPassword = eventHubsConnectionString, | ||
GroupId = consumerGroupId, | ||
EnableAutoCommit = true, | ||
AutoOffsetReset = AutoOffsetReset.Earliest | ||
}; | ||
using (var consumer = new ConsumerBuilder<Null, string>(consumerConfig).Build()) | ||
{ | ||
consumer.Subscribe(eventHubName); | ||
|
||
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(15)); | ||
Console.CancelKeyPress += (_, e) => { | ||
e.Cancel = true; | ||
cts.Cancel(); | ||
}; | ||
|
||
try | ||
{ | ||
while (receiveCount < produceCount) | ||
{ | ||
var cr = consumer.Consume(cts.Token); | ||
Console.WriteLine($"Consumed message '{cr.Message.Value}' from: '{cr.TopicPartitionOffset}'."); | ||
receiveCount += 1; | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
Console.WriteLine("Cancelled!"); | ||
} | ||
|
||
consumer.Close(); | ||
} | ||
|
||
Console.WriteLine($"Produced: {produceCount} messages"); | ||
Console.WriteLine($"Consumed: {receiveCount} messages"); | ||
} | ||
} |