记录使用Spark计算订单明细表销量排行榜的实现

一、引入相关依赖(版本很重要)

复制代码
<!--以下版本经过测试验证-->
<dependency>
	<groupId>org.apache.spark</groupId>
	<artifactId>spark-core_2.12</artifactId>
	<version>3.5.3</version>
</dependency>
<dependency>
	<groupId>org.apache.spark</groupId>
	<artifactId>spark-sql_2.12</artifactId>
	<version>3.5.3</version>
</dependency>
<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
	<version>4.1.58.Final</version>
</dependency>
<dependency>
	<groupId>org.codehaus.janino</groupId>
	<artifactId>janino</artifactId>
	<version>3.1.6</version>
</dependency>

二、实现Spark编码

  • ProductRanking.java

    public class ProductRanking {
    private Long skuId;
    private String goodsName;
    private Double totalQuantity;
    private Double totalSales;

    复制代码
      public ProductRanking(Long skuId, String goodsName, Double totalQuantity, Double totalSales) {
          this.skuId = skuId;
          this.goodsName = goodsName;
          this.totalQuantity = totalQuantity;
          this.totalSales = totalSales;
      }
    
      public Long getSkuId() {
          return skuId;
      }
    
      public void setSkuId(Long skuId) {
          this.skuId = skuId;
      }
    
      public String getGoodsName() {
          return goodsName;
      }
    
      public void setGoodsName(String goodsName) {
          this.goodsName = goodsName;
      }
    
      public Double getTotalQuantity() {
          return totalQuantity;
      }
    
      public void setTotalQuantity(Double totalQuantity) {
          this.totalQuantity = totalQuantity;
      }
    
      public Double getTotalSales() {
          return totalSales;
      }
    
      public void setTotalSales(Double totalSales) {
          this.totalSales = totalSales;
      }

    }

  • SparkJob.java

    public class SparkJob {
    public List<ProductRanking> getRanking(){
    // 初始化 SparkSession
    SparkSession spark = SparkSession.builder()
    .appName("Order Item Ranking")
    .master("local[*]") // 使用本地模式
    .getOrCreate();

    复制代码
          // MySQL JDBC 连接配置
          String jdbcUrl = "jdbc:mysql://localhost:3306/atest?useUnicode=true&characterEncoding=utf8&useSSL=false";
          Properties jdbcProps = new Properties();
          jdbcProps.put("user", "root");
          jdbcProps.put("password", "123456");
          jdbcProps.put("driver", "com.mysql.jdbc.Driver");
    
          // 设置查询条件:假设要查询 2023 年 1 月 1 日之后的订单
          String query = "(SELECT * FROM nasi_mts_trade_order_item ) AS order_item_filtered";
    
          // 从 MySQL 中读取符合条件的 order_item 表数据
          Dataset<Row> orderItemDF = spark.read()
                  .jdbc(jdbcUrl, query, jdbcProps);
    
          // 显示数据的前几行,检查数据是否加载正确

    // orderItemDF.show();

    复制代码
          // 统计每个商品的销量
          Dataset<Row> productRankingDF = orderItemDF.groupBy("sku_id", "goods_name")
                  .agg(
                          functions.sum("net_weight").alias("total_quantity"),
                          functions.sum(functions.col("net_weight").multiply(functions.col("goods_price"))).alias("total_sales")
                  )
                  .orderBy(functions.col("total_quantity").desc())  // 按销量降序排序
                  .limit(10);  // 获取销量前10名
    
          // 显示商品排名

    // productRankingDF.show();

    复制代码
          // 转换为 JSON 并映射为 Java 对象
          List<Row> rows = productRankingDF.collectAsList();
    
          // 将每一行映射为 com.cnnasi.erp.mts.spark.ProductRanking 对象
          List<ProductRanking> ra = rows.stream().map(row -> new ProductRanking(
                  row.getAs("sku_id"),
                  row.getAs("goods_name"),
                  row.getAs("total_quantity"),
                  row.getAs("total_sales")
          )).collect(Collectors.toList());
          log.info(JSONObject.toJSONString(ra));
          // 停止 SparkSession
          spark.stop();
          return ra;
      }

    }

相关推荐
武子康9 小时前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天11 小时前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
武子康2 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
肌肉娃子3 天前
20260227.spark.Spark 性能刺客:千万别在 for 循环里写 withColumn
spark
初次攀爬者3 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
武子康3 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库4 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟4 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法