[英]Why reading a json format file resulting all the records going to _corrupt_record in pyspark
我正在從 api 調用中讀取數據,數據的格式為 json,如下所示:
{'success': True, 'errors': \[\], 'requestId': '151a2#fg', 'warnings': \[\], 'result': \[{'id': 10322433, 'name': 'sdfdgd', 'desc': '', 'createdAt': '2016-09-20T13:48:58Z+0000', 'updatedAt': '2020-07-16T13:08:03Z+0000', 'url': 'https://eda', 'subject': {'type': 'Text', 'value': 'Register now'}, 'fromName': {'type': 'Text', 'value': 'ramjdn fg'}, 'fromEmail': {'type': 'Text', 'value': 'ffdfee@ozx.com'}, 'replyEmail': {'type': 'Text', 'value': 'ffdfee@ozx.com'}, 'folder': {'type': 'Folder', 'value': 478, 'folderName': 'sjha'}, 'operational': False, 'textOnly': False, 'publishToMSI': False, 'webView': False, 'status': 'approved', 'template': 1031, 'workspace': 'Default', 'isOpenTrackingDisabled': False, 'version': 2, 'autoCopyToText': True, 'preHeader': None}\]}
現在,當我使用以下代碼從這些數據中創建一個 dataframe 時:
df = spark.read.json(sc.parallelize(\[data\]))
我只得到一列 _corrupt_record,下面是我得到的 dataframe o/p。 我已經嘗試使用multine is true但我仍然沒有得到所需的 output。
+--------------------+
| \_corrupt_record|
\+--------------------+
|{'id': 12526, 'na...|
\+--------------------+
預期的 o/p 是用不同的列分解 json 后的 dataframe,比如 id 作為一列,name 作為另一列等等。
我已經嘗試了很多東西但無法解決這個問題。
我做了一些改變並且它奏效了。
我需要定義自定義模式
然后使用了這段代碼
data = sc.parallelize([items]) df = spark.createDataFrame(data,schema=schema)
它奏效了。
如果對此有任何優化的解決方案,請隨時分享。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.