0

I am using Spark 2.2.0 and Scala 2.11.8 in the Spark-Shell environment. I have a data frame df, and I need to filter out the previous day's data based on the value of column 'date' and then append the data to a HDFS location. (e.g. today is 2018-06-28, I need the data of 2018-06-27)

Below is the code:

 df.filter($"date" === "2018-06-27") .write.mode(SaveMode.Append).parquet("hdfs:/path..../date=2018-06-27")

I need the code above for automation, so I need to replace "2018-06-27" for the filter value as well as the directory name. So if I have a string -> date_test: String = 2018-06-27; The code below should be still working

 df.filter($"date" === "date_test") .write.mode(SaveMode.Append).parquet("hdfs:/path..../date=date_test")

How to do this?

3
  • 1
    What is the data type of your date column date or string Commented Jun 28, 2018 at 18:06
  • The type of the date column is string Commented Jun 28, 2018 at 18:41
  • why the downvote? Commented Dec 8, 2018 at 22:06

1 Answer 1

2

You can apply filter conditions like below

//Input
+----------+
|      date|
+----------+
|2018-02-01|
|2017-01-02|
+----------+

//Solution: 
 val previousDate="'2018-02-01'"
 df.filter(s"date=$previousDate").show

//Output: 
+----------+
|      date|
+----------+
|2018-02-01|
+----------+

You can do like this for your solution

 val datetest:String="2018-02-01"
 df.filter(s"date='$datetest'").write.mode(SaveMode.Append).parquet(s"hdfs:/path..../$datetest")
Sign up to request clarification or add additional context in comments.

5 Comments

So for automation purpose, It cannot contain the actually date '2018-02-01'. For example, if I have a string datetest: String = 2018-02-01. The code should be able to work like df.filter("date='datetest'").show
I modified answer you need to create your filter condition like datetest: String = "'2018-02-01'"
This works for the filter, but does it work for the directory name as well?
No, It will not work .Please see last part of answer It will work fine
Cool, I'll try it.

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.