【发布时间】:2021-01-20 08:16:04
【问题描述】:
我正在尝试将 xml 文件(小于 100 kb)发送到 Azure 事件中心,然后在发送后读取 Databricks 中的事件。
现在我已经使用 Python SDK 以字节为单位发送 XML 的内容(这一步 WORKS)。但我想要实现的下一步是从事件的“主体”中读取 XML 内容,并使用 PYSPARK 创建一个 Spark Dataframe。
能够做到这一点,我有两个疑问:
1- 有没有我在
spark.readStream选项中指定事件“正文”的内容是 XML 的选项?
2- 是否有任何替代方法可以将该内容直接转储到 Spark Dataframe?
3- 将 XML 作为事件发送时缺少一些配置?
我正在尝试如下示例:
Python 事件制作者
# this is the python event hub message producer
import asyncio
from azure.eventhub.aio import EventHubProducerClient
from azure.eventhub import EventData
import xml.etree.ElementTree as ET
from lxml import etree
from pathlib import Path
connection_str= "Endpoint_str"
eventhub_name = "eventhub_name"
xml_path = Path("path/to/xmlfile.xml")
xml_data = ET.parse(xml_path)
tree = xml_data.getroot()
data = ET.tostring(tree)
async def run():
# Create a producer client to send messages to the event hub.
# Specify a connection string to your event hubs namespace and
# the event hub name.
producer = EventHubProducerClient.from_connection_string(conn_str=connection_str, eventhub_name=eventhub_name)
async with producer:
# Create a batch.
event_data_batch = await producer.create_batch()
# Add events to the batch.
event_data_batch.add(EventData(data))
# Send the batch of events to the event hub.
await producer.send_batch(event_data_batch)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
事件阅读器
stream_data = spark \
.readStream \
.format('eventhubs') \
.options(**event_hub_conf) \
.option('multiLine', True) \
.option('mode', 'PERMISSIVE') \
.load()
谢谢!!!
【问题讨论】:
标签: python xml azure apache-spark azure-eventhub