首页 > 解决方案 > 使用 pyspark 将 Outlook 电子邮件转换为 json 文件格式

问题描述

我有大约 1 封 lac Outlook 电子邮件,并希望使用 pyspark 的分布式功能将它们转换为 json 格式。Outlook 电子邮件首先以 .msg 格式保存,然后执行将其转换为 json 格式的 python 算法。由于邮件的数量很大,有没有办法利用 pyspark 的实现,利用 spark 的分布式计算优势,将所有的 msg 文件转换为 json?

标签: jsonemailoutlookpysparkemail-attachments

解决方案


你确实可以用 pyspark 做到这一点,但我不能保证性能。

# first, acquire your data as a whole
rdd = sc.wholeTextFile("/path/to/files.msg")

# each line of your rdd will be a (key,value) pair where key is the path and value is the content of the file
# you need then to apply your python function to the "value"
json_rdd = rdd.map(lamba x : msg_to_json(x[1])

# json_rdd should now contain a json on each line, you just need to write it down
json_rdd.saveAsTextFile("/path/to/save/files.json")

在您这边,您只需要准备将msg_to_jsonmsg 文件的内容作为条目中的字符串并返回 json 文件的函数。


编辑
使用您的库,它将是:

import extract_msg
rdd = sc.binaryFiles("/path/to/files.msg")
rdd = rdd.map(extract_msg.Message)

推荐阅读