spark简单实例wordcount

视频地址:https://www.bilibili.com/video/BV11A411L7CK?p=25
参考教材:Spark大数据分析实战

文章内容:

  1. 在maven上部署spark依赖,运行wordcount
  2. 上传jar包在集群上运行wordcount
  3. map & flatMap

wordcount流程:
在这里插入图片描述
在这里插入图片描述
5. maven项目
用maven构建spark运行环境
项目依赖pom.xml

<dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext};

object WordCount {
  def main(args: Array[String]): Unit = {
    // 建立和spark框架的连接 连接配置
    val sparkConf = new SparkConf().setMaster("local").setAppName("WordCount");
    val sc = new SparkContext(sparkConf);
    // 执行业务操作
    // 1. 读取文件 获取一行一行数据
    val lines:RDD[String] = sc.textFile("datas");
    // 2. 将一行数据进行拆分 扁平化
    // val words = lines.flatMap(_.split(" "))
    val words:RDD[String] = lines.flatMap(line=>{line.split(" ")});
	// 3. 将原数据映射为我们想要的的数据
    val wordToOne = words.map(
      word => (word,1)
    )
    // 4. 在新数据上聚合计算
    val wordCount = wordToOne.reduceByKey((x,y)=>x + y)
    // 5. 打印结果
    val array = wordCount.collect()
    //array.foreach(println)
    array.foreach(item => println(item))
    // 关闭连接
    sc.stop();
  }
}
  1. 在集群上运行
    打包jar
package my.test

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Spark RDD单词计数程序
 */
object WordCount {

  def main(args: Array[String]): Unit = {
    //创建SparkConf对象
    val conf = new SparkConf()
    //设置应用程序名称,可以在Spark WebUI中显示
    conf.setAppName("Spark-WordCount")
    //设置集群Master节点访问地址
    conf.setMaster("yarn");

    //创建SparkContext对象,该对象是提交Spark应用程序的入口
    val sc = new SparkContext(conf);

    //读取指定路径(取程序执行时传入的第一个参数)中的文件内容,生成一个RDD集合
    val linesRDD: RDD[String] = sc.textFile(args(0))
    //将RDD数据按照空格进行切分并合并为一个新的RDD
    val wordsRDD: RDD[String] = linesRDD.flatMap(_.split(" "))
    //将RDD中的每个单词和数字1放到一个元组里,即(word,1)
    val paresRDD: RDD[(String, Int)] = wordsRDD.map((_, 1))
    //对单词根据key进行聚合,对相同的key进行value的累加
    val wordCountsRDD: RDD[(String, Int)] = paresRDD.reduceByKey(_ + _)
    //按照单词数量降序排列
    val wordCountsSortRDD: RDD[(String, Int)] = wordCountsRDD.sortBy(_._2, false)
    //保存结果到指定的路径(取程序执行时传入的第二个参数)
    wordCountsSortRDD.saveAsTextFile(args(1))
    //停止SparkContext,结束该任务
    sc.stop();
  }
}

打包得到一个.jar文件
wordcount.jar
传到集群上,启动dfs和yarn,用spark on yarn模式运行
首先在hdfs上创建一个 input文件夹存放输入数据
1.提交任务
在这里插入图片描述
2. 运行过程部分截图
提交任务到resourcemanager,RM选择节点分配container,并要求其启动AppMaster
在这里插入图片描述
AM开始启动任务
在这里插入图片描述
3. 结果
到对应output文件查看结果
在这里插入图片描述
map & flatMap

In Scala, flatMap() method is identical to the map() method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. It can be defined as a blend of map method and flatten method. The output obtained by running the map method followed by the flatten method is same as obtained by the flatMap(). So, we can say that flatMap first runs the map method and then the flatten method to generate the desired result.

map + flatten => flatMap

scala> val name = Seq("HELLO","WORLD")
name: Seq[String] = List(HELLO, WORLD)

scala> val res1 = name.map(_.toLowerCase)
res1: Seq[String] = List(hello, world)

scala> val res2 = res1.flatten
res2: Seq[Char] = List(h, e, l, l, o, w, o, r, l, d)

scala> name.flatMap(_.toLowerCase)
res0: Seq[Char] = List(h, e, l, l, o, w, o, r, l, d)

flatMap

scala> val text = List[String]("hello world","hello scala")
text: List[String] = List(hello world, hello scala)

scala> text.flatMap(_.split(" "))
res1: List[String] = List(hello, world, hello, scala)

RDD算子:https://blog.csdn.net/qq_43402639/article/details/117288236

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单Spark WordCount程序: ```python from pyspark import SparkConf, SparkContext conf = SparkConf().setAppName("WordCount") sc = SparkContext(conf=conf) text_file = sc.textFile("hdfs://localhost:9000/input/sample.txt") words = text_file.flatMap(lambda line: line.split(" ")) word_counts = words.map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b) word_counts.saveAsTextFile("hdfs://localhost:9000/output/wordcount") ``` 程序的功能是统计一个文本文件中每个单词出现的次数,并将结果保存到HDFS上。 下面是代码的详细解释: 首先,我们导入了`SparkConf`和`SparkContext`。这两个类是Spark的核心类,`SparkConf`用于配置Spark应用程序的属性,`SparkContext`用于连接Spark集群。 ```python from pyspark import SparkConf, SparkContext ``` 接下来,我们创建了一个`SparkConf`实例,并给应用程序命名为`WordCount`。我们还创建了一个`SparkContext`实例,并将`SparkConf`传递给它。这些代码将初始化Spark应用程序并连接到Spark集群。 ```python conf = SparkConf().setAppName("WordCount") sc = SparkContext(conf=conf) ``` 然后,我们使用`textFile()`方法从HDFS中读取输入文件,并创建一个RDD(弹性分布式数据集)。 ```python text_file = sc.textFile("hdfs://localhost:9000/input/sample.txt") ``` 接下来,我们使用`flatMap()`方法将每行文本拆分成单词,并创建一个新的RDD。 ```python words = text_file.flatMap(lambda line: line.split(" ")) ``` 然后,我们使用`map()`方法将每个单词转换为一个`(单词, 1)`的键值对,并创建一个新的RDD。 ```python word_counts = words.map(lambda word: (word, 1)) ``` 接下来,我们使用`reduceByKey()`方法对每个单词的计数进行聚合,并创建一个新的RDD。 ```python word_counts = word_counts.reduceByKey(lambda a, b: a + b) ``` 最后,我们使用`saveAsTextFile()`方法将结果保存到HDFS上,并指定输出目录。 ```python word_counts.saveAsTextFile("hdfs://localhost:9000/output/wordcount") ``` 这就是完整的Spark WordCount程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值