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