Spark sampling is a mechanism to get random sample records from the dataset, this is helpful when you have a larger dataset and wanted to analyze/test a subset of the data for example 10% of the original file.
Spark provides sampling methods on RDD, DataFrame, and Dataset API to get sample data, In this article, I will explain how to get random sample records and how to get the same random sample every time you run and many more with scala examples.
Data sampling is most used by data analysts and data scientists to get statistical data on a subset of the dataset before applying it to large datasets.
Depends on Spark API you choose, you can use DataFrame.sample(), RDD.sample(), RDD.takeSample(), DataFrameStatFunctions.sampleBy() functions to get sample data.
1. Spark DataFrame Sampling
sample() has several overloaded functions, every signature takes fraction as a mandatory argument with a double value between 0 to 1 and returns a new Dataset with selected random sample records.
1.1 DataFrame sample() Syntax:
sample(fraction : scala.Double, seed : scala.Long) : Dataset[T]
sample(fraction : scala.Double) : Dataset[T]
sample(withReplacement : scala.Boolean, fraction : scala.Double, seed : scala.Long) : Dataset[T]
sample(withReplacement : scala.Boolean, fraction : scala.Double) : Dataset[T]
fraction – Fraction of rows to generate, range [0.0, 1.0]. Note that it doesn’t guarantee to provide the exact number of the fraction of records.
seed – Seed for sampling (default a random seed). Used to reproduce same random sampling
withReplacement – Sample with replacement or not (default False).
1.2 DataFrame sample() Examples
Note: If you run the same examples on your system, you may see different results for Example 1 and 3.
Example 1 Using
fraction to get a random sample in Spark – By using fraction between 0 to 1, it returns the approximate number of the fraction of the dataset. For example,
0.1 returns 10% of the rows. However, this does not guarantee it returns the exact 10% of the records.
val spark = SparkSession.builder() .master("local").appName("SparkByExample")
My DataFrame has 100 records and I wanted to get 10% sample records which are 10 but the sample() function returned 12 records. This proves the sample function doesn’t return the exact fraction specified.
Example 2: Using
seed to reproduce same Samples in Spark – Every time you run a sample() function it returns a different set of sampling records, however sometimes during the development and testing phase you may need to regenerate the same sample every time as you need to compare the results from your previous run. To get consistent same random sampling use the same slice value for every run. Change slice value to get different results.
On above examples, first 2 I have used slice 123 hence the sampling results are same and for last I have used 456 as slice hence it has returned different sampling records
Example 3: Sample
withReplacement (May contain duplicates) – some times you may need to get the random sample with repeated values. By using the value
true, results in repeated values.
println(df.sample(true,0.3,123).collect().mkString(",")) //with Duplicates
println(df.sample(0.3,123).collect().mkString(",")) // No duplicates
On first example, values 14, 52 and 65 are repeated values.
2. Spark Stratified Sampling
DataFrameStatFunctions class to get Stratified sampling in Spark
2.1 sampleBy() Syntax
sampleBy[T](col : _root_.scala.Predef.String, fractions : _root_.scala.Predef.Map[T, scala.Double], seed : scala.Long) : DataFrame
sampleBy[T](col : _root_.scala.Predef.String, fractions : java.util.Map[T, java.lang.Double], seed : scala.Long) : DataFrame
sampleBy[T](col : org.apache.spark.sql.Column, fractions : _root_.scala.Predef.Map[T, scala.Double], seed : scala.Long) : DataFrame
sampleBy[T](col : org.apache.spark.sql.Column, fractions : java.util.Map[T, java.lang.Double], seed : scala.Long) : DataFrame
2.2 sampleBy() Example
3. Spark RDD Sampling
Spark RDD also provides
sample() function to get a random sampling, it also has another signature
takeSample() that returns an Array[T].
takeSample() is an action hence you need to careful when you use this function as returning too much data results in an out-of-memory error similar to collect().
3.1 RDD sample() Syntax
Spark RDD sample() function returns the random sampling similar to DataFrame and takes similar type of parameters but in different order. Since I’ve already covered the explanation of these parameters on DataFrame, I will not be repeating the explanation on RDD, If not already read I recommend to read DataFrame section above.
sample() of RDD returns a new RDD by selecting random sampling. Below is a syntax.
sample(withReplacement : scala.Boolean, fraction : scala.Double, seed : scala.Long) : RDD[T]
3.2 RDD sample() Example
val rdd = spark.sparkContext.range(0,100)
3.3 RDD takeSample() Syntax
Below is a syntax of RDD
takeSample(withReplacement : scala.Boolean, num : scala.Int, seed : scala.Long) : scala.Array[T]
3.4 RDD takeSample() Example
In summary, Spark sampling can be done on RDD and DataFrame. In order to do sampling, you need to know how much data you wanted to retrieve by specifying fractions.
Use seed to regenerate the same sampling multiple times. and
withReplacement if you are okay to repeat the random records.
Thanks for reading. If you recognize my effort or like articles here please do comment or provide any suggestions for improvements in the comments sections!