首页 > 其他分享 >2024.6.7

2024.6.7

时间:2024-06-07 23:55:56浏览次数:27  
标签:return 2024.6 orderCount clickCount Long public id

18万条数据

public class HotCategoryTop10_3 {
    public static void main(String[] args) {

        // TODO 搭建Spark的运行环境
        SparkConf conf = new SparkConf();
        conf.setAppName("HotCategoryTop10");
        conf.setMaster("local[*]"); // Yarn : Spark On Yarn
        JavaSparkContext jsc = new JavaSparkContext(conf);

        // TODO 将文件作为数据源,对接RDD
        //      如果当前环境是Yarn,那么相对路径指向的就是 HDFS
        jsc
            .textFile("data/user_visit_action.txt")
            .filter(
                line -> {
                    final String[] ss = line.split("_");
                    return "null".equals(ss[5]);
                }
            )
            .flatMap(
                line -> {
                    final String[] ss = line.split("_");
                    if (!"-1".equals(ss[6])) {
                        // TODO 点击数据
                        return Arrays.asList(new HotCategory(ss[6], 1L, 0L, 0L)).iterator();
                    } else if (!"null".equals(ss[8])) {
                        // TODO 下单数据
                        final String[] ids = ss[8].split(",");
                        List<HotCategory> objs = new ArrayList<>();
                        for (String id : ids) {
                            objs.add( new HotCategory(id, 0L, 1L, 0L) );
                        }
                        return objs.iterator();
                    } else {
                        // TODO 支付数据
                        final String[] ids = ss[10].split(",");
                        List<HotCategory> objs = new ArrayList<>();
                        for (String id : ids) {
                            objs.add( new HotCategory(id, 0L, 0L, 1L) );
                        }
                        return objs.iterator();
                    }
                }
        )
        .mapToPair(obj -> new Tuple2<>(obj.getId(), obj))
        .reduceByKey(
                (obj1, obj2) -> {
                    obj1.setClickCount(obj1.getClickCount() + obj2.getClickCount());
                    obj1.setOrderCount(obj1.getOrderCount() + obj2.getOrderCount());
                    obj1.setPayCount(obj1.getPayCount() + obj2.getPayCount());

                    return obj1;
                }
        )
        .map(kv -> kv._2)
        .sortBy(obj -> obj, true, 2)
        .take(10)
        .forEach(System.out::println);


        jsc.close();

    }
}

// TODO 自定义数据对象
//     1. 实现可序列化接口
//     2. 遵循Bean规范
//     3. 提供无参和全参的构造方法
//     4. 重写toString方法
//     5. 实现可比较的接口,重写比较方法
class HotCategory implements Serializable, Comparable<HotCategory> {
    private String id;
    private Long clickCount;
    private Long orderCount;
    private Long payCount;

    public HotCategory(String id, Long clickCount, Long orderCount, Long payCount) {
        this.id = id;
        this.clickCount = clickCount;
        this.orderCount = orderCount;
        this.payCount = payCount;
    }

    public HotCategory() {
    }

