Intellij idea下spark开发HelloWorld
来源:互联网 发布:申万宏源炒股软件下载 编辑:程序博客网 时间:2024/05/21 22:58
- 开发环境
Intellij idea 14
jdk: 1.7.71
spark: 1.1.0
hadoop: 2.4.0
scala: 2.11.1
maven: 3.2.5 - 创建maven工程
在src目录下创建main/java的source文件(在File –> Project Structure…–>Modules–>Sources右键添加目录和修改目录为source类型)
在File –> Project Structure…–>Libraries添加spark-assembly-1.1.0-hadoop2.4.0的依赖包
3. java目录下编写Wordcount例子程序
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.List;import java.util.regex.Pattern;public class JavaWordCount { private static final Pattern SPACE = Pattern.compile(" "); public static void main(String[] args)throws Exception { SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount"); String srcPath = null; String desPath = "/apps/ca/yanh/output"; if (args.length == 1) { srcPath = args[0]; } else if(args.length == 2) { srcPath = args[0]; desPath = args[1]; } else { System.out.println("Usage: java -jar jarName <src> [des]"); System.exit(1); } JavaSparkContext jsc = new JavaSparkContext(sparkConf); JavaRDD<String> lines = jsc.textFile(srcPath, 1); System.out.println("Begin to split!"); JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() { @Override public Iterable<String> call(String s) throws Exception { return Arrays.asList(SPACE.split(" ")); } }); System.out.println("Begin to map!"); JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() { @Override public Tuple2<String, Integer> call(String s) throws Exception { return new Tuple2<String, Integer>(s, 1); } }); System.out.println("Begin to reduce!"); JavaPairRDD<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() { @Override public Integer call(Integer i1, Integer i2) throws Exception { return i1 + i2; } }); System.out.println("Begin to save!"); /*List<Tuple2<String, Integer>> output = counts.collect(); for(Tuple2<?, ?> tuple: output) { System.out.println(tuple._1() + ": " + tuple._2()); }*/ counts.saveAsTextFile(desPath); jsc.stop(); }}
- 打包成jar包
在File –> Project Structure…–>Artifacts点击绿色“+”,Add–>JAR–>From Modules with Dependencies
输入main class入口函数名,将Output Layout下所有jar包删掉(因为spark运行环境已经包含了这些包),然后Apply
编译程序:Build–>Build Artifacts…,然后选择要编译的项目进行编译
在当前工程生成的out目录下就可以找到输出的jar包
- 运行程序
将jar包上传至spark集群,然后使用spark-submit进行提交运行(spark-submit具体参数自行查看)
提交命令:spark-submit –class JavaWordCount ~/JavaWordCount.jar /apps/ca/yanh/data/README.md
这是由于缺少本地库依赖和压缩包引起。
在此提供了这个包:http://pan.baidu.com/s/1rqkQa
spark-submit命令: spark-submit –driver-library-path :/usr/lib/hadoop/lib/native/ –jars /usr/lib/hadoop/lib/hadoop-lzo-0.6.0.jar –class JavaWordCount ~/JavaWordCount.jar /apps/ca/yanh/data/README.md
来自yhao2014
0 0
- Intellij idea下spark开发HelloWorld
- IntelliJ IDEA下的Spark程序开发
- 在IntelliJ IDEA下开发Spark应用的配置
- IntelliJ IDEA Windows下Spark开发环境部署
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- 使用IntelliJ IDEA开发Spring MVC HelloWorld
- IDEA下用SBT搭建Spark Helloworld
- 【Spark】在window下搭建spark开发环境(IntelliJ IDEA)
- Intellij Idea搭建Spark开发环境
- IntelliJ IDEA开发Spark、打JAR包
- IntelliJ Idea开发spark程序及运行
- intelliJ IDEA进行spark配置和开发
- 使用Intellij Idea搭建Spark开发环境
- 使用Intellij IDEA构建spark开发环境
- IntelliJ IDEA开发Spark、打JAR包
- IntelliJ IDEA开发Spark、打JAR包
- spark mac intellij idea开发环境
- Intellij Idea搭建Spark开发环境
- Ext Js 4.x Tree从Json加载数据
- Android源码分析工具及方法
- CodeChef 2016年04月问题
- mysql查询第n到第m条数据
- 获取view所在的控制器
- Intellij idea下spark开发HelloWorld
- update host
- php 学习笔记 -- 过滤器(七)
- Ubuntu系统编译整个osdrv时出现错误
- hdu 4826 Labyrinth【DP】
- ffmpeg解码流程
- iOS 9 by Tutorials 笔记(十三)Testing
- hdu 1548 A strange lift bfs搜索 解题报告
- ShapeDrawable