如何将 Spark 数据帧输出转换为 json?
How to convert Spark dataframe output to json?
我正在使用 Spark SQL 上下文读取 CSV 文件。
代码:
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的return类型是什么。如何将输出转换为JSON格式,如何将数据帧转换为JSON??
想要的 O/P:
{"column":"id","datatype":"integer"}
DataType 有一个 json() 方法和一个 fromJson() 方法,您可以将其用于 serialize/deserialize 模式。
val df = sqlContext.read().....load()
val jsonString:String = df.schema.json()
val schema:StructType = DataType.fromJson(jsonString).asInstanceOf[StructType]
Spark SQL 方式,
df.createOrReplaceTempView("<table_name>")
spark.sql("SELECT COLLECT_SET(STRUCT(<field_name>)) AS `` FROM <table_name> LIMIT 1").coalesce(1).write.format("org.apache.spark.sql.json").mode("overwrite").save(<Blob Path1/ ADLS Path1>)
输出会像,[=13=]
{"":[{<field_name>:<field_value1>},{<field_name>:<field_value2>}]}
此处可以通过以下 3 行(假设数据中没有 Tilda)来避免 header,
val jsonToCsvDF=spark.read.format("com.databricks.spark.csv").option("delimiter", "~").load(<Blob Path1/ ADLS Path1>)
jsonToCsvDF.createOrReplaceTempView("json_to_csv")
spark.sql("SELECT SUBSTR(`_c0`,5,length(`_c0`)-5) FROM json_to_csv").coalesce(1).write.option("header",false).mode("overwrite").text(<Blob Path2/ ADLS Path2>)
我正在使用 Spark SQL 上下文读取 CSV 文件。
代码:
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的return类型是什么。如何将输出转换为JSON格式,如何将数据帧转换为JSON??
想要的 O/P:
{"column":"id","datatype":"integer"}
DataType 有一个 json() 方法和一个 fromJson() 方法,您可以将其用于 serialize/deserialize 模式。
val df = sqlContext.read().....load()
val jsonString:String = df.schema.json()
val schema:StructType = DataType.fromJson(jsonString).asInstanceOf[StructType]
Spark SQL 方式,
df.createOrReplaceTempView("<table_name>")
spark.sql("SELECT COLLECT_SET(STRUCT(<field_name>)) AS `` FROM <table_name> LIMIT 1").coalesce(1).write.format("org.apache.spark.sql.json").mode("overwrite").save(<Blob Path1/ ADLS Path1>)
输出会像,[=13=]
{"":[{<field_name>:<field_value1>},{<field_name>:<field_value2>}]}
此处可以通过以下 3 行(假设数据中没有 Tilda)来避免 header,
val jsonToCsvDF=spark.read.format("com.databricks.spark.csv").option("delimiter", "~").load(<Blob Path1/ ADLS Path1>)
jsonToCsvDF.createOrReplaceTempView("json_to_csv")
spark.sql("SELECT SUBSTR(`_c0`,5,length(`_c0`)-5) FROM json_to_csv").coalesce(1).write.option("header",false).mode("overwrite").text(<Blob Path2/ ADLS Path2>)