다음을 통해 공유


Validate schemas for Apache Kafka applications using Avro (Java)

In this quickstart guide, we explore how to validate event from Apache Kafka applications using Azure Schema Registry for Event Hubs.

In this use case a Kafka producer application uses Avro schema stored in Azure Schema Registry to, serialize the event and publish them to a Kafka topic/event hub in Azure Event Hubs. Kafka 소비자는 Event Hubs에서 사용하는 이벤트를 역직렬화합니다. For that it uses schema ID of the event and the Avro schema, which is stored in Azure Schema Registry.

Diagram showing schema serialization/de-serialization for Kafka applications using Avro schema.

필수 조건

Azure Event Hubs를 처음 사용하는 경우 이 빠른 시작을 수행하기 전에 Event Hubs 개요를 참조하세요.

이 빠른 시작을 완료하려면 다음 필수 구성 요소가 필요합니다.

이벤트 허브 만들기

빠른 시작의 지침에 따라 Event Hubs 네임스페이스 및 이벤트 허브를 생성하여 Event Hubs 네임스페이스 및 이벤트 허브를 만듭니다. 그런 다음 연결 문자열 가져오기의 지침에 따라 Event Hubs 네임스페이스에 대한 연결 문자열을 가져옵니다.

현재 빠른 시작에서 사용하는 다음 설정을 적어 둡니다.

  • Event Hubs 네임스페이스에 대한 연결 문자열
  • 이벤트 허브의 이름

스키마 만들기

스키마 레지스트리를 사용하여 스키마 만들기의 지침에 따라 스키마 그룹 및 스키마를 만듭니다.

  1. 스키마 레지스트리 포털을 사용하여 contoso-sg 라는 스키마 그룹을 만듭니다. Use Avro as the serialization type and None for the compatibility mode.

  2. In that schema group, create a new Avro schema with schema name: Microsoft.Azure.Data.SchemaRegistry.example.Order using the following schema content.

    {
      "namespace": "Microsoft.Azure.Data.SchemaRegistry.example",
      "type": "record",
      "name": "Order",
      "fields": [
        {
          "name": "id",
          "type": "string"
        },
        {
          "name": "amount",
          "type": "double"
        },
        {
          "name": "description",
          "type": "string"
        }
      ]
    } 
    

스키마 레지스트리에 액세스하는 애플리케이션 등록

You can use Microsoft Entra ID to authorize your Kafka producer and consumer application to access Azure Schema Registry resources by registering your client application with a Microsoft Entra tenant from the Azure portal.

Microsoft Entra 애플리케이션을 example-app 등록하려면 Microsoft Entra 테넌트에 애플리케이션 등록을 참조하세요.

  • tenant.id - 애플리케이션의 테넌트 ID 설정
  • client.id - 애플리케이션의 클라이언트 ID 설정
  • client.secret - 인증을 위한 클라이언트 암호 설정

관리 ID를 사용하는 경우 다음이 필요합니다.

  • use.managed.identity.credential - MSI 자격 증명을 사용해야 하며 MSI 사용 VM에 사용해야 임을 나타냅니다.
  • managed.identity.clientId - if specified, it builds MSI credential with given client ID
  • managed.identity.resourceId - if specified, it builds MSI credential with given resource ID

스키마 레지스트리 읽기 권한자 역할에 사용자 추가

네임스페이스 수준에서 스키마 레지스트리 읽기 권한자 역할에 사용자 계정을 추가합니다. 스키마 레지스트리 기여자 역할을 사용할 수도 있지만 이 빠른 시작에는 필요하지 않습니다.

  1. Event Hubs 네임스페이스 페이지의 왼쪽 메뉴에서 액세스 제어(IAM)를 선택합니다.
  2. 액세스 제어(IAM) 페이지에서 + 추가 ->역할 할당 추가를 선택합니다.
  3. 과제 유형 페이지에서 다음을 선택합니다.
  4. On the Roles page, select Schema Registry Reader (Preview), and then select Next at the bottom of the page.
  5. + 멤버 선택 링크를 사용하여 이전 단계에서 만든 애플리케이션을 역할에 추가 example-app 한 다음, 다음을 선택합니다.
  6. 검토 + 할당 페이지에서 검토 + 할당을 선택합니다.

Kafka 애플리케이션의 클라이언트 애플리케이션 구성 업데이트

You need to update the client configuration of the Kafka producer and consumer applications with the configuration related to Microsoft Entra application that we created and the schema registry information.