    @Override
    public int compareTo(HotCategory other) {
        if ( this.clickCount > other.clickCount ) {
            return -1;
        } else if ( this.clickCount < other.clickCount ) {
            return 1;
        } else {
            if ( this.orderCount > other.orderCount ) {
                return -1;
            } else if ( this.orderCount < other.orderCount ) {
                return 1;
            } else {
                return (int)(other.payCount - this.payCount);
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getClickCount() {
        return clickCount;
    }

    public void setClickCount(Long clickCount) {
        this.clickCount = clickCount;
    }

    public Long getOrderCount() {
        return orderCount;
    }

    public void setOrderCount(Long orderCount) {
        this.orderCount = orderCount;
    }

    public Long getPayCount() {
        return payCount;
    }

    public void setPayCount(Long payCount) {
        this.payCount = payCount;
    }

    @Override
    public String toString() {
        return "HotCategory{" +
                "id='" + id + '\'' +
                ", clickCount=" + clickCount +
                ", orderCount=" + orderCount +
                ", payCount=" + payCount +
                '}';
    }
}

标签:return,2024.6,orderCount,clickCount,Long,public,id
From: https://www.cnblogs.com/258-333/p/18238058

相关文章

  • 2024.6.7.exercise
    //#define_CRT_SECURE_NO_WARNINGS//#include<stdio.h>//#include<stdlib.h>//#include<stdbool.h>////typedefintdatatype;//typedefstructtree//{// datatypedata;// structtree*left;// structtree*right;//}tree;////datatype*......
  • 2024.6.7 日记
    今天心情不好,晚上效率很低。先总结一下模拟赛。今天确实打的不好,首先T1这个构造没啥说的,没发现就是没发现,但是freoepn是真逆天,交题前切记,编译一遍先。T2的问题在于维护方式想假导致被耗了好多好多时间,过程中还出现了倍增LCA写错,真的不牛,不过似乎这些时间省下来也没用。......
  • 2024.6.7
    2024.6.7【高考咯!!!学长们加油啊!!!】Friday五月初二A.双色球【题目描述】机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233“来来来,学弟,我考你道水题检验一下你的水平……”一个栈内初始有n个红色和蓝色的小球,请你按照以下规则进......
  • 2024.6.6
    更换了hadoop中的jdk的版本从1.8->17rdd行动算子和转换算子序列化//TODO//Spark在编写代码时,调用转换算子,并不会真正执行,因为只是在Driver端组合功能//所以当前的代码其实就是在Driver端执行//所以当前main方法也称之为driver方法......
  • 2024.6.6 日记
    晚上写不动题,所以打算每天睡前写点神秘文字。明天还有模拟赛,相似。周二T1挂了,凭借神秘的狗运打表瞪出了T2的结论,明天,或者以后,还会有这样的好运吗。呃我要干啥,要不然写点总结。这两天讲了dp,于是我补了一点题,找了一点题。感觉dp的方法其实大概就是,对着一个已知的过程dp,......
  • 2024.6 做题记录
    2024.6做题记录[JSOI2009]球队收益/球队预算考虑到要求最小总支出,想到最小费用流。首先容易发现,每场比赛都只有两种可能,即甲输乙赢或甲赢乙输。但是这样我们在跑费用流的时候显然需要考虑对于两个因素同时的影响,显然这样不好做。我们不妨假设剩下的比赛所有人都输,那么我们......
  • 2024.6.6
    2024.6.6【一天高考!!!“夏天周而复始、该相逢的人会再相逢”】Thursday五月初一<theme=oi-"DP">来学习一下DP的优化其实考试时我应该很难用到优化的P2569[SCOI2010]股票交易DP柿子比较好推,T,Maxp都比较小,作为f数组的两维还是挺合理的。那么设f[i][j]为第i天,有j张......
  • 2024.6.3 时光机会是最没用的发明
    正如标题,时光机会是最无用的发明。如果问昨天的我,时光机有用吗,我会毫不犹豫地回答有用。我希望回到5月,乞求自己好好改初赛;我希望回到1月,乞求自己不要虚度光阴;我希望回到去年9月,乞求自己不要头铁T2,乞求自己检查T1;我希望回到去年6月,乞求自己不要玩florr,这个万恶之源;我希望回到......
  • 2024.6.2
    2024.6.2【明霄升海平,飞彩镌流年。】Sunday四月廿六A.矩形覆盖题目描述有N个矩形,矩形的底边边长为1,且均在X轴上,高度给出,第i个矩形的高为h[i],求最少需要几个矩形才能覆盖这个图形。例如h=[3,2,4,2]的图形如下:image容易发现,只需要3个矩形就能覆盖这个图形。输入......
  • 2024.6 做题记录
    1.#2498.XavierisLearningtoCount有\(n\)个互不相同的整数\(a_{1,\cdots,n}\),从其中任取恰好\(k\)个数,记他们和为\(s\),求对于每个\(s\)的方案数。\(n,a_i\le1.3\times10^4,k\le5\)。根据互不相等容斥的结论,只需枚举集合划分的方案\(\{S_i\}\),钦定同一......