Azure Java SDK – 如何从事件中心(Event Hub)读取事件?

huangapple go评论63阅读模式
英文:

Azure java SDK - how to read events from an Event Hub?

问题

我有一个带有以下依赖关系的Java应用程序:

<dependency>
   <groupId>com.microsoft.azure</groupId>
   <artifactId>azure</artifactId>
   <version>1.33.0</version>
</dependency>

我需要从事件中心消费事件。我似乎无法找到如何做到这一点。

Azure azure = Azure.configure().withLogLevel(...).authenticate(credentials).withSubscription(subscriptionId);
EventHub eventHub = azure.eventHubs().getByName("?", "?", "?");

//现在怎么办?
英文:

I have a Java app with the following dependency:

<dependency>
   <groupId>com.microsoft.azure</groupId>
   <artifactId>azure</artifactId>
   <version>1.33.0</version>
</dependency>

I need to consume events from an Event Hub. I can't seem to find out how to this.

Azure azure = Azure.configure().withLogLevel(...).authenticate(credentials).withSubscription(subscriptionId);
EventHub eventHub = azure.eventHubs().getByName("?", "?", "?");

//Now what?

答案1

得分: 0

你是否查阅过文档?您需要创建客户端

EventHubConsumerAsyncClient consumer = new EventHubClientBuilder()
    .connectionString("<<特定事件中心实例的连接字符串>>")
    .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
    .buildAsyncConsumerClient();

// 从分区ID为"0"的分区接收新增事件。EventPosition指定开始消费事件的位置
consumer.receiveFromPartition("0", EventPosition.latest()).subscribe(event -> {
    // 处理每个到达的事件。
});

并使用方法getData();

EventData event = partitionEvent.getData();
英文:

Have you had a look at the docs? . You need to create the Client

EventHubConsumerAsyncClient consumer = new EventHubClientBuilder()
    .connectionString(&quot;&lt;&lt; CONNECTION STRING FOR SPECIFIC EVENT HUB INSTANCE &gt;&gt;&quot;)
    .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
    .buildAsyncConsumerClient();

// Receive newly added events from partition with id &quot;0&quot;. EventPosition specifies the position
// within the Event Hub partition to begin consuming events.
consumer.receiveFromPartition(&quot;0&quot;, EventPosition.latest()).subscribe(event -&gt; {
    // Process each event as it arrives.
});

and use the method getData();

EventData event = partitionEvent.getData();

huangapple
  • 本文由 发表于 2020年10月21日 23:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64466630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定