json - 如何在 spark sql 中解析嵌套的 JSON 对象?

我有一个如下所示的架构。我如何解析嵌套对象

root
 |-- apps: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- appName: string (nullable = true)
 |    |    |-- appPackage: string (nullable = true)
 |    |    |-- Ratings: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- date: string (nullable = true)
 |    |    |    |    |-- rating: long (nullable = true)
 |-- id: string (nullable = true)

最佳答案

假设您读入一个 json 文件并打印您向我们展示的架构,如下所示:

DataFrame df = sqlContext.read().json("/path/to/file").toDF();
    df.registerTempTable("df");
    df.printSchema();

然后您可以像这样选择结构类型中的嵌套对象...

DataFrame app = df.select("app");
        app.registerTempTable("app");
        app.printSchema();
        app.show();
DataFrame appName = app.select("element.appName");
        appName.registerTempTable("appName");
        appName.printSchema();
        appName.show();

https://stackoverflow.com/questions/29948789/

相关文章:

json - 如何在 Play Framework 2.x 中实现嵌入式对象的隐式 Json 写入

c# - 如何返回 JSON 对象

.net - 为什么 ServiceStack.Text 不默认日期为 iso8601?

python - 如何在scrapy中实现嵌套项?

android - 对等通信选项

java - Spring 3.0 使用 jackson 消息转换器做出 JSON 响应

json - 在 oracle 12c 中以 JSON 形式返回 sql 查询的结果

android - 如何从 JSONObject 中删除 nameValuePairs 键?

json - 确保数组中的项目属性在 Json Schema 中是唯一的?

java - 序列化对象时如何让 jackson 忽略 get() 方法