记录使用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;
      }

    }

相关推荐
-曾牛37 分钟前
Git完全指南:从入门到精通版本控制 ------- Git仓库创建 (5)
大数据·网络·git·学习·elasticsearch·个人开发
caihuayuan42 小时前
Redis奇幻之旅(三)1.redis客户端与服务端
java·大数据·sql·spring·课程设计
掘金-我是哪吒3 小时前
分布式微服务系统架构第105集:协议,高性能下单系统示例项目
分布式·微服务·架构·系统架构·linq
唐天下文化3 小时前
甜心速达智慧潮流精选超市、即时零售新业态,打造可持续发展商业模式
大数据·人工智能·零售
科技小E3 小时前
5G时代,视频分析设备平台EasyCVR实现通信基站远程安全便捷管控
大数据·网络·人工智能·音视频·安防监控
twj_one3 小时前
[ElasticSearch]Suggest查询建议(自动补全&纠错)
大数据·elasticsearch·搜索引擎
风铃儿~4 小时前
Java微服务注册中心深度解析:环境隔离、分级模型与Eureka/Nacos对比
java·分布式·微服务·面试
Debug_TheWorld4 小时前
Spark rdd算子解析与实践
spark
King.6244 小时前
从 SQL2API 到 Text2API:开启数据应用开发的新征程
大数据·开发语言·数据库·sql·低代码
alin、m5 小时前
银河麒麟(Kylin) - V10 SP1桌面操作系统ARM64编译QT-5.15.12版本
大数据·qt·kylin