赞
踩
Java版本是1.8,不能是18,16,15或者其他版本。
Scala版本是2.13.1版本。
至此spark安装成功!
import org.apache.spark.SparkConf import org.apache.spark.SparkContext object WordCount2 { def main(args: Array[String])={ val conf = new SparkConf() .setMaster("local") .setAppName("WordCount") val sc = new SparkContext(conf) val lines = sc.textFile("E:\\WordCount-Data.txt") val words = lines.flatMap { line => line.split(" ") } val pairs = words.map { word => (word, 1) } val wordCounts = pairs.reduceByKey { _ + _ } wordCounts.foreach(wordCount => println(wordCount._1 + "出现了" + wordCount._2 + "次")) sc.stop() } }
import java.util.Arrays; import java.util.Iterator; 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.api.java.function.VoidFunction; import scala.Tuple2; public class WordCount3{ public static void main(String[] args) { // 第一步:创建SparkConf对象,设置相关配置信息 SparkConf conf = new SparkConf(); conf.setAppName("WordCount_Java"); conf.setMaster("local"); // 第二步:创建JavaSparkContext对象,SparkContext是Spark的所有功能的入口 JavaSparkContext sc = new JavaSparkContext(conf); // 第三步:创建一个初始的RDD // SparkContext中,用于根据文件类型的输入源创建RDD的方法,叫做textFile()方法 JavaRDD<String> lines = sc.textFile("E:\\WordCount-Data.txt"); // 第四步:对初始的RDD进行transformation操作,也就是一些计算操作 JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() { private static final long serialVersionUID = 1L; @Override public Iterator<String> call(String s) throws Exception { return Arrays.asList(s.split(" ")).iterator(); } }); JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, Integer> call(String word) throws Exception { return new Tuple2<String, Integer>(word, 1); } }); JavaPairRDD<String, Integer> wordCounts = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() { private static final long serialVersionUID = 1L; @Override public Integer call(Integer v1, Integer v2) throws Exception { return v1 + v2; } }); wordCounts.foreach(new VoidFunction<Tuple2<String,Integer>>() { private static final long serialVersionUID = 1L; @Override public void call(Tuple2<String, Integer> wordCount) throws Exception { System.out.println(wordCount._1 + "------" + wordCount._2+"次"); } }); sc.close(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。