当前位置:网站首页>RDD creation method of spark

RDD creation method of spark

2022-07-06 02:04:00 Diligent ls

         stay Spark Created in RDD There are three ways to create : Create... From the collection RDD、 Create... From external storage RDD、 From the other RDD establish .

Creation time environment dependency

<dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

<build>
    <finalName>SparkCoreTest</finalName>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.4.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

1. Create... From collection

object createrdd {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
          .setAppName("SparkCoreTest")
          .setMaster("local[*]")
    val sc: SparkContext = new SparkContext(conf)

    // Use parallelize() establish rdd
    //val rdd: RDD[Int] = sc.parallelize(Array(1,2,3,4,5,6))

   // rdd.collect().foreach(println)
    // Use makeRDD() establish rdd
    val rdd1: RDD[Int] = sc.makeRDD(Array(1,2,3,4,5,6))
    rdd1.collect().foreach(println)
    sc.stop()
  }
}

notes :makeRDD Not exactly equal to parallelize, In one of the refactoring methods ,makeRDD Added location information .

2. Create from a dataset of an external storage system

object crearedd2 {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
          .setAppName("WC")
          .setMaster("local[*]")
    val sc: SparkContext = new SparkContext(conf)

    val value: RDD[String] = sc.textFile("input")
    value.foreach(println)
    sc.stop()
  }
}

3. From the other RDD establish

         Mainly through a RDD After the calculation , And create new RDD.

原网站

版权声明
本文为[Diligent ls]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140042491007.html