Json 文件到 pyspark 数据帧

2023-12-28

我正在尝试在 Spark (pyspark) 环境中使用 JSON 文件。

问题:无法将 Pyspark Dataframe 中的 JSON 转换为预期格式

第一个输入数据集:

https://health.data.ny.gov/api/views/cnih-y5dw/rows.json https://health.data.ny.gov/api/views/cnih-y5dw/rows.json

在此文件中,元数据在文件的开头定义为带有标签“meta”,然后是带有标签“data”的数据。

仅供参考:将数据从网络下载到本地驱动器所采取的步骤。 1. 我已将文件下载到本地驱动器 2. 然后推送到 hdfs - 从那里我将其读取到 Spark 环境。

df=sqlContext.read.json("/user/train/ny.json",multiLine=True)
df.count()
out[5]: 1
df.show()

df.printSchema()

root
 |-- data: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: string (containsNull = true)
 |-- meta: struct (nullable = true)
 |    |-- view: struct (nullable = true)
 |    |    |-- attribution: string (nullable = true)
 |    |    |-- attributionLink: string (nullable = true)
 |    |    |-- averageRating: long (nullable = true)
 |    |    |-- category: string (nullable = true)
 |    |    |-- columns: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- cachedContents: struct (nullable = true)
 |    |    |    |    |    |-- average: string (nullable = true)
 |    |    |    |    |    |-- largest: string (nullable = true)
 |    |    |    |    |    |-- non_null: long (nullable = true)
 |    |    |    |    |    |-- null: long (nullable = true)
 |    |    |    |    |    |-- smallest: string (nullable = true)
 |    |    |    |    |    |-- sum: string (nullable = true)
 |    |    |    |    |    |-- top: array (nullable = true)
 |    |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |    |-- count: long (nullable = true)
 |    |    |    |    |    |    |    |-- item: string (nullable = true)
 |    |    |    |    |-- dataTypeName: string (nullable = true)
 |    |    |    |    |-- description: string (nullable = true)
 |    |    |    |    |-- fieldName: string (nullable = true)
 |    |    |    |    |-- flags: array (nullable = true)
 |    |    |    |    |    |-- element: string (containsNull = true)
 |    |    |    |    |-- format: struct (nullable = true)
 |    |    |    |    |    |-- align: string (nullable = true)
 |    |    |    |    |    |-- mask: string (nullable = true)
 |    |    |    |    |    |-- noCommas: string (nullable = true)
 |    |    |    |    |    |-- precisionStyle: string (nullable = true)
 |    |    |    |    |-- id: long (nullable = true)
 |    |    |    |    |-- name: string (nullable = true)
 |    |    |    |    |-- position: long (nullable = true)
 |    |    |    |    |-- renderTypeName: string (nullable = true)
 |    |    |    |    |-- tableColumnId: long (nullable = true)
 |    |    |    |    |-- width: long (nullable = true)
 |    |    |-- createdAt: long (nullable = true)
 |    |    |-- description: string (nullable = true)
 |    |    |-- displayType: string (nullable = true)
 |    |    |-- downloadCount: long (nullable = true)
 |    |    |-- flags: array (nullable = true)
 |    |    |    |-- element: string (containsNull = true)
 |    |    |-- grants: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- flags: array (nullable = true)
 |    |    |    |    |    |-- element: string (containsNull = true)
 |    |    |    |    |-- inherited: boolean (nullable = true)
 |    |    |    |    |-- type: string (nullable = true)
 |    |    |-- hideFromCatalog: boolean (nullable = true)
 |    |    |-- hideFromDataJson: boolean (nullable = true)
 |    |    |-- id: string (nullable = true)
 |    |    |-- indexUpdatedAt: long (nullable = true)
 |    |    |-- metadata: struct (nullable = true)
 |    |    |    |-- attachments: array (nullable = true)
 |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |-- assetId: string (nullable = true)
 |    |    |    |    |    |-- blobId: string (nullable = true)
 |    |    |    |    |    |-- filename: string (nullable = true)
 |    |    |    |    |    |-- name: string (nullable = true)
 |    |    |    |-- availableDisplayTypes: array (nullable = true)
 |    |    |    |    |-- element: string (containsNull = true)
 |    |    |    |-- custom_fields: struct (nullable = true)
 |    |    |    |    |-- Additional Resources: struct (nullable = true)
 |    |    |    |    |    |-- See Also: string (nullable = true)
 |    |    |    |    |-- Dataset Information: struct (nullable = true)
 |    |    |    |    |    |-- Agency: string (nullable = true)
 |    |    |    |    |-- Dataset Summary: struct (nullable = true)
 |    |    |    |    |    |-- Contact Information: string (nullable = true)
 |    |    |    |    |    |-- Coverage: string (nullable = true)
 |    |    |    |    |    |-- Data Frequency: string (nullable = true)
 |    |    |    |    |    |-- Dataset Owner: string (nullable = true)
 |    |    |    |    |    |-- Granularity: string (nullable = true)
 |    |    |    |    |    |-- Organization: string (nullable = true)
 |    |    |    |    |    |-- Posting Frequency: string (nullable = true)
 |    |    |    |    |    |-- Time Period: string (nullable = true)
 |    |    |    |    |    |-- Units: string (nullable = true)
 |    |    |    |    |-- Disclaimers: struct (nullable = true)
 |    |    |    |    |    |-- Limitations: string (nullable = true)
 |    |    |    |    |-- Local Data: struct (nullable = true)
 |    |    |    |    |    |-- County Filter: string (nullable = true)
 |    |    |    |    |    |-- County_Column: string (nullable = true)
 |    |    |    |-- filterCondition: struct (nullable = true)
 |    |    |    |    |-- children: array (nullable = true)
 |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |-- metadata: struct (nullable = true)
 |    |    |    |    |    |    |    |-- includeAuto: long (nullable = true)
 |    |    |    |    |    |    |    |-- multiSelect: boolean (nullable = true)
 |    |    |    |    |    |    |    |-- operator: string (nullable = true)
 |    |    |    |    |    |    |    |-- tableColumnId: struct (nullable = true)
 |    |    |    |    |    |    |    |    |-- 583607: long (nullable = true)
 |    |    |    |    |    |    |-- type: string (nullable = true)
 |    |    |    |    |    |    |-- value: string (nullable = true)
 |    |    |    |    |-- metadata: struct (nullable = true)
 |    |    |    |    |    |-- advanced: boolean (nullable = true)
 |    |    |    |    |    |-- unifiedVersion: long (nullable = true)
 |    |    |    |    |-- type: string (nullable = true)
 |    |    |    |    |-- value: string (nullable = true)
 |    |    |    |-- jsonQuery: struct (nullable = true)
 |    |    |    |    |-- order: array (nullable = true)
 |    |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |    |-- ascending: boolean (nullable = true)
 |    |    |    |    |    |    |-- columnFieldName: string (nullable = true)
 |    |    |    |-- rdfSubject: string (nullable = true)
 |    |    |    |-- renderTypeConfig: struct (nullable = true)
 |    |    |    |    |-- visible: struct (nullable = true)
 |    |    |    |    |    |-- table: boolean (nullable = true)
 |    |    |    |-- rowLabel: string (nullable = true)
 |    |    |-- name: string (nullable = true)
 |    |    |-- newBackend: boolean (nullable = true)
 |    |    |-- numberOfComments: long (nullable = true)
 |    |    |-- oid: long (nullable = true)
 |    |    |-- owner: struct (nullable = true)
 |    |    |    |-- displayName: string (nullable = true)
 |    |    |    |-- flags: array (nullable = true)
 |    |    |    |    |-- element: string (containsNull = true)
 |    |    |    |-- id: string (nullable = true)
 |    |    |    |-- profileImageUrlLarge: string (nullable = true)
 |    |    |    |-- profileImageUrlMedium: string (nullable = true)
 |    |    |    |-- profileImageUrlSmall: string (nullable = true)
 |    |    |    |-- screenName: string (nullable = true)
 |    |    |    |-- type: string (nullable = true)
 |    |    |-- provenance: string (nullable = true)
 |    |    |-- publicationAppendEnabled: boolean (nullable = true)
 |    |    |-- publicationDate: long (nullable = true)
 |    |    |-- publicationGroup: long (nullable = true)
 |    |    |-- publicationStage: string (nullable = true)
 |    |    |-- query: struct (nullable = true)
 |    |    |    |-- orderBys: array (nullable = true)
 |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |-- ascending: boolean (nullable = true)
 |    |    |    |    |    |-- expression: struct (nullable = true)
 |    |    |    |    |    |    |-- columnId: long (nullable = true)
 |    |    |    |    |    |    |-- type: string (nullable = true)
 |    |    |-- rights: array (nullable = true)
 |    |    |    |-- element: string (containsNull = true)
 |    |    |-- rowsUpdatedAt: long (nullable = true)
 |    |    |-- rowsUpdatedBy: string (nullable = true)
 |    |    |-- tableAuthor: struct (nullable = true)
 |    |    |    |-- displayName: string (nullable = true)
 |    |    |    |-- flags: array (nullable = true)
 |    |    |    |    |-- element: string (containsNull = true)
 |    |    |    |-- id: string (nullable = true)
 |    |    |    |-- profileImageUrlLarge: string (nullable = true)
 |    |    |    |-- profileImageUrlMedium: string (nullable = true)
 |    |    |    |-- profileImageUrlSmall: string (nullable = true)
 |    |    |    |-- screenName: string (nullable = true)
 |    |    |    |-- type: string (nullable = true)
 |    |    |-- tableId: long (nullable = true)
 |    |    |-- tags: array (nullable = true)
 |    |    |    |-- element: string (containsNull = true)
 |    |    |-- totalTimesRated: long (nullable = true)
 |    |    |-- viewCount: long (nullable = true)
 |    |    |-- viewLastModified: long (nullable = true)
 |    |    |-- viewType: string (nullable = true)

