参考内容: http://blog.csdn.net/deng2012web/article/details/51931686
java程序:
package com.hq;
import org.apache.hadoop.conf.Configuration;
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 scala.Tuple2;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
public final class JavaWordCount {
private static final Pattern SPACE = Pattern.compile(" ");
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: JavaWordCount ");
System.exit(1);
}
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master:9000");
conf.set("mapreduce.jobtracker.address", "master:9001");
conf.set("yarn.resourcemanager.address", "master:8032");
conf.set("mapreduce.framework.name", "yarn");
conf.set("mapreduce.opp-submission.cross-platform", "true");
conf.set("yarn.resourcemanager.scheduler.address", "master:8030");
SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount");
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
JavaRDD lines = ctx.textFile(args[0], 1);
JavaRDD words = lines.flatMap(new FlatMapFunction() {
@Override
public Iterator call(String s) {
return Arrays.asList(SPACE.split(s)).iterator();
}
});
JavaPairRDD ones = words.mapToPair(new PairFunction() {
@Override
public Tuple2 call(String s) {
return new Tuple2(s, 1);
}
});
JavaPairRDD counts = ones.reduceByKey(new Function2() {
@Override
public Integer call(Integer i1, Integer i2) {
return i1 + i2;
}
});
List> output = counts.collect();
for (Tuple2, ?> tuple : output) {
System.out.println(tuple._1() + ": " + tuple._2());
}
ctx.stop();
}
}
有关脚本:
spark-submit --master spark://master:7077 --name WordCountByscala --class com.hq.WordCount --executor-memory 1G --total-executor-cores 2 jars/spark-wordcount-in-scala.jar hdfs://master:9000/user/word.txt
spark-submit --master spark://master:7077 --name JavaWordCountByHQ --class com.hq.JavaWordCount --executor-memory 1G --total-executor-cores 2 jars/spark-wordcount-in-scala.jar hdfs://master:9000/user/word.txt
spark-submit --master spark://192.168.0.76:7077 --name JavaWordCountByHQ --class com.hq.JavaWordCount --executor-memory 1G --total-executor-cores 2 spark-wordcount-in-scala.jar hdfs://192.168.0.76:9000/user/word.txt
spark-submit --master spark://master --name JavaWordCountByHQ --class com.hq.JavaWordCount --executor-memory 1G --total-executor-cores 2 spark-wordcount-in-scala.jar hdfs://master:9000/user/word.txt
阅读(1765) | 评论(0) | 转发(0) |