如何将 Spark 数据帧输出转换为 json? [英] How to convert Spark dataframe output to json?
本文介绍了如何将 Spark 数据帧输出转换为 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读取带有 Spark SQL 上下文的 CSV 文件.
I am reading file with CSV file with Spark SQL Context.
代码:
m.put("path", CSV_DIRECTORY+file.getOriginalFilename());
m.put("inferSchema", "true"); // Automatically infer data types else string by default
m.put("header", "true"); // Use first line of all files as header
m.put("delimiter", ";");
DataFrame df = sqlContext.load("com.databricks.spark.csv",m);
df.printSchema();
使用 df.printSchema()
O/P:
|--id : integer (nullable = true)
|-- ApplicationNo: string (nullable = true)
|-- Applidate: timestamp(nullable = true)
语句printSchema的返回类型是什么.如何将输出转换为JSON格式,如何将数据帧转换为JSON??
What is the return type of the statement printSchema. How to convert the output in JSON format, How to convert data frame into JSON??
期望的 O/P:
{"column":"id","datatype":"integer"}
推荐答案
DataType 有一个 json() 方法和一个 fromJson() 方法,可用于序列化/反序列化模式.
DataType has a json() method and a fromJson() method which you can use to serialize/deserialize schemas.
val df = sqlContext.read().....load()
val jsonString:String = df.schema.json()
val schema:StructType = DataType.fromJson(jsonString).asInstanceOf[StructType]
这篇关于如何将 Spark 数据帧输出转换为 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文