Spark2.x学习笔记:9、 Spark编程实例

9、 Spark编程实例

9.1 SparkPi

package cn.hadron
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import scala.math.random

object SparkPi {
  def main(args: Array[String]): Unit = {
    val masterUrl = "local[1]"
    val conf=new SparkConf().setMaster(masterUrl).setAppName("SparkPi")
    val sc=new SparkContext(conf)
    //启动Task数,默认2个
    val slices=if(args.length>0)args(0).toInt else 2
    // n是迭代次数(默认2w次),Int.MaxValue是防止溢出
    val n = math.min(100000L * slices, Int.MaxValue).toInt
    //默认两个patition,[1,100000]和[100001,20000]
    val count = sc.parallelize(1 until n, slices).map { i =>
      //产生的点范围[-1,1],圆心是(0,0)
      val x = random * 2 - 1
      val y = random * 2 - 1
      //如果产生的点落在圆内计数1,否则计数0
      if (x*x + y*y <= 1) 1 else 0
    }.reduce(_ + _)
    println("Pi is roughly " + 4.0 * count / (n - 1))
  }
}

9.2 平均值

(1)生成数据

[root@node1 data]# vi genAge.sh 
[root@node1 data]# cat genAge.sh 
#!/bin/sh

for i in {1..1000000};do
        echo -e $i'\t'$(($RANDOM%100))
done;
[root@node1 data]# sh genAge.sh > age.txt
[root@node1 data]# tail -10 age.txt 
999991  53
999992  63
999993  62
999994  14
999995  62
999996  27
999997  15
999998  99
999999  62
1000000 79

(2)上传到HDFS

[root@node1 data]# hdfs dfs -put age.txt input

(3)编写代码 AvgAge.scala

package cn.hadron
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object AvgAge {
  def main(args:Array[String]) {
    if (args.length < 1){
      println("Usage:AvgAge datafile")
      System.exit(1)
    }
    val conf = new SparkConf().setAppName("Spark Exercise:Average Age Calculator")
    val sc = new SparkContext(conf)
    val rdd = sc.textFile(args(0), 5);
    val count = rdd.count()
    val totalAge =rdd.map(line => line.split("\t")(1))
                      .map(age => Integer.parseInt(String.valueOf(age)))
                      .collect()
                      .reduce(_+_)
    println("Total Age:" + totalAge + ";Number of People:" + count )
    val avgAge : Double = totalAge.toDouble / count.toDouble
    println("Average Age is " + avgAge)
  }
}

(4)编译打包 (5)提交任务 spark-submit --master yarn --deploy-mode client --class cn.hadron.AvgAge /root/simpleSpark-1.0-SNAPSHOT.jar input/age.txt

[root@node1 ~]# spark-submit --master yarn --deploy-mode client --class cn.hadron.AvgAge /root/simpleSpark-1.0-SNAPSHOT.jar input/age.txt
17/09/22 10:30:47 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/09/22 10:30:56 WARN Client: Neither spark.yarn.jars nor spark.yarn.archive is set, falling back to uploading libraries under SPARK_HOME.
Total Age:49536760;Number of People:1000000                                     
Average Age is 49.53676
[root@node1 ~]# 

9.3 TopK

(1)问题描述 查找一个文本文件中词频最高的前K个词。 比如有1个txt格式的汉姆雷特Hamlet.txt,统计该文件中词频 最高的前10个。 (2)上传数据

[root@node1 data]# hdfs dfs -put Hamlet.txt input
[root@node1 data]# hdfs dfs -ls input
Found 3 items
-rw-r--r--   3 root supergroup     281498 2017-09-20 10:11 input/Hamlet.txt
-rw-r--r--   3 root supergroup         71 2017-08-27 09:18 input/books.txt
drwxr-xr-x   - root supergroup          0 2017-08-13 09:33 input/emp.bak
[root@node1 data]#

(3)spark-shell调试

[root@node1 data]# spark-shell
17/09/20 10:12:44 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/09/20 10:13:01 WARN ObjectStore: Version information not found in metastore. hive.metastore.schema.verification is not enabled so recording the schema version 1.2.0
17/09/20 10:13:02 WARN ObjectStore: Failed to get database default, returning NoSuchObjectException
17/09/20 10:13:04 WARN ObjectStore: Failed to get database global_temp, returning NoSuchObjectException
Spark context Web UI available at http://192.168.80.131:4040
Spark context available as 'sc' (master = local[*], app id = local-1505916766832).
Spark session available as 'spark'.
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.2.0
      /_/

Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112)
Type in expressions to have them evaluated.
Type :help for more information.

scala> val rdd1=sc.textFile("input/Hamlet.txt")
rdd1: org.apache.spark.rdd.RDD[String] = input/Hamlet.txt MapPartitionsRDD[1] at textFile at <console>:24

scala> rdd1.count
res0: Long = 6878

scala> val rdd2=rdd1.flatMap(x=>x.split(" ")).filter(_.size>1)
rdd2: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[3] at filter at <console>:26

