2

I have created a dataframe using spark SQL in Java.

Dataset<Row> dateDF = spark.sql("select dates from dim_date where dates between '2017-01-01' and '2017-01-04'");

When I use the show() method it returns each date in new line. I understand that it is basically one column.

I want those values in a String variable with single quotes like this:

'2017-01-01','2017-01-02','2017-01-03,'2017-01-04'

How can I achieve this?

1 Answer 1

3

You can select your column, map to a String with as and collect to a list. I think converting a list of String to a single String should not be a problem afterwards:

dateDF.select("dates").as(Encoders.STRING()).collectAsList() //... and so on

On Java 8, you can merge values into one String using:

dateDF.select("dates").as(Encoders.STRING()).collectAsList().stream().collect(Collectors.joining(","));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @T. Gaweda. It works for me. I am new to spark and was stuck there.
@Vish No problem, I've just added the last paragraph and code snippet ;) Main idea is from moe.

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.