python - PySpark - 使用 read.format('json') 读取 JSON 文件时,DataFrame 只包含第一行。为什么会这样?
问题描述
我正在读取具有这种格式的 JSON 文件:
{"username": "robert87", "currency": "BZD", "amount": 143472}
{"username": "taylorrobert", "currency": "TZS", "amount": 183074}
{"username": "ascott", "currency": "LRD", "amount": 154351}
{"username": "julie29", "currency": "JPY", "amount": 128404}
{"username": "rachelrogers", "currency": "CUP", "amount": 46338}
{"username": "tiffanyschmidt", "currency": "GBP", "amount": 88392}
尽管 JSON 文件包含 6 行,但当我运行以下命令时:
df = spark.read.format('json').load('file.json')
df.printSchema()
df.show()
我只取回第一行:
df:pyspark.sql.dataframe.DataFrame
amount:long
currency:string
username:string
Session created
root
|-- amount: long (nullable = true)
|-- currency: string (nullable = true)
|-- username: string (nullable = true)
+------+--------+--------+
|amount|currency|username|
+------+--------+--------+
|143472| BZD|robert87|
+------+--------+--------+
为什么我看不到相应列中的所有其他记录?是否与我使用的格式有关?
解决方案
我可以看到所有的行,试试下面
df = spark.read.json("/FileStore/tables/a_json.json")
df.show()
+------+--------+--------------+
|amount|currency| username|
+------+--------+--------------+
|143472| BZD| robert87|
|183074| TZS| taylorrobert|
|154351| LRD| ascott|
|128404| JPY| julie29|
| 46338| CUP| rachelrogers|
| 88392| GBP|tiffanyschmidt|
+------+--------+--------------+
推荐阅读
- vue.js - 为多个模板使用一个 Vue 组件
- apache-nifi - 如何使用 apache nifi 拆分 xml 文件?
- python - 以最 Pythonic 的方式仅获取迭代器的最后一个元素
- docker - 需要帮助使用 docker 设置开发/测试 Corda 网络
- c# - c# Websocket在关闭连接之前不发送消息
- angular - 如何获取已动态注入两次的组件的输出(侄子)?
- mongodb - 如何使用 $in 获取 mongo 中的所有数据?
- hadoop - 我们可以在同一个集群中安装多个 Hive 服务器吗
- angular - 如何使用 ng-container 和 ng-template
- python - 在 django ImageField 的错误目录中上传图像