當前位置:
首頁 >
Spark入门(十四)之分组求最大值
發布時間:2023/12/3
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Spark入门(十四)之分组求最大值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?一、分組求最大值
計算文本里面的每個key分組求最大值,輸出結果。
?
二、maven設置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mk</groupId><artifactId>spark-test</artifactId><version>1.0</version><name>spark-test</name><url>http://spark.mk.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><scala.version>2.11.1</scala.version><spark.version>2.4.4</spark.version><hadoop.version>2.6.0</hadoop.version></properties><dependencies><!-- scala依賴--><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>${scala.version}</version></dependency><!-- spark依賴--><dependency><groupId>org.apache.spark</groupId><artifactId>spark-core_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-sql_2.11</artifactId><version>${spark.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin></plugins></pluginManagement></build> </project>?
三、編程代碼?
public class GroupByMaxApp implements SparkConfInfo {public static void main(String[] args) {String filePath = "E:\\spark\\groubByNumber.txt";SparkSession sparkSession = new GroupByMaxApp().getSparkConf("groubByNumber");JavaPairRDD<String, Integer> numbers = sparkSession.sparkContext().textFile(filePath, 4).toJavaRDD().flatMap(v -> Arrays.asList(v.split("\n")).iterator()).mapToPair(v -> {String[] data = v.split("\\s+");if (data.length != 2) {return null;}if (!data[1].matches("-?[0-9]+(.[0-9]+)?"))return null;return new Tuple2<>(data[0], Integer.valueOf(data[1]));}).filter(v -> v != null).cache();//數據量大會溢出內存無法計算 // numbers.groupByKey() // .sortByKey(true) // .mapValues(v -> { // // Integer max = null; // Iterator<Integer> it = v.iterator(); // while (it.hasNext()) { // Integer val = it.next(); // if(max==null || max<val){ // max = val; // } // } // return max; // }) // .collect() // .forEach(v -> System.out.println(v._1 + ":" + v._2));//這種聚合數據再計算numbers.combineByKey(max -> max, // 將val映射為一個元組,作為分區內聚合初始值(max,val) -> {if (max < val) {max = val;}return max;}, //分區內聚合,(a, b) -> Math.max(a, b)) //分區間聚合.sortByKey(true).collect().forEach(v -> System.out.println(v._1 + ":" + v._2));sparkSession.stop();} }public interface SparkConfInfo {default SparkSession getSparkConf(String appName){SparkConf sparkConf = new SparkConf();if(System.getProperty("os.name").toLowerCase().contains("win")) {sparkConf.setMaster("local[4]");System.out.println("使用本地模擬是spark");}else{sparkConf.setMaster("spark://hadoop01:7077,hadoop02:7077,hadoop03:7077");sparkConf.set("spark.driver.host","192.168.150.1");//本地ip,必須與spark集群能夠相互訪問,如:同一個局域網sparkConf.setJars(new String[] {".\\out\\artifacts\\spark_test\\spark-test.jar"});//項目構建生成的路徑}SparkSession session = SparkSession.builder().appName(appName).config(sparkConf).config(sparkConf).getOrCreate();return session;} }groubByNumber.txt文件內容
A 100 A 24 B 43 C 774 D 43 D 37 D 78 E 42 C 68 F 89 G 49 F 543 H 36 E 888 A 258 A 538 B 79 B 6 H 67 C 99輸出
A:538 B:79 C:774 D:78 E:888 F:543 G:49 H:67?
四、combineByKey方法
<C> JavaPairRDD<K, C> combineByKey(Function<V, C> createCombiner, Function2<C, V, C> mergeValue, Function2<C, C, C> mergeCombiners);
首先介紹一下上面三個參數:
* Users provide three functions:
* ?- `createCombiner`, which turns a V into a C (e.g., creates a one-element list)
這個函數把當前的值作為參數,此時我們可以對其做些附加操作(類型轉換)并把它返回?(這一步類似于初始化操作)
* ?- `mergeValue`, to merge a V into a C (e.g., adds it to the end of a list)
該函數把元素V合并到之前的元素C(createCombiner)上 (這個操作在每個分區內進行)
* ?- `mergeCombiners`, to combine two C's into a single one.
該函數把2個元素C合并 (這個操作在不同分區間進行)
總結
以上是生活随笔為你收集整理的Spark入门(十四)之分组求最大值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一文教你如何挑选电脑如何挑选合适的电脑
- 下一篇: Spark入门(十五)之分组求最小值