当前位置: 首页> 文旅> 艺术 > 网站服务器租用还是托管呢_东莞建设网招标信息_seo建站教学_手机seo快速排名

网站服务器租用还是托管呢_东莞建设网招标信息_seo建站教学_手机seo快速排名

时间:2025/7/11 10:40:33来源:https://blog.csdn.net/u011314083/article/details/144188979 浏览次数:0次
网站服务器租用还是托管呢_东莞建设网招标信息_seo建站教学_手机seo快速排名

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

<!--以下版本经过测试验证-->
<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(){// 初始化 SparkSessionSparkSession 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));// 停止 SparkSessionspark.stop();return ra;}
}
关键字:网站服务器租用还是托管呢_东莞建设网招标信息_seo建站教学_手机seo快速排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: