RDD基础学习-[1]RDD建立与WordCount

本文介绍如何使用Apache Spark构建Resilient Distributed Datasets (RDD),包括从Scala数据集和外部文件创建RDD的方法,并演示了如何进行基本的数据处理操作如单词计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

构建RDD
[1]读外部文件: textFile()
[2]从scala数据集构建RDD: parallelize()

readme.txt

I love you
Please waiting for me
I will try my best to find you

import org.apache.spark.{SparkConf, SparkContext}
/**
  * Created by  on 16/7/17.
  */

//==========================================
/*
 构建RDD
 [1]读外部文件: textFile()
 [2]从scala数据集构建RDD: parallelize()
 */

object RDDWordCnt {
  def main(args: Array[String]) {
    val conf = new SparkConf()

    conf.setAppName("test")
    conf.setMaster("local")

    val sc = new SparkContext(conf)

    //==========================================
    /*
     构建RDD
     [1]从scala数据集构建RDD: parallelize()

     */
    val listRDDExample = sc.parallelize(List("1","2","3"),2)

    //==========================================
    /*
     获取分区数
     */
    val partitionsSzie = listRDDExample.partitions.size
    println(partitionsSzie)
    //2



    //==========================================
    /*
     构建RDD
     [2]读外部文件: textFile()

     */
    val txtFile = sc.textFile("./src/com/dt/spark/main/RDDLearn/wordCount/srcFile/readme.txt")

    //==========================================
    /*
     take(N)返回第N个元素,这里是返回第N行,注意返回类型:数组Array[T]

     */

    txtFile.take(1).foreach(println(_))
    // I love you

    //==========================================
    /*
     first返回第1个元素,这里是返回第1行,注意返回类型:String

     */
    txtFile.first().foreach(println(_))

    //==========================================
    /*
     count()计算元素个数

     */
    println(txtFile.count())
    //4

    //==========================================
    /*
     获得单次个数最多行的单次

     map对每个元素按照给定的function操作,
     */
    val max = txtFile.map(line=>line.split(" ").size).reduce((a,b)=>if(a>b) a else b)
    println(max)
    //8

    val max2 = txtFile.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b))
    println(max2)
    //8

    //==========================================
    /*
    单词计数

     */
    val words = txtFile.flatMap { line => line.split(" ") }
    val pairs = words.map { word => (word, 1) }
    val wordCounts = pairs.reduceByKey(_ + _)
    wordCounts.foreach(wordNumberPair => println(wordNumberPair._1 + " : " + wordNumberPair._2))
    //    find : 1
    //    Please : 1
    //    you : 2
    //    love : 1
    //    will : 1
    //    I : 2
    //    to : 1
    //      : 1
    //    best : 1
    //    try : 1
    //    for : 1
    //    my : 1
    //    waiting : 1
    //    me : 1
    sc.stop()


  }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值