송/생산 json 메시지를 통해 카프카

0

질문

이 내용을 보고 카프카와 나은 계획을 사용하여 카프카니다.net

나는 알고 싶어하는 경우 난 보낼 수 있습니다 JSON 으로 메시지가 나는 생산 이벤트

나는 다음과 같은 튜토리얼: https://developer.confluent.io/get-started/dotnet/#build-producer

또한,방법은 없는 가치를 매핑할 수 있도록 모델치/json 구조는 항상 해당 모델

그래서 예를 들어 내가 원하는 경우 내 json 할 값

{
  "customerName":"anything",
  "eventType":"one-of-three-enums",
  "columnsChanged": "string value or something"
}

의 대부분은 예를 찾을 수 있습은 다음과 같다:

using Confluent.Kafka;
using System;
using Microsoft.Extensions.Configuration;

class Producer {
    static void Main(string[] args)
    {
        if (args.Length != 1) {
            Console.WriteLine("Please provide the configuration file path as a command line argument");
        }

        IConfiguration configuration = new ConfigurationBuilder()
            .AddIniFile(args[0])
            .Build();

        const string topic = "purchases";

        string[] users = { "eabara", "jsmith", "sgarcia", "jbernard", "htanaka", "awalther" };
        string[] items = { "book", "alarm clock", "t-shirts", "gift card", "batteries" };

        using (var producer = new ProducerBuilder<string, string>(
            configuration.AsEnumerable()).Build())
        {
            var numProduced = 0;
            const int numMessages = 10;
            for (int i = 0; i < numMessages; ++i)
            {
                Random rnd = new Random();
                var user = users[rnd.Next(users.Length)];
                var item = items[rnd.Next(items.Length)];

                producer.Produce(topic, new Message<string, string> { Key = user, Value = item },
                    (deliveryReport) =>
                    {
                        if (deliveryReport.Error.Code != ErrorCode.NoError) {
                            Console.WriteLine($"Failed to deliver message: {deliveryReport.Error.Reason}");
                        }
                        else {
                            Console.WriteLine($"Produced event to topic {topic}: key = {user,-10} value = {item}");
                            numProduced += 1;
                        }
                    });
            }

            producer.Flush(TimeSpan.FromSeconds(10));
            Console.WriteLine($"{numProduced} messages were produced to topic {topic}");
        }
    }
}

나는 다음과 같은 항목을 클래스에서 json 구조입니다.

.net apache-kafka asp.net-core
2021-11-23 21:53:21
1

최고의 응답

0

알고 싶어하는 경우 난 보낼 수 있습니다 JSON 으로 메시지가 나는 생산 이벤트

그렇습니다. 카프카의점 바이트로 변환합니다 바이트를 사용하여 Serializer. 건축할 때 프로듀서,당신은 옵션의 호출 SetValueSerializer.

부분의 내장에서 직렬화기에서 찾을 수 있습니다- https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/src/Confluent.Kafka/Serializers.cs

할 필요가 직접 작성하는 일반적으로 처리 JSON 모델 유형입니다.

을 사용할 때 Utf8Serializer 문자열해야 합 pre-직렬화하는 개체에서의 모델 클래스를 보내는 값입니다. 에를 들어,당신은 대체 var item 일부는 직렬화 개체입니다.

하도록 설정하려면 어떻게 해야 하 C#체로 JSON 문자열.NET?

할 때 사용하여 모델 클래스,데이터는 일반적으로 강력한 형식의 시작할 때까지 직접 작성 JSON 사용하거나 사전에 형식입니다. 당신이 원하는 외부 메시지 유효성 검사 Confluent 스키마이 레지스트리가 한 예를 지원하는 JSONSchema 고 JsonSerializerconfluent-dotnet-kafka 프로젝트를 지원합니다.

2021-11-23 22:27:28

그냥한 후속 질문입니다. 당신 경우 제한할 수 있는 메시지 크기와는 방법이 있 위해 프로듀서 확인하는 것은 크기의 이벤트에 보내기 전에 허용하지 않는 메시지를 보내는 경우는 크기 이상의 제한이 있나요?
Learn AspNet

카프카에는 기본 제한 1MB 메시지가 가치가 있습니다. 당신의 크기는 직렬화된 바이트 배열해야 하는 근사치의 개별 레코드 크기(거기에 추가적인 오버헤드 등과 같은 헤더를 기록하고 타임 스탬프,만)
OneCricketeer

감사합니다. 할 수 있습니 대답하십시오: stackoverflow.com/questions/70097676/...
Learn AspNet

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................