forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[YSPARK-414] Build spark-starter for yspark-2.0
[YSPARK-414] Build spark-starter for yspark-2.0 This change adds a Scala word count example which writes output to disk. Updates Java word count for spark 2.0. And adds examples in README file.
- Loading branch information
Dhruve Ashar
committed
Jun 29, 2016
1 parent
08b7d03
commit c716ce9
Showing
5 changed files
with
115 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
.idea/ | ||
spark-starter.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,64 @@ | ||
package com.yahoo.spark.starter; | ||
|
||
import scala.Tuple2; | ||
import org.apache.spark.SparkConf; | ||
|
||
import org.apache.spark.api.java.JavaPairRDD; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.api.java.function.FlatMapFunction; | ||
import org.apache.spark.api.java.function.Function2; | ||
import org.apache.spark.api.java.function.PairFunction; | ||
import org.apache.spark.sql.SparkSession; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
// A simple Word Count example in Java. | ||
public final class JavaWordCount { | ||
private static final Pattern SPACE = Pattern.compile(" "); | ||
private static final Pattern SPACE = Pattern.compile(" "); | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
if (args.length < 1) { | ||
System.err.println("Usage: JavaWordCount <file>"); | ||
System.exit(1); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
SparkSession spark = SparkSession | ||
.builder() | ||
.appName("JavaWordCount") | ||
.getOrCreate(); | ||
|
||
if (args.length < 1) { | ||
System.err.println("Usage: JavaWordCount <file>"); | ||
System.exit(1); | ||
JavaRDD<String> lines = spark.read().textFile(args[0]).javaRDD(); | ||
|
||
JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | ||
@Override | ||
public Iterator<String> call(String s) { | ||
return Arrays.asList(SPACE.split(s)).iterator(); | ||
} | ||
}); | ||
|
||
JavaPairRDD<String, Integer> ones = words.mapToPair( | ||
new PairFunction<String, String, Integer>() { | ||
@Override | ||
public Tuple2<String, Integer> call(String s) { | ||
return new Tuple2<>(s, 1); | ||
} | ||
}); | ||
|
||
SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount"); | ||
JavaSparkContext ctx = new JavaSparkContext(sparkConf); | ||
JavaRDD<String> lines = ctx.textFile(args[0], 1); | ||
|
||
JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() { | ||
@Override | ||
public Iterable<String> call(String s) { | ||
return Arrays.asList(SPACE.split(s)); | ||
} | ||
}); | ||
|
||
JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() { | ||
@Override | ||
public Tuple2<String, Integer> call(String s) { | ||
return new Tuple2<String, Integer>(s, 1); | ||
} | ||
}); | ||
|
||
JavaPairRDD<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() { | ||
@Override | ||
public Integer call(Integer i1, Integer i2) { | ||
return i1 + i2; | ||
} | ||
}); | ||
|
||
List<Tuple2<String, Integer>> output = counts.collect(); | ||
for (Tuple2<?, ?> tuple : output) { | ||
System.out.println(tuple._1() + ": " + tuple._2()); | ||
JavaPairRDD<String, Integer> counts = ones.reduceByKey( | ||
new Function2<Integer, Integer, Integer>() { | ||
@Override | ||
public Integer call(Integer i1, Integer i2) { | ||
return i1 + i2; | ||
} | ||
ctx.stop(); | ||
}); | ||
|
||
List<Tuple2<String, Integer>> output = counts.collect(); | ||
for (Tuple2<?,?> tuple : output) { | ||
System.out.println(tuple._1() + ": " + tuple._2()); | ||
} | ||
spark.stop(); | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/yahoo/spark/starter/ScalaWordCount.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.yahoo.spark.starter | ||
|
||
import org.apache.spark._ | ||
|
||
// Simple example of Word Count in Scala | ||
object ScalaWordCount { | ||
def main(args: Array[String]) { | ||
|
||
if (args.length < 2) { | ||
System.err.println("Usage: ScalaWordCount <inputFilesURI> <outputFilesUri>") | ||
System.exit(1) | ||
} | ||
|
||
val conf = new SparkConf().setAppName("Scala Word Count") | ||
val sc = new SparkContext(conf) | ||
|
||
// get the input file uri | ||
val inputFilesUri = args(0) | ||
|
||
// get the output file uri | ||
val outputFilesUri = args(1) | ||
|
||
val textFile = sc.textFile(inputFilesUri) | ||
val counts = textFile.flatMap(line => line.split(" ")) | ||
.map(word => (word, 1)) | ||
.reduceByKey(_ + _) | ||
counts.saveAsTextFile(outputFilesUri) | ||
|
||
sc.stop() | ||
} | ||
} |