蝈蝈大王

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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.FlatMapFunction;
import org.apache.spark.api.java.function.VoidFunction;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* flatmap 算子:
* 一对多 处理数据
*/
public class FlatMapOperator {

public static void main(String[] args){
SparkConf conf = new SparkConf().setMaster("local").setAppName("flatmap");
JavaSparkContext sc = new JavaSparkContext(conf);
List<String> list = Arrays.asList("w1 1","w2 2","w3 3","w4 4");

JavaRDD<String> listRdd = sc.parallelize(list);

JavaRDD<String> result = listRdd.flatMap(new FlatMapFunction<String, String>() {
@Override
public Iterator<String> call(String s) throws Exception {
return Arrays.asList(s.split(" ")).iterator();
}
});

result.foreach(new VoidFunction<String>() {
@Override
public void call(String s) throws Exception {
System.err.println(s);
}
});
}
}

微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!

posted on 2019-09-27 16:36  蝈蝈大王  阅读(3020)  评论(0编辑  收藏  举报