Problem:所有记录都包含在单行和两列中,即元和数据。另外,使用 Spark 本机 JSON 实用程序 - Spark 自动推断架构(元数据) - 我的期望是它不应该明确作为数据帧上的单独列。

预期输出JSON 数据集具有以下列列表。它应该在数据框中以表格格式显示它们,我可以在其中查询它们”

FACILITY, ADDRESS, LAST INSPECTED, VIOLATIONS,TOTAL  CRITICAL VIOLATIONS, TOTAL CRIT.  NOT CORRECTED, TOTAL  NONCRITICAL VIOLATIONS, DESCRIPTION, LOCAL HEALTH DEPARTMENT, COUNTY, FACILITY ADDRESS, CITY, ZIP CODE, NYSDOH GAZETTEER (1980), MUNICIPALITY, OPERATION NAME, PERMIT EXPIRATION DATE, PERMITTED  (D/B/A), PERMITTED  CORP. NAME,PERM. OPERATOR LAST NAME, PERM. OPERATOR LAST NAME, PERM. OPERATOR FIRST NAME, NYS HEALTH OPERATION ID, INSPECTION TYPE, INSPECTION COMMENTS, FOOD SERVICE FACILITY STATE, Location1

第二个输入数据集:现场,这是世界银行资助项目的第一个数据集

