import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import java.util.Arrays;
import java.util.List;
/**
*sampleoperator(withReplacement,fraction,seed) 算子
*对RDD中的数据进行随机采样
* 第一个参数:boolean类型,表示产生的样本是否可以重复
* 第二个参数:代表取样的比例
* 第三个参数:代表一个随机数种子,就是抽样算法的初始值
*
*/
public class SampleOperator {
public static void main(String[] args){
SparkConf conf = new SparkConf().setMaster("local").setAppName("sample");
JavaSparkContext sc = new JavaSparkContext(conf);
List<String> list = Arrays.asList("w1","w2","w3","w4","w5","w6","w7","w8","w9","w10");
JavaRDD<String> listRdd = sc.parallelize(list);
JavaRDD<String> sampleRdd = listRdd.sample(false,0.5);
sampleRdd.foreach(new VoidFunction<String>() {
@Override
public void call(String s) throws Exception {
System.err.println(s);
}
});
}
}
微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!