【发布时间】:2020-05-04 22:43:53
【问题描述】:
我在 S3 中有很多行分隔的 json 文件,我想在 spark 中读取所有这些文件,然后读取 json 中的每一行,并为该行输出一个字典/行,并将文件名作为列。我将如何以有效的方式在 python 中执行此操作?每个 json 大约 200 MB。
这是一个文件的例子(会有 200,000 行这样的),调用这个文件 class_scores_0219:
{"name": "Maria C", "class":"Math", "score":"80", "student_identification":22}
{"name": "Maria F", "class":"Physics", "score":"90", "student_identification":12}
{"name": "Fink", "class":"English", "score":"75", "student_identification":7}
输出 DataFrame 将是(为简单起见仅显示一行):
+-------------------+---------+-------+-------+------------------------+
| file_name | name | class | score | student_identification |
+-------------------+---------+-------+-------+------------------------+
| class_scores_0219 | Maria C | Math | 80 | 22 |
+-------------------+---------+-------+-------+------------------------+
我已经使用这个设置了 s3 密钥/访问密钥:sc._jsc.hadoopConfiguration().set("fs.s3n.awsSecretAccessKey", SECRET_KEY)
(访问密钥相同),但可以以不同的方式连接。
我对任何最有效的选项持开放态度,我可以提供文件列表并将其输入,或者我可以连接到 boto3 并提供前缀。我是 Spark 的新手,因此感谢所有帮助。
【问题讨论】:
标签: python apache-spark pyspark apache-spark-sql databricks