http://jsonstudio.com/resources/ http://jsonstudio.com/resources/

(现场,这是世界银行资助项目的第一个数据集)

一切正常。

df=sqlContext.read.json("/user/train/wb.json")
df.count()
500 

第二输入数据集可以正常工作,但第一输入数据集则不能。我的观察是两个 Json 文件定义元数据的方式不同。在第一。首先定义元数据,然后定义数据,但是在第二个文件中 - 肉数据可用于每行数据。

您能否指导我了解第一个输入 JSON 文件格式以及如何在将其转换为 pyspark 数据帧时处理情况?

更新结果:经过初步分析,我们发现格式似乎错误,但社区成员提供了另一种读取格式的方法。将答案标记为正确并关闭此线程。

如果您需要更多详细信息,请告诉我,提前致谢。


查看我的笔记本数据块 https://community.cloud.databricks.com

https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/753971180331031/2636709537891264/8469274372247319/latest.html https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/753971180331031/2636709537891264/8469274372247319/latest.html

第一个数据集已损坏,即无效json所以 Spark 无法读取它。

但这是针对 Spark 2.2.1 的

这尤其令人困惑,因为这种方式json文件已组织好 数据存储为列表的列表

df=spark.read.json("rows.json",multiLine=True)
data = df.select("data").collect()[0]['data']

并且列名是单独存储的

column_names = map(lambda x: x['fieldName'], df.select("meta").collect()[0][0][0][4])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Json 文件到 pyspark 数据帧 的相关文章

