Creating a Topic in Kafka on startup for a test #6990
-
I am using Aspire for local development and I would like to have the Kafka topics created immediately on startup, is there any way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
If there's no aspire way you can probably use the Confluent AdminClientBuilder
|
Beta Was this translation helpful? Give feedback.
-
You can run code using the ResourceReadyEvent, this will fire after the kafka container is healthy: https://learn.microsoft.com/en-us/dotnet/aspire/app-host/eventing#resource-eventing Then as @krisnashaypp said, you can use the kafka client apis to connect and write code to seed topics. We're working on making this slightly better in 9.1 but the pattern is the same. Note, this only works in development. Here's an example of doing it with mongodb: var db = builder.AddMongoDB("mongo")
.WithMongoExpress(c => c.WithHostPort(3022))
.AddDatabase("db");
builder.Eventing.Subscribe<ResourceReadyEvent>(db.Resource, async (@event, ct) =>
{
// Seed the database with some data
var cs = await db.Resource.ConnectionStringExpression.GetValueAsync(ct);
using var client = new MongoClient(cs);
const string collectionName = "entries";
var myDb = client.GetDatabase("db");
await myDb.CreateCollectionAsync(collectionName, cancellationToken: ct);
for (int i = 0; i < 10; i++)
{
await myDb.GetCollection<Entry>(collectionName).InsertOneAsync(new Entry(), cancellationToken: ct);
}
}); |
Beta Was this translation helpful? Give feedback.
Thanks - it worked flawlessly. FWIW, here is my complete code snippet in AppHost: