快速入门:使用 Kafka 协议通过事件中心进行数据流式传输Quickstart: Data streaming with Event Hubs using the Kafka protocol

此快速入门介绍如何在不更改协议客户端或运行自己的群集的情况下将数据流式传输到事件中心。This quickstart shows how to stream into Event Hubs without changing your protocol clients or running your own clusters. 你将了解如何只需更改应用程序配置,即可使用生产者和使用者与事件中心通信。You learn how to use your producers and consumers to talk to Event Hubs with just a configuration change in your applications. Azure 事件中心支持 Apache Kafka 版本 1.0Azure Event Hubs supports Apache Kafka version 1.0.

备注

GitHub 上提供了此示例This sample is available on GitHub

先决条件Prerequisites

若要完成本快速入门,请确保符合以下先决条件:To complete this quickstart, make sure you have the following prerequisites:

创建事件中心命名空间Create an Event Hubs namespace

当你创建标准层事件中心命名空间时,系统会自动为该命名空间启用 Kafka 终结点。When you create a standard tier Event Hubs namespace, the Kafka endpoint for the namespace is automatically enabled. 可以将事件从使用 Kafka 协议的应用程序流式传输到标准层事件中心。You can stream events from your applications that use the Kafka protocol into standard tier Event Hubs. 按照使用 Azure 门户创建事件中心中的分步说明创建标准层事件中心命名空间。Follow step-by-step instructions in the Create an event hub using Azure portal to create a standard tier Event Hubs namespace.

备注

Kafka 的事件中心仅在标准专用层上可用。Event Hubs for Kafka is available only on standard and dedicated tiers. 基本 层不支持事件中心上的 Kafka。The basic tier doesn't support Kafka on Event Hubs.

在事件中心内使用 Kafka 发送和接收消息Send and receive messages with Kafka in Event Hubs

  1. 克隆用于 Kafka 的 Azure 事件中心存储库Clone the Azure Event Hubs for Kafka repository.

  2. 导航到 azure-event-hubs-for-kafka/quickstart/java/producerNavigate to azure-event-hubs-for-kafka/quickstart/java/producer.

  3. src/main/resources/producer.config 中更新生产者的配置详细信息,如下所示:Update the configuration details for the producer in src/main/resources/producer.config as follows:

    TLS/SSL:TLS/SSL:

    bootstrap.servers=NAMESPACENAME.servicebus.chinacloudapi.cn:9093
    security.protocol=SASL_SSL
    sasl.mechanism=PLAIN
    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="{YOUR.EVENTHUBS.CONNECTION.STRING}";
    

    OAuth:OAuth:

    bootstrap.servers=NAMESPACENAME.servicebus.chinacloudapi.cn:9093
    security.protocol=SASL_SSL
    sasl.mechanism=OAUTHBEARER
    sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required;
    sasl.login.callback.handler.class=CustomAuthenticateCallbackHandler;
    

    可以在此处的 GitHub 上找到示例处理程序类 CustomAuthenticateCallbackHandler 的源代码。You can find the source code for the sample handler class CustomAuthenticateCallbackHandler on GitHub here.

  4. 运行生产者代码并将事件流式传输到事件中心:Run the producer code and stream events into Event Hubs:

    mvn clean package
    mvn exec:java -Dexec.mainClass="TestProducer"                                    
    
  5. 导航到 azure-event-hubs-for-kafka/quickstart/java/consumerNavigate to azure-event-hubs-for-kafka/quickstart/java/consumer.

  6. src/main/resources/consumer.config 中更新使用者的配置详细信息,如下所示:Update the configuration details for the consumer in src/main/resources/consumer.config as follows:

    TLS/SSL:TLS/SSL:

    bootstrap.servers=NAMESPACENAME.servicebus.chinacloudapi.cn:9093
    security.protocol=SASL_SSL
    sasl.mechanism=PLAIN
    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="{YOUR.EVENTHUBS.CONNECTION.STRING}";
    

    OAuth:OAuth:

    bootstrap.servers=NAMESPACENAME.servicebus.chinacloudapi.cn:9093
    security.protocol=SASL_SSL
    sasl.mechanism=OAUTHBEARER
    sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required;
    sasl.login.callback.handler.class=CustomAuthenticateCallbackHandler;
    

    可以在此处的 GitHub 上找到示例处理程序类 CustomAuthenticateCallbackHandler 的源代码。You can find the source code for the sample handler class CustomAuthenticateCallbackHandler on GitHub here.

    可在此处找到 Kafka 的事件中心的所有 OAuth 示例。You can find all the OAuth samples for Event Hubs for Kafka here.

  7. 使用 Kafka 客户端运行使用者代码并处理来自事件中心的事件:Run the consumer code and process events from event hub using your Kafka clients:

    mvn clean package
    mvn exec:java -Dexec.mainClass="TestConsumer"                                    
    

如果事件中心 Kafka 群集有事件,则现在开始从使用者接收这些事件。If your Event Hubs Kafka cluster has events, you now start receiving them from the consumer.

后续步骤Next steps

本文介绍了如何在不更改协议客户端或运行自己的群集的情况下,将事件流式传输到事件中心。In this article, you learned how to stream into Event Hubs without changing your protocol clients or running your own clusters. 若要了解详细信息,请参阅针对 Azure 事件中心的 Apache Kafka 开发人员指南To learn more, see Apache Kafka developer guide for Azure Event Hubs.