Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using Confluent.Kafka to Consume data, and my code as(c#):

C#
public static class Confluent
{
    public static void Consume()
    {
        var conf = new ConsumerConfig
        {
            GroupId = Guid.NewGuid().ToString(),
            BootstrapServers = "pdkafka.test.com:9092",//"localhost:9092",
            // Note: The AutoOffsetReset property determines the start offset in the event
            // there are not yet any committed offsets for the consumer group for the
            // topic/partitions of interest. By default, offsets are committed
            // automatically, so in this example, consumption will only start from the
            // earliest message in the topic 'my-topic' the first time you run the program.
            AutoCommitIntervalMs = 5000,
            AutoOffsetReset = AutoOffsetReset.Earliest,
            SecurityProtocol = SecurityProtocol.SaslPlaintext,
            SaslMechanism = SaslMechanism.Plain,
            SaslUsername= "test",
            SaslPassword= "123",

        };

        using (var c = new ConsumerBuilder<Ignore, string>(conf).Build())
        {
            c.Subscribe("prj.basedata");//c.Subscribe("test");//传入Topic Name

            CancellationTokenSource cts = new CancellationTokenSource();
            Console.CancelKeyPress += (_, e) =>
            {
                e.Cancel = true; // prevent the process from terminating.
                cts.Cancel();
            };

            try
            {
                while (true)
                {
                    try
                    {
                        var cr = c.Consume(cts.Token);
                        Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                    }
                    catch (ConsumeException e)
                    {
                        Console.WriteLine($"Error occured: {e.Error.Reason}");
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // Ensure the consumer leaves the group cleanly and final offsets are committed.
                c.Close();
            }
        }
    }
}


but the return message value like this:
\0\0\0\0\u0015\u0002\u001aSandra  LTC75\0\0\0\0\0\0\0\u0002\u001010604012\u0002\u0006ODM\u0002����W\u0002�����W\u0002�����X\u0002�Н��X\u0002�����X\u0002���ރY\u0002���ܯ[\u0002\u00023\u0002\u000e8810074\u0002\bSONY\0\0\u0002\u0002-\u0002�����W\u0002����X\u0002�����W\u0002\u0006LCM\u0002\fPCJ100\u0002����W\u0002�����W\u0002�����X\u0002�Н��X\u0002�����X\u0002���ރY\u0002���ܯ[\u0002\u0018TOM WH HUANG\u0002\u00187PD02M450001\0\u0002\fActive\0\u0002\u00021\u0002\u0002-\0\u0002\u0006132\u0002�\u0003\u0002\u0002I\u0002��Î�Y


What I have tried:

what should i do to make this normal?

please help,thanks a lot.
Posted
Updated 12-May-19 17:44pm
Comments
Richard MacCutchan 13-May-19 3:34am    
Go to the website that generates the data and ask them for details of the format and content.

1 solution

Quote:
but the return message value like this:
\0\0\0\0\u0015\u0002\u001aSandra  LTC75\0\0\0\0\0\0\0\u0002\u001010604012\u0002\u0006ODM\u0002����W\u0002�����W\u0002�����X\u0002�Н��X\u0002�����X\u0002���ރY\u0002���ܯ[\u0002\u00023\u0002\u000e8810074\u0002\bSONY\0\0\u0002\u0002-\u0002�����W\u0002����X\u0002�����W\u0002\u0006LCM\u0002\fPCJ100\u0002����W\u0002�����W\u0002�����X\u0002�Н��X\u0002�����X\u0002���ރY\u0002���ܯ[\u0002\u0018TOM WH HUANG\u0002\u00187PD02M450001\0\u0002\fActive\0\u0002\u00021\u0002\u0002-\0\u0002\u0006132\u0002�\u0003\u0002\u0002I\u0002��Î�Y

Obviously, the message you receive contain non printable chars, This is a common technique to encode complex data in a message, As of today, we use xml encoding. You need to know how the message is encoded in order to decode it.
Another problem is that you try to display the message in utf8 when it is probably not.
Advice: paste the message in a programmer's editor and switch to hex mode, it may help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900