Skip to main content
 首页 » 编程设计

scala中如何在 Spark Streaming 中验证 azure iot hub 的连接字符串

2025年05月04日317lidabo

我是 azure iot hub 的新手。我正在尝试使用 Spark Streaming 从 azure iot hub 提取消息。当我执行代码时,我收到错误,我可以理解连接字符串中存在一些问题。有没有特定的方法来验证 Spark 中的连接字符串,还请告诉我我指定的格式是否正确。

我的示例代码:

import org.apache.spark.eventhubs._ 
 
val eventHubName = "xyztest.azure-devices.net" 
val eventHubNSConnStr = "Endpoint=sb://testname.servicebus.windows.net/;SharedAccessKeyName=primary;SharedAccessKey=abcedfgrdxyeurjrsdfyasdf=" 
val connStr = ConnectionStringBuilder(eventHubNSConnStr).setEventHubName(eventHubName).build  
val customEventhubParameters = EventHubsConf(connStr).setMaxEventsPerTrigger(5) 
val incomingStream = spark.readStream.format("eventhubs").options(customEventhubParameters.toMap).load() 
incomingStream.writeStream.outputMode("append").format("console").option("truncate", false).start().awaitTermination() 

错误:

java.util.concurrent.ExecutionException: com.microsoft.azure.eventhubs.IllegalEntityException: The messaging entity 'sb://testname.servicebus.windows.net/xyztest' could not be found. 
        at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) 
        at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) 
        at org.apache.spark.eventhubs.client.EventHubsClient.partitionCount(EventHubsClient.scala:169) 

请您参考如下方法:

val eventHubName = "xyztest.azure-devices.net"

您似乎设置了错误的事件中心名称。 “xyztest.azure-devices.net”应该是您的 Azure IoT 中心主机名。

要查找事件中心名称,您可以转到物联网中心 -> 端点 -> 事件并复制事件中心兼容名称的值,如下所示:

enter image description here

最后,event hub connect string将具有以下格式:

Endpoint=sb://SAMPLE;SharedAccessKeyName=KEY_NAME;SharedAccessKey=KEY;EntityPath=EVENTHUB_NAME