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.Function;
import org.apache.spark.api.java.function.VoidFunction;
import java.util.Arrays;
import java.util.List;
/**
* map算子
* 通过函数将RDD中的每一个元素转换为另一个新的RDD
*/
public class MapOperator {
public static void main(String[] args){
SparkConf conf = new SparkConf().setMaster("local").setAppName("mapOperator");
JavaSparkContext sc = new JavaSparkContext(conf);
List<Integer> numbers = Arrays.asList(1,2,3,4,5);
JavaRDD<Integer> numberRdd = sc.parallelize(numbers);
JavaRDD<String> result = numberRdd.map(new Function<Integer, String>() {
@Override
public String call(Integer inte) throws Exception{
return "number:"+inte;
}
});
result.foreach(new VoidFunction<String>() {
@Override
public void call(String s) throws Exception {
System.err.println("使用map算子:"+s);
}
});
}
}
微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!