import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by yz02 on 2017/6/16.
*/
object A_takeSample {
System.setProperty("hadoop.home.dir","F:\\hadoop-2.6.5")
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("takeSample_test").setMaster("local")
val sc = new SparkContext(conf)
//准备一下数据
val nameList : List[Int] = List(1,2,3,4,5)
val numbers = sc.parallelize(nameList)
//随机两个数据
val num = numbers.takeSample(true,2,1)
for (x <- num)
{
println(x)
}
//随机4个数据,注意随机的数据可能是重复的
val num1 = numbers.takeSample(true,4,1)
for (x <- num1)
{
println(x)
}
val num2 = numbers.takeSample(false,4,1)
for (x <- num2)
{
println(x)
}
}
}
运行结果:
1
3
3
4
4
5
2
4
5
2
5
3
1
2
3
1
2