[Python] Unable to Read from Azure Event Hub

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am successfully sending dummy data to Azure Event Hub with the following Python Script:

    import dbldatagen as dg
    from pyspark.sql.types import IntegerType, StringType, FloatType
    import json
    from pyspark.sql.types import StructType, StructField, IntegerType, DecimalType, StringType, TimestampType, Row, BooleanType
    from pyspark.sql.functions import *
    import pyspark.sql.functions as F
    num_rows = 1 * 10 # number of rows to generate
    num_partitions = 1 # number of Spark dataframe partitions


    delay_reasons = ["Air Carrier", "Extreme Weather", "National Aviation System", "Security", "Late Aircraft"]

    # will have implied column `id` for ordinal of row
    flightdata_defn = (dg.DataGenerator(spark, name="flight_delay_data", rows=num_rows, partitions=num_partitions)
    .withColumn("body",StringType())
    .withColumn("flightNumber", "int", minValue=1000, uniqueValues=10000, random=True)
    .withColumn("airline", "string", minValue=1, maxValue=500, prefix="airline", random=True, distribution="normal")
    .withColumn("original_departure", "timestamp", begin="2020-01-01 01:00:00", end="2020-12-31 23:59:00", interval="1 minute", random=True)
    .withColumn("delay_minutes", "int", minValue=20, maxValue=600, distribution=dg.distributions.Gamma(1.0, 2.0))
    .withColumn("delayed_departure", "timestamp", expr="cast(original_departure as bigint) + (delay_minutes * 60) ", baseColumn=["original_departure", "delay_minutes"])
    .withColumn("reason", "string", values=delay_reasons, random=True)
    )

    df_flight_data = flightdata_defn.build(withStreaming=True, options={'rowsPerSecond': 10})


    streamingDelays = (
    df_flight_data
    .groupBy(
    # df_flight_data.body,
    df_flight_data.flightNumber,
    df_flight_data.airline,
    df_flight_data.original_departure,
    df_flight_data.delay_minutes,
    df_flight_data.delayed_departure,
    df_flight_data.reason,
    window(df_flight_data.original_departure, "1 hour")
    )
    .count()
    )

    writeConnectionString = sc._jvm.org.apache.spark.eventhubs.EventHubsUtils.encrypt(connectionString)
    checkpointLocation = "///checkpoint"


    ehWriteConf = {
    'eventhubs.connectionString' : writeConnectionString
    }

    connectionString = connectionString

    ehConf = {}

    ehConf['eventhubs.connectionString'] = sc._jvm.org.apache.spark.eventhubs.EventHubsUtils.encrypt(connectionString)


    # Write body data from a DataFrame to EventHubs. Events are distributed across partitions using round-robin model.
    ds = streamingDelays \
    .select(F.to_json(F.struct("*")).alias("body")) \
    .writeStream.format("eventhubs") \
    .options(**ehWriteConf) \
    .outputMode("complete") \
    .option("checkpointLocation", "...") \
    .start()

    display(streamingDelays)
    # display(ds)


    The being sent to the EventHub looks like the following:

    [​IMG]

    I now attempt to read the data with the following code:

    connectionString = connectionString

    ehConf = {}

    ehConf['eventhubs.connectionString'] = sc._jvm.org.apache.spark.eventhubs.EventHubsUtils.encrypt(connectionString)

    streaming_df = spark \
    .readStream \
    .format("eventhubs") \
    .options(**ehConf) \
    .load()

    display(streaming_df)


    However, no data is retrieved when I attempt to read the EventHub

    Can someone let me know where I might be going wrong?

    Continue reading...

Compartilhe esta Página