package com.mengyao.examples.spark.core;

import java.io.Serializable;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction; import scala.Tuple2; /**
* 国内乘用车4月、1-4月销量数据统计
* @author mengyao
*
*/
@SuppressWarnings("all")
public class CarSaleStatistics { static class Sale implements Serializable {
private static final long serialVersionUID = -5393067134730174480L;
//排名
private int no;
//车型
private String model;
//车企
private String brand;
//4月销量
private int fourSale;
//1-4月累计销量
private int totalSale;
public Sale(int no, String model, String brand, int fourSale, int totalSale) {
this.no = no;
this.model = model;
this.brand = brand;
this.fourSale = fourSale;
this.totalSale = totalSale;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getFourSale() {
return fourSale;
}
public void setFourSale(int fourSale) {
this.fourSale = fourSale;
}
public int getTotalSale() {
return totalSale;
}
public void setTotalSale(int totalSale) {
this.totalSale = totalSale;
}
@Override
public String toString() {
return no + "\t" + model + "\t" + brand + "\t" + fourSale + "\t" + totalSale;
}
} /**
* 集群模式:spark-submit --class com.mengyao.examples.spark.core.CarSaleStatistics --master yarn --deploy-mode cluster --driver-memory 2048m --executor-memory 1024m --executor-cores 1 --queue default examples-0.0.1-SNAPSHOT.jar /data/carsales_data/2018.4-china-car-sales_volume.txt /data/carsales_data/statistics/
* 本地模式:Run As > Java Application
* @param args [in,out]
*/
public static void main(String[] args) {
SparkConf conf = new SparkConf()
.setAppName(CarSaleStatistics.class.getName());
if (null==args||args.length==0) {
args = new String[]{"./src/main/resources/data/2018.4-china-car-sales_volume.txt", "D:/"};
System.setProperty("hadoop.home.dir", "D:/softs/dev/apache/hadoop-2.7.5");
conf.setMaster("local");
}
JavaSparkContext sc = new JavaSparkContext(conf);
//中国市场合资、国产乘用车4月分销量数据
JavaRDD<String> linesRDD = sc.textFile(args[0]);
//按品牌分组
JavaPairRDD<String, Sale> brandSalesRDD = linesRDD.mapToPair(new PairFunction<String, String, Sale>() {
private static final long serialVersionUID = -3023653638555855696L;
@Override
public Tuple2<String, Sale> call(String line) throws Exception {
String[] fields = line.split("\t");
Sale sale = new Sale(Integer.parseInt(fields[0]), fields[1], fields[2], Integer.parseInt(fields[3]), Integer.parseInt(fields[4]));
return new Tuple2<String, Sale>(sale.getBrand(), sale);
}
});
//同品牌4月总销量、1-4月总销量
JavaPairRDD<String, Sale> brandTotalSalesRDD = brandSalesRDD.reduceByKey(new Function2<Sale, Sale, Sale>() {
private static final long serialVersionUID = 1L;
@Override
public Sale call(Sale item1, Sale item2) throws Exception {
item2.setFourSale(item1.getFourSale()+item2.getFourSale());
item2.setTotalSale(item1.getTotalSale()+item2.getTotalSale());
item2.setModel(item1.getModel()+","+item2.getModel());
return item2;
}
});
//4月份销量排名,转换key为4月销量
JavaPairRDD<Integer, Sale> fourSaleRankRDD = brandTotalSalesRDD.mapToPair(new PairFunction<Tuple2<String,Sale>, Integer, Sale>() {
private static final long serialVersionUID = 2012736852338064223L;
@Override
public Tuple2<Integer, Sale> call(Tuple2<String, Sale> t) throws Exception {
return new Tuple2<Integer, Sale>(t._2.getFourSale(), t._2);
}
});
//4月份销量排名降序
JavaPairRDD<Integer, Sale> fourSaleRankDescRDD = fourSaleRankRDD.sortByKey(false);
fourSaleRankDescRDD.foreach(new VoidFunction<Tuple2<Integer,Sale>>() {
private static final long serialVersionUID = -8110929872210046547L;
@Override
public void call(Tuple2<Integer, Sale> t) throws Exception {
Sale sale = t._2;
System.out.println("==== 4月份销量排名:"+sale.getBrand()+" = "+sale.getFourSale());
}
});
fourSaleRankDescRDD.saveAsNewAPIHadoopFile(args[1]+"fourSaleRank", NullWritable.class, Text.class, TextOutputFormat.class); //1-4月份累计销量排名,转换key为1-4月销量
JavaPairRDD<Integer, Sale> totalSaleRankRDD = brandTotalSalesRDD.mapToPair(new PairFunction<Tuple2<String,Sale>, Integer, Sale>() {
private static final long serialVersionUID = 2012736852338064223L;
@Override
public Tuple2<Integer, Sale> call(Tuple2<String, Sale> t) throws Exception {
return new Tuple2<Integer, Sale>(t._2.getTotalSale(), t._2);
}
});
//1-4月份累计销量排名降序
JavaPairRDD<Integer, Sale> totalSaleRankDescRDD = totalSaleRankRDD.sortByKey(false);
totalSaleRankDescRDD.foreach(new VoidFunction<Tuple2<Integer,Sale>>() {
private static final long serialVersionUID = -8110929872210046547L;
@Override
public void call(Tuple2<Integer, Sale> t) throws Exception {
Sale sale = t._2;
System.out.println("==== 1-4月份累计销量排名:"+sale.getBrand()+" = "+sale.getTotalSale());
}
});
fourSaleRankDescRDD.saveAsNewAPIHadoopFile(args[1]+"oneTofourSaleRank", NullWritable.class, Text.class, TextOutputFormat.class);
//关闭
sc.close();
} }

查看HDP Spark的HistoryServer(IP,18081),如下图表示成功:

Spark实现销量统计的更多相关文章