随机推荐

  • GetQueuedCompletionStatus 永远阻塞

    我正在编写一个服务器应用程序 我想使用 IOCompletion 端口 因此我为服务器编写了一个原型 但我遇到了 GetQueuedCompletionStatus 的问题 它永远不会返回 它会阻塞 下面是我的代码 bool CreateS
  • UserControl 依赖属性设计时

    我正在 WPF 中创建一个简单的用户控件 其中包含一个按钮内的 TextBlock
  • 如何在数据源位置中使用sitecore查询? (动态数据源)

    是否可以将数据源位置 而不是数据源 设置为 sitecore 查询 我想要做的是将子布局的数据源位置设置为包含它的项目 当前项目 下的文件夹 子布局数据源位置应指向当前项目下的文件夹 所以我尝试将数据源位置设置为query Items 但这
  • 在讨论编程语言时,“强大”意味着什么?

    在编程语言讨论 比较的背景下 术语 能力 是什么意思 它有明确的含义吗 即使是一个定义不明确的含义 假设有人说 X 语言比 Y 语言更强大 或提出相同的问题 他们的意思是什么 或者他们想找出什么信息 它没有明确定义的含义 在这些类型的讨论中
  • Android 锁屏 C# .NET 副本

    我希望在 Visual Studio C 中创建一个模仿 Android 模式锁定屏幕的应用程序 并且想知道是否有人有任何这样做的提示 这不是一个商业项目 不会赚钱 只是一点乐趣 我一直在玩 WPF 项目 现在不知所措 我想到的唯一方法是在
  • Spark 中的 Dataframe 连接可以保留顺序吗?

    我目前正在尝试将两个数据帧连接在一起 但在其中一个数据帧中保留相同的顺序 From 哪些操作保留 RDD 顺序 https stackoverflow com questions 29284095 which operations pres
  • React Native + Firebase 存储:base64 和 blob 不起作用

    我试图将我的图像文件编码为 base64 或 blob 但它们都不起作用 我也在用这个 https github com react community react native image picker https github com
  • 如何在 Pnotify 中将焦点设置在按钮上

    我在我的项目中使用 pnotify Alert jquery 我试图在弹出对话框时将焦点设置在 确定 按钮上 这样用户只需按 Enter 或空格键即可关闭对话框 但无法做到这一点 这是 pnotify 的链接 http sciactive
  • Ruby“.downcase!”和“downcase”混淆

    我刚刚开始 Ruby 编程 我读过小写和小写的区别 在红宝石中 https stackoverflow com questions 709229 difference between downcase and downcase in rub
  • 使用 dplyr mutate 将向量分成两列

    假设我有以下功能 get answer lt function condition dp rp if condition DD result lt rbinom n 2 size 1 prob dp if condition in c DR
  • 在 Android 中捕获完整的日志

    我想知道如何从 Android 设备获取完整日志 从我的应用程序初始化到任何崩溃或直到强制关闭我的应用程序 我在这里发帖的原因是我的应用程序在某个时候崩溃了 但是当我使用日志时DDMS Logcat我的崩溃详细信息已被新日志覆盖 我如何获取
  • 从堆栈内存中删除结构

    我有一个链表结构 我想将一个节点 另一个结构 指针传递给一个函数 该节点是链表的一部分 但我将节点单独传递给删除器函数 我希望它将下一个节点数据复制到自身中 覆盖其数据 并删除下一个节点 从而删除自身 这部分正在工作 我让它检查传递的节点是
  • @Html.BeginForm 在页面上显示“System.Web.Mvc.Html.MvcForm”

    我有一个剃刀视图 我在 if 语句内部添加了一个删除按钮 当视图在浏览器中呈现时 它会显示 系统 Web Mvc Html MvcForm 删除按钮旁边 我该如何摆脱它 这是代码 div if update User UserName Eq
  • processing.js 如何检测浏览器的大小?

    如前所述 processing js 如何响应浏览器的大小 响应式设计 我尝试过 screen width 和 screen height 但效果不佳 看来只能检测电脑屏幕尺寸的大小 更重要的是 我想在拖动并更改浏览器大小时与窗口大小保持同
  • C++数学表达式的错误结果[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我必须计算
  • 具有 MultiIndex 的 DataFrame 到 dict

    我有一个带有多重索引的数据框 我想知道我是否以正确的方式创建了数据框 见下文 01 01 02 01 03 01 04 01 bar total1 40 52 18 11 total2 36 85 5 92 baz total1 23 39
  • 如何在java中读取.pem文件格式的EC私钥

    如何使用JAVA读取 pem文件中的EC私钥 在阅读时我遇到以下异常 引起原因 java security InvalidKeyException IOException 版本不匹配 支持 00 解析 01 其实我的 Pem 文件包含以下结
  • 在 SQL 中使用 COUNT 函数

    首先也是最重要的 这是作业的一部分 我正在尝试使用 COUNT 函数作为与 Northwind 数据库相关的查询的一部分 查询应返回 CustomerID CompanyName 和为每个客户下的订单数量 当然 前两部分很简单 但我无法让
  • C++ 外部函数,以函数指针作为参数,在具有成员函数的类内部使用

    对 C 相当陌生 假设我有一堂课 class A private double m x m y public A double x double y m x x m y extF m x y intF double intF double
  • Json 文件到 pyspark 数据帧

    我正在尝试在 Spark pyspark 环境中使用 JSON 文件 问题 无法将 Pyspark Dataframe 中的 JSON 转换为预期格式 第一个输入数据集 https health data ny gov api views