Authentication and database connection using the Kafka API
Endpoint
The Kafka API endpoint appears in the management console
The endpoint has the following format: <FQDN_YDB>:PORT. For example, ydb-01.serverless.yandexcloud.net:9093.
Prerequisites
To authenticate, take these steps:
- Create a service account.
- Assign the following roles to the service account:
ydb.kafkaApi.clientandydb.viewer: for reading from a data stream.ydb.kafkaApi.clientandydb.editor: for writing to a data stream.
- Create an API key with the
yc.ydb.topics.managescope.
Authentication
The Kafka API uses the SASL_SSL/PLAIN
The following parameters are required:
-
<database>: Database path. The database path appears in the management console under: Data Streams → [Your Stream] → Overview → Endpoint (a substring followingdatabase=).For example, if the Endpoint field contains
grpcs://ydb.serverless.yandexcloud.net:2135/?database=/ru-central1/b1gia87mbaomkfvs6rgl/etnudu2n9ri35luqe4h1, the database path is/ru-central1/b1gia87mbaomkfvs6rgl/etnudu2n9ri35luqe4h1. -
<api-key>: API key.
These parameters will be used for authentication when reading and writing messages:
<sasl.username>=@<database>(Note that the database path must be prefixed with the@symbol)<sasl.password>=<api-key>
Example of writing and reading a message
This example uses the following parameters:
<kafka-api-endpoint>: Endpoint.<stream-name>: Data stream name.
-
If you are using a dedicated database, you need to Install an SSL certificate:
sudo mkdir -p /usr/local/share/ca-certificates/Yandex/ && \ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \ --output-document /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt && \ sudo chmod 0655 /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crtThe certificate will be saved to the
/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crtfile. -
Install
kcat, an open-source tool for producing and consuming data:sudo apt-get install kafkacat -
Run the following command to get messages from the stream:
Serverless databaseDedicated databasekcat -C \ -b <kafka-api-endpoint> \ -t <stream-name> \ -X security.protocol=SASL_SSL \ -X sasl.mechanism=PLAIN \ -X sasl.username="<sasl.username>" \ -X sasl.password="<sasl.password>"kcat -C \ -b <kafka-api-endpoint> \ -t <stream-name> \ -X security.protocol=SASL_SSL \ -X sasl.mechanism=PLAIN \ -X sasl.username="<sasl.username>" \ -X sasl.password="<sasl.password>" \ -X ssl.ca.location=/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crtThis command will continuously read new messages from the stream.
-
In a separate terminal, run the following command to send a message to the stream:
Serverless databaseDedicated databaseecho "test message" | kcat -P \ -b <kafka-api-endpoint> \ -t <stream-name> \ -k key \ -X security.protocol=SASL_SSL \ -X sasl.mechanism=PLAIN \ -X sasl.username="<sasl.username>" \ -X sasl.password="<sasl.password>"echo "test message" | kcat -P \ -b <kafka-api-endpoint> \ -t <stream-name> \ -k key \ -X security.protocol=SASL_SSL \ -X sasl.mechanism=PLAIN \ -X sasl.username="<sasl.username>" \ -X sasl.password="<sasl.password>" \ -X ssl.ca.location=/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt
For details on working with Data Streams via the Kafka API and more examples, refer to the YDB documentation