Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: using AsyncAPI with Kafka using Avro #2329

Merged
merged 26 commits into from
Feb 10, 2024
Merged
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8e2fe00
initial draft
J0SAL Nov 17, 2023
2b580a6
added temp files for preview
J0SAL Nov 17, 2023
f78f20c
updated introduction
J0SAL Nov 17, 2023
60b0aad
fix - update payload to schema
J0SAL Nov 22, 2023
ae46832
added section with json avro schema
J0SAL Nov 29, 2023
42773ee
Merge branch 'asyncapi:master' into patch-1
J0SAL Dec 26, 2023
eeaa49a
Apply suggestions from code review
J0SAL Dec 26, 2023
fa1557e
Apply suggestions from code review
J0SAL Dec 26, 2023
65e8e04
updated external avro section
J0SAL Dec 26, 2023
57a9db2
added hypelinks and updated intro
J0SAL Dec 27, 2023
cb59c07
Apply suggestions from code review
J0SAL Jan 4, 2024
3402fc6
Merge branch 'asyncapi:master' into patch-1
J0SAL Jan 4, 2024
2054ddf
Merge branch 'asyncapi:master' into patch-1
J0SAL Jan 17, 2024
b307363
minor fixes
J0SAL Jan 17, 2024
442540a
removed index file
J0SAL Jan 17, 2024
0506eae
spelling fix
J0SAL Jan 17, 2024
03f860b
updated hyperlink text of prev tutorial
J0SAL Jan 23, 2024
483313b
Merge branch 'asyncapi:master' into patch-1
J0SAL Jan 23, 2024
9467337
updated background context
J0SAL Jan 24, 2024
25f7824
Merge branch 'asyncapi:master' into patch-1
J0SAL Jan 24, 2024
ddde250
Apply suggestions from code review
J0SAL Jan 25, 2024
c09ac05
Merge branch 'master' into patch-1
quetzalliwrites Jan 30, 2024
122915f
Merge branch 'master' into patch-1
quetzalliwrites Feb 9, 2024
6b82e12
Delete pages/docs/tutorials/kafka/_section.md
quetzalliwrites Feb 9, 2024
b1f0e55
Merge branch 'master' into patch-1
quetzalliwrites Feb 10, 2024
b548cf4
tw editorial fix
quetzalliwrites Feb 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions pages/docs/tutorials/kafka/configure-kafka-avro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
---
title: Describe Kafka message payload using Avro Schema
description: Explore configuring AsyncAPI document for Kafka messages with Avro Schema.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved
weight: 30
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved
---

## Introduction

The previous tutorial on [creating an AsyncAPI document for applications consuming from Kafka](/docs/tutorials/kafka) taught you about writing an AsyncAPI document for Kafka messages using the default schema. This tutorial will teach you to write the same document using Avro Schema.

While AsyncAPI schema can be the default choice for describing payloads, many prefer using Avro Schemas to define messages in Kafka. Through this tutorial, you'll learn to modify your existing AsyncAPI schema to add Avro schema into your document in both YAML and JSON formats.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

## Background Context
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

