I need to clear or delete Kafka topics programmatically using C# language. Currently, I have used Confluent.Kafka library for publishing and consuming Kafka topics.
I can delete Kafka topics using the command line like this
kafka-topics.bat --zookeeper 192.108.94.79:2181 --delete --topic test-topic3
Is any library or way available for clearing Kafka topic programmatically using C# language?
In version 1.3.0 of confluent.kafka AdminClient class is internal,
so you have to use AdminClientBuilder
Example:
AdminClientBuilder builder = new AdminClientBuilder(new AdminClientConfig() { BootstrapServers =""})
builder.Build();
We can quickly delete the Kafka topics using Confluent.Kafka library version 1.0.0. But currently, it's in beta release. This library support Kafka admin utilities. Following code helps to clear/delete Kafka topics.
using Confluent.Kafka;
using System;
using System.Collections.Generic;
namespace deleteKafkaTopic
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine($"librdkafka Version: {Library.VersionString} ({Library.Version:X})");
Console.WriteLine($"Debug Contexts: {string.Join(", ", Library.DebugContexts)}");
IEnumerable<string> topicList = new List<string>() { "test-topic4" };
deleteTopics("192.168.64.49:9092", topicList);
}
static void deleteTopics(string brokerList, IEnumerable<string> topicNameList)
{
using (var adminClient = new AdminClient(new AdminClientConfig { BootstrapServers = brokerList }))
{
adminClient.DeleteTopicsAsync(topicNameList, null);
}
}
}
}
Related
I am trying to find the best way to see the last date a subscription in a topic was accessed via c# (SDK or otherwise) i.e. to purge the queue if not accessed in over x hours. I know there is that functionality built into the service bus explorer but have not been able to find any SDK functionality. If anyone could point me in the right direction it would be appreciated.
Please see the code below. It uses Azure.Messaging.ServiceBus SDK. The properties you're interested in is available in SubscriptionRuntimeProperties class.
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus.Administration;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
string connectionString =
"connection-string";
string topicName = "topic-name";
string subscriptionName = "subscription-name";
ServiceBusAdministrationClient administrationClient = new ServiceBusAdministrationClient(connectionString);
var result = await administrationClient.GetSubscriptionRuntimePropertiesAsync(topicName, subscriptionName);
Console.WriteLine(result.Value.AccessedAt.ToString("yyyy-MM-ddTHH:mm:ss"));
}
}
}
Microsoft will deprecated support of Classic API for Service Bus at November 2021 (as described here)
In our code we use WindowsAzure.ServiceBus package.
It is an ol package and Microsoft suggets to use new Azure.Messaging.ServiceBus package.
WindowsAzure.ServiceBus package contain GetQueues(String) method. This method can use filter parameter for filtration queues by name or properties. It is very useful if a ServiceBus has many Queues.
But I can't find equivalent of this feature in new Azure.Messaging.ServiceBus package.
How can I implement filter feature in new package?
Thanks for any help.
How can I implement filter feature in new package?
You will need to use GetQueuesAsync method in ServiceBusAdministrationClient class to get this information.
Please see the sample code:
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus.Administration;
namespace SO67703647
{
class Program
{
static string connectionString = "your-connection-string";
static async Task Main(string[] args)
{
var adminClient = new ServiceBusAdministrationClient(connectionString);
var queuesListingResult = adminClient.GetQueuesAsync();
await foreach (var item in queuesListingResult)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("=======================");
Console.WriteLine("Press any key to terminate the application.");
Console.ReadKey();
}
}
}
I am developing an app which requires SMS gateway service. Our company uses Plivo service. I followed along the sample code.
When I tried to build solution I received two errors:
Error 1:The type 'RestSharp.IRestResponse'1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'RestSharp, Version=105.1.0.0, Culture=neutral, PublicKeyToken=null'
Error 2:Cannot implicitly convert type 'RestSharp.IRestResponse'1<Plivo.API.MessageResponse>' to 'RestSharp.IRestResponse<Plivo.API.MessageResponse>'
I don't get the reasons for these errors since I installed Plivo and RestSharp API's through NuGet and can see the dll's in Solution Explorer.
The second error is even more confusing to me because of the strange type 'RestSharp.IRestResponse'1.
If anyone could advise me on this issues I'd be very grateful.
My source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
using Plivo.API;
using System.Reflection;
namespace SMSGatewayTest
{
class Program
{
static void Main(string[] args)
{
string auth_id = "XXX"; // obtained from Plivo account dashboard
string auth_token = "YYY"; // obtained from Plivo account dashboard
RestAPI plivo = new RestAPI(auth_id, auth_token);
IRestResponse<MessageResponse> resp = plivo.send_message(new Dictionary<string, string>()
{
{ "src", "37061549145" },
{ "dst", "37068824525" },
{ "text", "Hi, text from Plivo." },
});
if (resp.Data != null)
{
PropertyInfo[] proplist = resp.Data.GetType().GetProperties();
foreach (PropertyInfo property in proplist)
Console.WriteLine("{0}: {1}", property.Name, property.GetValue(resp.Data, null));
}
else
{
Console.WriteLine(resp.ErrorMessage);
}
}
}
}
P.S. If I missed something while writing a question, please, ask for it - it's my first experience with SMS-gateway
So after a few hours of investigating I managed to find out the case. It seems that automatic NuGet installation install RestSharp version 100.0.0, and you need 105.1.0.0. The solution was to enter the following in NuGet console:
Install-Package RestSharp -Version 105.0.1
As you may know Windows Explorer allows to mount ISO files to a virtual drive.
Is there any API which can be used to do this?
The native function call AttachVirtualDisk.
However, if you are using C# like your tags suggest it may be easier to just call out to PowerShell and use its wrapper around that function Mount-DiskImage
using System.Management.Automation;
namespace IsoMountTest
{
internal class Program
{
private static void Main(string[] args)
{
var isoPath = #"C:\Foo\bar.iso";
using (var ps = PowerShell.Create())
{
ps.AddCommand("Mount-DiskImage").AddParameter("ImagePath", isoPath).Invoke();
}
}
}
}
I've been trying to evaluate XSockets but it appears I've hit my first stumbling block pretty early. I can connect to Generic controller just fine but custom controllers do not seem to work at all - I get a custom message: "The handler name was not found in loaded plugins". A Google search shows one other person having this problem in SE, but their solution did not work for me.
I've created a console project and installed the latest XSockets 3.03 from NuGet. My code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSockets.Core.Common.Socket;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using XSockets.Core.Common.Socket.Event.Interface;
namespace XSockets2
{
class Program
{
static void Main(string[] args)
{
using (var server = XSockets.Plugin.Framework.Composable.GetExport<IXSocketServerContainer>())
{
Console.WriteLine("running!");
server.StartServers();
Console.ReadLine();
server.StopServers();
}
}
}
public class TestCont: XSocketController
{
public override void OnMessage(ITextArgs textArgs)
{
this.SendToAll(textArgs);
}
}
}
And my Javascript
function connect2() {
var host = "ws://localhost:4502/testcont";
var conn;
conn = new XSockets.WebSocket(host);
conn.on(XSockets.Events.open, function (clientInfo) {
message(clientInfo.ClientGuid); //appends message to textarea
console.log('Open', clientInfo);
});
conn.on('OnMessage', function (d) {
message(d);
console.log('Message', d);
});
conn.on(XSockets.Events.onError, function (err) {
message(err.CustomMessage);
console.log('Error', err);
});
conn.on(XSockets.Events.close, function () {
message('Closed');
console.log('Closed');
});
First of all the latest version is 3.0.2 (not 3.0.3) but that is not important :)
There is a well known and documented bug in the plugin framework for the latest version. The bug only affect you if you run a console application (or any other *.exe) project since xsockets by default only looks in *.dll and not *.exe.
The issue and work around is described here
But your code will not work anyway since you have an error (from what I can see).
Your controller is named "TestCont" but you connect to "testcont". The connectionstring is case sensitive.
EDIT: I also think you are missunderstanding the method OnMessage since you have added a subscription to that exact name.