How to parse nested JSON objects in spark sql? -
i have schema shown below. how can parse nested objects
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)
assuming read in json file , print schema showing this:
dataframe df = sqlcontext.read().json("/path/to/file").todf(); df.registertemptable("df"); df.printschema();
then can select nested objects inside struct type so...
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();
Comments
Post a Comment