scala> rdd2.take(2)
res1: Array[String] = Array(Hamlet, by)

scala> val rdd3=rdd2.map(x=>(x,1))
rdd3: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[4] at map at <console>:28

scala> rdd3.take(2)
res2: Array[(String, Int)] = Array((Hamlet,1), (by,1))

scala> val rdd4=rdd3.reduceByKey(_+_)
rdd4: org.apache.spark.rdd.RDD[(String, Int)] = ShuffledRDD[5] at reduceByKey at <console>:30

scala> rdd4.take(3)
res3: Array[(String, Int)] = Array((rises.,1), (Let,35), (lug,1))

scala> val rdd5=rdd4.map{case(x,y)=>(y,x)}
rdd5: org.apache.spark.rdd.RDD[(Int, String)] = MapPartitionsRDD[6] at map at <console>:32

scala> rdd5.take(2)
res4: Array[(Int, String)] = Array((1,rises.), (35,Let))

scala> val rdd6=rdd5.sortByKey(false)
rdd6: org.apache.spark.rdd.RDD[(Int, String)] = ShuffledRDD[7] at sortByKey at <console>:34

scala> rdd6.take(2)
res5: Array[(Int, String)] = Array((988,the), (693,and))

scala> val rdd7=rdd6.map{case(a,b)=>(b,a)}
rdd7: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[8] at map at <console>:36

scala> rdd7.take(10)
res6: Array[(String, Int)] = Array((the,988), (and,693), (of,621), (to,604), (my,441), (in,387), (HAMLET,378), (you,356), (is,291), (his,277))

scala> rdd7.take(10).foreach(println)
(the,988)
(and,693)
(of,621)
(to,604)
(my,441)
(in,387)
(HAMLET,378)
(you,356)
(is,291)
(his,277)

scala> 

(4)编写完整程序

package cn.hadron
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object TopK {
  def main(args: Array[String]): Unit = {
    if (args.length < 2) {
      println("Usage:TopK KeyWordsFile K");
      System.exit(1)
    }
    val conf = new SparkConf().setAppName("TopK Key Words")
    val sc = new SparkContext(conf)
    val rdd1 = sc.textFile(args(0))
    val result= rdd1.flatMap(x=>x.split(" "))
                    .filter(_.size>1)
                    .map(x=>(x,1))
                    .reduceByKey(_+_)
                    .map{case(x,y)=>(y,x)}
                    .sortByKey(false)
                    .map{case(a,b)=>(b,a)}
    result.take(10).foreach(println)
  }
}

(5)打包与上传

mvn package

(6)提交执行 spark-submit –master yarn –deploy-mode client –class cn.hadron.TopK /root/simpleSpark-1.0-SNAPSHOT.jar input/Hamlet.txt 10

[root@node1 ~]# spark-submit --master yarn --deploy-mode client --class cn.hadron.TopK /root/simpleSpark-1.0-SNAPSHOT.jar input/Hamlet.txt 10
17/09/21 09:48:08 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
(the,988)
(and,693)
(of,621)
(to,604)
(my,441)
(in,387)
(HAMLET,378)
(you,356)
(is,291)
(his,277)
[root@node1 ~]# 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Spark生态圈

[spark] Checkpoint 源码解析

在spark应用程序中,常常会遇到运算量很大经过很复杂的 Transformation才能得到的RDD即Lineage链较长、宽依赖的RDD,此时我们可以考虑将...

812
来自专栏智能大石头

Cortex-M3启动深度解析

Cortex-Mx启动,备忘,以免将来忘记。 中断向量表不用说,从重置中断开始吧 LDR R0, =SystemInit BLX R0 LDR ...

1726
来自专栏高性能服务器开发

这些年一直记不住的 Java I/O

阅读目录 参考资料 前言 从对立到统一,字节流和字符流 从抽象到具体,数据的来源和目的 从简单到丰富,使用 Decorator 模式扩展功能 Java 7 中引...

3095
来自专栏编码小白

tomcat请求处理分析(二) 启动mapperListener

1.1.1启动mapperListener     这个方法的核心就是注册Host的 public void startInternal() throws L...

3305
来自专栏恰同学骚年

Hadoop学习笔记—12.MapReduce中的常见算法

    "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选。统计大数据集上的数据种类个数、从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据...

602
来自专栏牛肉圆粉不加葱

[源码剖析]Spark读取配置Spark读取配置

我们知道,有一些配置可以在多个地方配置。以配置executor的memory为例,有以下三种方式:

703
来自专栏名山丶深处

springaop——AspectJ不可不知的细节

1217
来自专栏名山丶深处

springaop——AspectJ不可不知的细节

2635
来自专栏MasiMaro 的技术博文

枚举进程中的模块

在Windows中枚举进程中的模块主要是其中加载的dll,在VC上主要有2种方式,一种是解析PE文件中导入表,从导入表中获取它将要静态加载的dll,一种是利用查...

982
来自专栏后台及大数据开发

MongoDB基本命令用

成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作。

481

扫码关注云+社区