2

I want to create a DF of text files where each row represents a whole txt file in a column named text.

I've tried the following but I got a DF where the text is separated by lines.

Dataset<Row> df = spark.read()
            .textFile("resources/textfile.txt")
            .toDF("text");

Instead of DF of 1 row in the case of 1 file, I've got a DF of 70 rows for this file.

1 Answer 1

1

You can collect the dataframe into an array and then join the array to a single string:

import static org.apache.spark.sql.functions.*;

df.agg(collect_list("text").alias("text"))
    .withColumn("text", concat_ws(" ", col("text")))
    .show();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.