[Original] Spark programming guide

Source: Internet
Author: User

Respect Original, pay attention to copyright, please note the original post address: http://www.cnblogs.com/vincent-hv/p/3322966.html 1, configure the Use of resources:

System.setProperty("spark.executor.memary", "512m") 
2. Create your own SparkContext object:
val sc =   SparkContext("master", "Job name", "$SPARK_HOME", "Job JARs")

 

3. Create RDDs
sc.parallelize(List(1, 2, 3))    sc.textFile("directory/*.txt")    sc.testFile("hdfs://namenode:port/path/file")    

 

4. Basic RDD Transformations operations:
val nums = sc.parallelize(List(1, 2, 3= nums.map(x => x * x)    val even = squares.filter(_ % 2 == 0)    nums.flatMap(x => 1 to x)    

 

5. Basic Actions operations:
val nums = sc.parallelize(List(1, 2, 3nums.take(2)    nums.count()    nums.reduce(_ + _)    nums.saveAsTextFile("hdfs://file.txt")

 

6. For the Key-Value pair job:
val pair =pair._2    
7. Operations on some Key-Value pairs:
val pets = sc.parallelize(List(("cat", 1), ("dog", 1), ("cat", 2+ _)    pets.groupByKey()    pets.sortByKey()    
8. Other Key-Value operations:
val visits = sc.parallelize(List(("index.html", "1.2.3.4"), ("about.html", "3.4.5.6"), ("index.html", 1.3.3.1= sc.parallelize(List(("index.html", "Home"), ("about.html", "About"vlisits.cogroup(pageNames)    

 

9. Control the number of Reduce Tasks: You can set the second parameter for all RDD group operations to control the number of tasks.
words.reduceByKey(_ + _, 555)
Of course, you can also set spark. default. parallelism attribute value to control 10. Use local variables: In the closure, you use any external variables to automatically pass to the cluster: val query = Console. readLine () pages. filter (_. contains (query )). count:
  • Each task gets a new copy (updates aren't sent bask)
  • Variable must be serialized
  • Do not use the external domain of an object
11. Examples of major hazards in the Cluster: 12. Other RDD operations:
  • Sample (): Determine the sample Subset
  • Union (): merge two RDDs.
  • Cartesian (): Cross Product
  • Pipe (): through external programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.