今日总结

统计男女人数,并分别计算出男性和女性的最高和最低身高,数据格式“序号 M/F 身高”

 

    //生成性别身高数据,格式“序号  性别(M/F) 身高”
    private static void makeHeightData() throws IOException {
        File newFile = new File("src/main/files/heightData.txt");
        if (newFile.exists()){
            System.out.println("文件已存在!");
            return;
        }
        newFile.createNewFile();
        FileWriter fw = new FileWriter(newFile,true);
        Random rand = new Random();
        for (int i=1;i<=50000;i++){
            fw.append(i+"  M  "+(rand.nextInt(100)+100)+"\n");
            fw.append(i+"  F  "+(rand.nextInt(80)+100)+"\n");
            fw.flush();
        }
        fw.close();
    }

首先通过textFile()函数将文件数据读入RDD。然后使用filter算子分别过滤出男性和女性数据,接着用map算子分割出身高值并将其转化成Integer类型,这样才能用于数字排序,然后使用sortBy算子排序。sortBy算子可以直接对RDD中的数据排序,不用区分key还是value。

public class HeightMaxMin {
    public static void main(String[] args){
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("HeightAnalysis");
        JavaSparkContext sc = new JavaSparkContext(conf);

        JavaRDD<String> fileRDD = sc.textFile("src/main/files/heightData.txt");
        //使用filter算子分别过滤出男性和女性数据
        JavaRDD<String> maleRDD = fileRDD.filter(new Function<String, Boolean>() {
            @Override
            //如果这行数据符合过滤条件则返回true
            public Boolean call(String s) throws Exception {
                return s.contains("M");
            }
        });
        JavaRDD<String> femaleRDD = fileRDD.filter(new Function<String, Boolean>() {
            @Override
            //如果这行数据符合过滤条件则返回true
            public Boolean call(String s) throws Exception {
                return s.contains("F");
            }
        });

        //使用map算子分割出身高并转化为整数类型,这样才能用排序
        JavaRDD<Integer> maleHeightRDD = maleRDD.map(new Function<String, Integer>() {
            @Override
            public Integer call(String s) throws Exception {
                return Integer.parseInt(s.split("\\s+")[2]);
            }
        });
        JavaRDD<Integer> femaleHeightRDD = femaleRDD.map(new Function<String, Integer>() {
            @Override
            public Integer call(String s) throws Exception {
                return Integer.parseInt(s.split("\\s+")[2]);
            }
        });

        //使用sortBy算子排序
        JavaRDD<Integer> sortmaleHeightRDD = (JavaRDD<Integer>) maleHeightRDD.sortBy(new Function<Integer, Object>() {
            @Override
            //返回的是排序的内容,即对输入RDD中的哪一部分进行排序就输出哪一部分
            public Object call(Integer integer) throws Exception {
                return integer;
            }
        }, false,10);//false降序true升序,10是partition分区数,因为没有用集群所以也不太明白这个分区数具体指什么
        JavaRDD<Integer> sortfemaleHeightRDD = (JavaRDD<Integer>) femaleHeightRDD.sortBy(new Function<Integer, Object>() {
            @Override
            public Object call(Integer integer) throws Exception {
                return integer;
            }
        }, false,10);//第二个参数true/false是正序逆序,最后一个参数10是分区数

        //first()函数返回排名第一的数据
        System.out.println("男性: "+sortmaleHeightRDD.count()+"  "+sortmaleHeightRDD.first());
        System.out.println("女性: "+sortfemaleHeightRDD.count()+"  "+sortfemaleHeightRDD.first());

    }
}

 

 

posted @ 2024-01-23 21:49  北·岛  阅读(11)  评论(0)    收藏  举报