  1. Spark MLib 基本统计汇总 2

    4. 假设检验 基础回顾: 假设检验,用于判断一个结果是否在统计上是显著的.这个结果是否有机会发生. 显著性检验 原假设与备择假设 常把一个要检验的假设记作 H0,称为原假设(或零假设) (null ...

  2. Spark MLib 基本统计汇总 1

    1.  概括统计 summary statistics MLlib支持RDD[Vector]列式的概括统计,它通过调用 Statistics 的 colStats方法实现. colStats返回一个  ...

  3. Spark Streaming 002 统计单词的例子

    1.准备 事先在hdfs上创建两个目录: 保存上传数据的目录:hdfs://alamps:9000/library/SparkStreaming/data checkpoint的目录:hdfs://a ...

  4. [Spark Core] Spark 实现气温统计

    0. 说明 聚合气温数据,聚合出 MAX . MIN . AVG 1. Spark Shell 实现 1.1 MAX 分步实现 # 加载文档 val rdd1 = sc.textFile(" ...

  5. spark 累加历史 + 统计全部 + 行转列

    spark 累加历史主要用到了窗口函数,而进行全部统计,则需要用到rollup函数 1  应用场景: 1.我们需要统计用户的总使用时长(累加历史) 2.前台展现页面需要对多个维度进行查询,如:产品.地 ...

  6. spark 省份次数统计实例

    //统计access.log文件里面IP地址对应的省份,并把结果存入到mysql package access1 import java.sql.DriverManager import org.ap ...

  7. spark复习笔记(3):使用spark实现单词统计

    wordcount是spark入门级的demo,不难但是很有趣.接下来我用命令行.scala.Java和python这三种语言来实现单词统计. 一.使用命令行实现单词的统计 1.首先touch一个a. ...

  8. spark jdk8 单词统计示例

    在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...

  9. Spark入门案例 - 统计单词个数 / wordcount

    Scala版 import org.apache.spark.{SparkConf, SparkContext} object WordCountScala { def main(args: Arra ...

随机推荐

  1. java中sql语句能不能加分号的问题?

    一.原因  在程序运行中,当执行sql后总是报无效字符错误:但是把程序放在pl/sql中执行又没有错误.让我很纳闷!于是我开始查找资料,然后我终于发现了问题. 二.问题剖析 原来在程序中:如果你在程序 ...

  2. concurrenthashmap jdk1.8

    参考:https://www.jianshu.com/p/c0642afe03e0 CAS的思想很简单:三个参数,一个当前内存值V.旧的预期值A.即将更新的值B,当且仅当预期值A和内存值V相同时,将内 ...

  3. css边框以及其他常用样式

    1. 边框是1像素,实体的,红色的. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. (转)java +libsvm 安装与测试:

    libsvm 用SVM实现简单线性分类  (转自:http://www.cnblogs.com/freedomshe/archive/2012/10/09/2717356.html) 0. 下载lib ...

  5. CentOS 配置无线网络,开启wifi

    背景:一台老笔记本安装CentOS7.x,最小安装模式,安装后无法开启wifi 1.先用NetworkManager包的nmcli命令检查网卡,发现无线网卡wlo1信息里有个错误plugin miss ...

  6. Oracle 物化视图创建以及常见问题

    create materialized view MV_XXXXrefresh fast on commitwith rowidenable query rewriteasselect * from ...

  7. bzoj1588 [HNOI2002]营业额统计 (treap)

    平衡树裸题 只需要求前驱后驱 treap写法 const mm=<<; maxnumber=; maxn=; var left,right,fix,key:..maxn]of longin ...

  8. 消息传递 树形DP

    非常妙的树形DP:由于n很小,我们可以枚举每一个点作为第一个节点,计算其时间花费 那么问题就转化为对于给点节点求花费时间. 通过观察,显然我们会发现先传给花费时间多的人更加合算,因为这样可以最大限度的 ...

  9. BZOJ3165 & 洛谷4097:[HEOI2013]Segment——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3165 https://www.luogu.org/problemnew/show/P4097 要求 ...

  10. 【转】TCP拥塞控制,慢启动、拥塞避免、快重传以及快恢复

    转自:http://blog.csdn.net/yusiguyuan/article/details/22847787 注:本文绝大部分是来自转载的博客,还补充了少量内容. 一.TCP的拥塞控制 拥塞 ...