テキストファイル

Spark SQLは、テキストファイルまたはテキストファイルのディレクトリをSpark DataFrameに読み込むためにspark.read().text("file_name")を提供し、テキストファイルに書き込むためにdataframe.write().text("path")を提供します。テキストファイルを読み込む場合、各行はデフォルトで文字列 "value" 列を持つ各行になります。行区切り文字は、以下の例に示すように変更できます。option()関数は、行区切り文字、圧縮などの動作を制御するなど、読み取りまたは書き込みの動作をカスタマイズするために使用できます。

# spark is from the previous example
sc = spark.sparkContext

# A text dataset is pointed to by path.
# The path can be either a single text file or a directory of text files
path = "examples/src/main/resources/people.txt"

df1 = spark.read.text(path)
df1.show()
# +-----------+
# |      value|
# +-----------+
# |Michael, 29|
# |   Andy, 30|
# | Justin, 19|
# +-----------+

# You can use 'lineSep' option to define the line separator.
# The line separator handles all `\r`, `\r\n` and `\n` by default.
df2 = spark.read.text(path, lineSep=",")
df2.show()
# +-----------+
# |      value|
# +-----------+
# |    Michael|
# |   29\nAndy|
# | 30\nJustin|
# |       19\n|
# +-----------+

# You can also use 'wholetext' option to read each input file as a single row.
df3 = spark.read.text(path, wholetext=True)
df3.show()
# +--------------------+
# |               value|
# +--------------------+
# |Michael, 29\nAndy...|
# +--------------------+

# "output" is a folder which contains multiple text files and a _SUCCESS file.
df1.write.csv("output")

# You can specify the compression format using the 'compression' option.
df1.write.text("output_compressed", compression="gzip")
Sparkリポジトリの"examples/src/main/python/sql/datasource.py"で完全なサンプルコードを見つけてください。
// A text dataset is pointed to by path.
// The path can be either a single text file or a directory of text files
val path = "examples/src/main/resources/people.txt"

val df1 = spark.read.text(path)
df1.show()
// +-----------+
// |      value|
// +-----------+
// |Michael, 29|
// |   Andy, 30|
// | Justin, 19|
// +-----------+

// You can use 'lineSep' option to define the line separator.
// The line separator handles all `\r`, `\r\n` and `\n` by default.
val df2 = spark.read.option("lineSep", ",").text(path)
df2.show()
// +-----------+
// |      value|
// +-----------+
// |    Michael|
// |   29\nAndy|
// | 30\nJustin|
// |       19\n|
// +-----------+

// You can also use 'wholetext' option to read each input file as a single row.
val df3 = spark.read.option("wholetext", true).text(path)
df3.show()
//  +--------------------+
//  |               value|
//  +--------------------+
//  |Michael, 29\nAndy...|
//  +--------------------+

// "output" is a folder which contains multiple text files and a _SUCCESS file.
df1.write.text("output")

// You can specify the compression format using the 'compression' option.
df1.write.option("compression", "gzip").text("output_compressed")
Sparkリポジトリの"examples/src/main/scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala"で完全なサンプルコードを見つけてください。
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;

// A text dataset is pointed to by path.
// The path can be either a single text file or a directory of text files
String path = "examples/src/main/resources/people.txt";

Dataset<Row> df1 = spark.read().text(path);
df1.show();
// +-----------+
// |      value|
// +-----------+
// |Michael, 29|
// |   Andy, 30|
// | Justin, 19|
// +-----------+

// You can use 'lineSep' option to define the line separator.
// The line separator handles all `\r`, `\r\n` and `\n` by default.
Dataset<Row> df2 = spark.read().option("lineSep", ",").text(path);
df2.show();
// +-----------+
// |      value|
// +-----------+
// |    Michael|
// |   29\nAndy|
// | 30\nJustin|
// |       19\n|
// +-----------+

// You can also use 'wholetext' option to read each input file as a single row.
Dataset<Row> df3 = spark.read().option("wholetext", "true").text(path);
df3.show();
//  +--------------------+
//  |               value|
//  +--------------------+
//  |Michael, 29\nAndy...|
//  +--------------------+

// "output" is a folder which contains multiple text files and a _SUCCESS file.
df1.write().text("output");

// You can specify the compression format using the 'compression' option.
df1.write().option("compression", "gzip").text("output_compressed");
Sparkリポジトリの"examples/src/main/java/org/apache/spark/examples/sql/JavaSQLDataSourceExample.java"で完全なサンプルコードを見つけてください。

データソースオプション

テキストのデータソースオプションは、以下を使用して設定できます

プロパティ名デフォルト意味スコープ
wholetext false trueの場合、入力パスからの各ファイルを単一行として読み込みます。 read
lineSep \r, \r\n, \n (読み取り時), \n (書き込み時) 読み取りまたは書き込みに使用する必要がある行区切り文字を定義します。 read/write
compression (なし) ファイルに保存するときに使用する圧縮コーデック。これは、既知の大文字と小文字を区別しない短縮名(none、bzip2、gzip、lz4、snappy、deflate)のいずれかです。 write

その他の一般的なオプションは、汎用ファイルソースオプションにあります。