您的位置:首页 > 其它

spark--actions算子--takeSample

2017-07-13 09:41 218 查看
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

4

4

5

2

5

3

1

2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spark actions takeSample