Spark SQL - 如何将 DataFrame 写入文本文件? [英] Spark SQL - How to write DataFrame to text file?

查看:115
本文介绍了Spark SQL - 如何将 DataFrame 写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spark SQL 读取 parquet 和写入 parquet 文件.

但在某些情况下,我需要将 DataFrame 编写为文本文件,而不是 Json 或 Parquet.

是否支持任何默认方法,或者我必须将该 DataFrame 转换为 RDD 然后使用 saveAsTextFile() 方法?

解决方案

使用 Databricks Spark-CSV 您可以直接保存到 CSV 文件,然后像这样从 CSV 文件加载

<块引用>

import org.apache.spark.sql.SQLContextSQLContext sqlContext = new SQLContext(sc);数据帧 df = sqlContext.read().format("com.databricks.spark.csv").option("inferSchema", "true").option("header", "true").load("cars.csv");df.select("year", "model").write().format("com.databricks.spark.csv").option("header", "true").option("codec", "org.apache.hadoop.io.compress.GzipCodec").save("newcars.csv");

I am using Spark SQL for reading parquet and writing parquet file.

But some cases,i need to write the DataFrame as text file instead of Json or Parquet.

Is there any default methods supported or i have to convert that DataFrame to RDD then use saveAsTextFile() method?

解决方案

Using Databricks Spark-CSV you can save directly to a CSV file and load from a CSV file afterwards like this

import org.apache.spark.sql.SQLContext

SQLContext sqlContext = new SQLContext(sc);
DataFrame df = sqlContext.read()
    .format("com.databricks.spark.csv")
    .option("inferSchema", "true")
    .option("header", "true")
    .load("cars.csv");

df.select("year", "model").write()
    .format("com.databricks.spark.csv")
    .option("header", "true")
    .option("codec", "org.apache.hadoop.io.compress.GzipCodec")
    .save("newcars.csv");

这篇关于Spark SQL - 如何将 DataFrame 写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