To update the Kafka Producer configuration, navigate to azure-schema-registry-for-kafka/tree/master/java/avro/samples/kafka-producer.

  1. Event Hubs에 대한 Kafka 빠른 시작 가이드에 따라 src/main/resources/app.properties에서 Kafka 애플리케이션의 구성을 업데이트합니다.

  2. Update the configuration details for the producer in src/main/resources/app.properties using schema registry related configuration and Microsoft Entra application that you created above as follows:

    schema.group=contoso-sg
    schema.registry.url=https://<NAMESPACENAME>.servicebus.windows.net
    
     tenant.id=<>
     client.id=<>
     client.secret=<>
    
  3. Follow the same instructions and update the azure-schema-registry-for-kafka/tree/master/java/avro/samples/kafka-consumer configuration as well.

  4. For both Kafka producer and consumer applications, following Avro schema is used:

    {
      "namespace": "com.azure.schemaregistry.samples",
      "type": "record",
      "name": "Order",
      "fields": [
        {
          "name": "id",
          "type": "string"
        },
        {
          "name": "amount",
          "type": "double"
        },
        {
          "name": "description",
          "type": "string"
        }
      ]
    }
    

Using Kafka producer with Avro schema validation

To run the Kafka producer application, navigate to azure-schema-registry-for-kafka/tree/master/java/avro/samples/kafka-producer.

  1. You can run the producer application so that it can produce Avro specific records or generic records. 특정 레코드 모드의 경우 먼저 다음 maven 명령을 사용하여 생산자 스키마에 대해 클래스를 생성해야 합니다.

    mvn generate-sources
    
  2. 그런 다음, 다음 명령을 사용하여 생산자 애플리케이션을 실행할 수 있습니다.

    mvn clean package
    mvn -e clean compile exec:java -Dexec.mainClass="com.azure.schemaregistry.samples.producer.App"
    
  3. 생산자 애플리케이션을 성공적으로 실행하면 생산자 시나리오를 선택하라는 메시지가 표시됩니다. For this quickstart, you can choose option 1 - produce Avro SpecificRecords.

    Enter case number:
    1 - produce Avro SpecificRecords
    2 - produce Avro GenericRecords
    
  4. 데이터 직렬화 및 게시에 성공하면 생산자 애플리케이션에 다음 콘솔 로그가 표시됩니다.

    INFO com.azure.schemaregistry.samples.producer.KafkaAvroSpecificRecord - Sent Order {"id": "ID-0", "amount": 10.0, "description": "Sample order 0"}
    INFO com.azure.schemaregistry.samples.producer.KafkaAvroSpecificRecord - Sent Order {"id": "ID-1", "amount": 11.0, "description": "Sample order 1"}
    INFO com.azure.schemaregistry.samples.producer.KafkaAvroSpecificRecord - Sent Order {"id": "ID-2", "amount": 12.0, "description": "Sample order 2"}
    

Using Kafka consumer with Avro schema validation

To run the Kafka consumer application, navigate to azure-schema-registry-for-kafka/tree/master/java/avro/samples/kafka-consumer.

  1. You can run the consumer application so that it can consume Avro specific records or generic records. 특정 레코드 모드의 경우 먼저 다음 maven 명령을 사용하여 생산자 스키마에 대해 클래스를 생성해야 합니다.

    mvn generate-sources
    
  2. 그런 다음, 다음 명령을 사용하여 소비자 애플리케이션을 실행할 수 있습니다.

    mvn clean package
    mvn -e clean compile exec:java -Dexec.mainClass="com.azure.schemaregistry.samples.consumer.App"
    
  3. 소비자 애플리케이션을 성공적으로 실행하면 생산자 시나리오를 선택하라는 메시지가 표시됩니다. For this quickstart, you can choose option 1 - consume Avro SpecificRecords.

    Enter case number:
    1 - consume Avro SpecificRecords
    2 - consume Avro GenericRecords
    
  4. 데이터 사용 및 역직렬화에 성공하면 생산자 애플리케이션에 다음 콘솔 로그가 표시됩니다.

    INFO com.azure.schemaregistry.samples.consumer.KafkaAvroSpecificRecord - Order received: {"id": "ID-0", "amount": 10.0, "description": "Sample order 0"}
    INFO com.azure.schemaregistry.samples.consumer.KafkaAvroSpecificRecord - Order received: {"id": "ID-1", "amount": 11.0, "description": "Sample order 1"}
    INFO com.azure.schemaregistry.samples.consumer.KafkaAvroSpecificRecord - Order received: {"id": "ID-2", "amount": 12.0, "description": "Sample order 2"}
    

리소스 정리

Event Hubs 네임스페이스를 삭제하거나 네임스페이스가 포함된 리소스 그룹을 삭제합니다.