[AsyncAPI](https://www.asyncapi.com/) is a specification for describing Event-Driven Architectures (EDAs) in a machine-readable format. AsyncAPI schema outlines the format and content specifications that enable a consistent representation of agreements for communication between services in an Event-Driven Architecture.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

[Avro](https://avro.apache.org/), on the other hand is an efficient binary serialization format, used to ensure schema-aware communications between the messages within Apache Kafka. With Avro Schema there is a standardized method for serializing data allowing for interoperability and schema evolution capabilities. Avro promotes data exchange among systems by offering a common schema that fosters compatibility, between different components.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

## Defining message payload with Avro Schema directly in the AsyncAPI document
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

Defining message schema with the default schema is already covered in the [previous tutorial](/docs/tutorials/kafka). The default choice was the AsyncAPI schemas which is a superset of JSON Schema that looked as follows,
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```
messages:
userSignedUp:
payload:
type: object
properties:
userId:
type: integer
description: This property describes the ID of the user
userEmail:
type: string
description: This property describes the email of the user
```

In this section, let's shift our focus to defining messages using Avro Schemas directly within our document.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```
messages:
userSignedUp:
payload:
schemaFormat: 'application/vnd.apache.avro;version=1.9.0'
schema:
type: record
name: UserSignedUp
namespace: com.company
doc: User sign-up information
fields:
- name: userId
type: int
- name: userEmail
type: string
```

In the above snippet:
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved
- The `userSignedUp` message is defined with Avro Schema, using the specified `schemaFormat` and the `schema`.
- Use the `schemaFormat` to specify using a MIME type, that you use Avro and what version of Avro Schema.
- The `schema` includes a `record` named `UserSignedUp` within the `com.company` namespace. It also describes two fields, `userId` and `userEmail`, defining their data types as `int` and `string` respectively.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

By combining the Avro Schema discussed above into the previous tutorial, you'll have an AsyncAPI document fully equipped with Avro Schema!
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

```
J0SAL marked this conversation as resolved.
Show resolved Hide resolved
asyncapi: 3.0.0
info:
title: User Signup API
version: 1.0.0
description: The API notifies you whenever a new user signs up in the application.
servers:
kafkaServer:
host: test.mykafkacluster.org:8092
description: Kafka Server
protocol: kafka
operations:
onUserSignedUp:
action: receive
channel:
$ref: '#/channels/userSignedUp'
channels:
userSignedUp:
description: This channel contains a message per each user who signs up in our application.
address: user_signedup
messages:
userSignedUp:
$ref: '#/components/messages/userSignedUp'
components:
messages:
userSignedUp:
payload:
schemaFormat: 'application/vnd.apache.avro;version=1.9.0'
schema:
type: record
name: UserSignedUp
namespace: com.company
doc: User sign-up information
fields:
- name: userId
type: int
- name: userEmail
type: string
```

## Reusing existing Avro Schemas

Occasionally you might find yourself with a set of existing Avro Schemas. In such cases, instead of defining these schemas again anew in your AsyncAPI document, you can integrate them seamlessly by calling out existing files.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

Assume you have a file named `userSchema.json` that encapsulates the Avro Schema that resembles the following:

```
// userSchema.json
{
"type": "record",
"name": "UserSignedUp",
"namespace": "com.company",
"doc": "User sign-up information",
"fields": [
{ "name": "userId", "type": "int" },
{ "name": "userEmail", "type": "string" }
]
}
```

To seamlessly incorporate this existing Avro schema into your AsyncAPI document, you can use the `$ref` property to reference the path to the JSON file. This way, your AsyncAPI document will incorporate the Avro Schema from the external JSON file, ensuring consistency and interoperability in your Kafka ecosystem.

```
asyncapi: 3.0.0
info:
title: User Signup API
version: 1.0.0
description: The API notifies you whenever a new user signs up in the application.
servers:
kafkaServer:
host: test.mykafkacluster.org:8092
description: Kafka Server
protocol: kafka
operations:
onUserSignedUp:
action: receive
channel:
$ref: '#/channels/userSignedUp'
channels:
userSignedUp:
description: This channel contains a message per each user who signs up in our application.
address: user_signedup
messages:
userSignedUp:
$ref: '#/components/messages/userSignedUp'
components:
messages:
userSignedUp:
payload:
schemaFormat: 'application/vnd.apache.avro;version=1.9.0'
schema:
$ref: './userSchema.json'
```

## Summary

Through this tutorial, you have smoothly progressed from the default schema to utilizing the capabilities of Avro Schema. The use of Avro Schema with AsyncAPI ensures improved interoperability and schema-aware communication in event-driven systems. Now, you can further experiment by incorporating your business logic and experimenting with more advanced capabilities.
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved

## Next Steps
Now you know how to write an AsyncAPI document using Avro Schemas. Let's now proceed to learn to how use [Schema Registry with AsyncAPI](/docs/tutorials/kafka/managing-schemas-using-schema-registry).
quetzalliwrites marked this conversation as resolved.
Show resolved Hide resolved
Loading