http://blog.csdn.net/xiefu5hh/article/details/51707529
分类:
spark(5)
版权声明:本文为博主原创文章,未经博主允许不得转载。
前言
本文旨在记录初学Spark时,根据官网快速入门中的一段Java代码,在Maven上建立应用程序并实现执行。
首先推荐一个很好的入门文档库,就是CSDN的Spark知识库,里面有很多spark的从入门到精通的形形色色的资料,
1.开发软件恭喜你,拿到spark驾考名额了,可以开始参加驾校培训了~http://lib.csdn.net/base/spark
大概理解下:spark主要分为 1.核心 2.实时streaming 3.对sql支持sparksql 4.机器学习mllib 还有别的暂时不考虑
本文只引入sparkcore核心部门案例入门,其他的同学们自己去探索吧~
环境准备: window电脑一台是不可少的,不然怎么玩
其他需要安装的软件: 版本可以自己选择 ,以下是我的选择
1) JDK 版本:1.7
2) Maven 版本:3.2.3 : http://maven.apache.org/
3) Spark 版本:spark-1.1.0 http://spark.apache.org/
4》eclipse
注意安装的软件的位数和操作系统的位数。
1.JDK安装
具体安装步骤过于简单 略~
2.MAVEN安装
下载解压,具体安装步骤过于简单 略~
3.spark 安装
下载解压,具体安装步骤过于简单 略~
4.eclipse 安装
下载解压,具体安装步骤不是很简单,不能略,因为要安装maven
那就下载有maven的eclipse版本即可,推荐luna的eclipse 自带maven的
下载winutil 放到spark 的bin目录下
配置环境变量
需要配置 JAVA_HOME HADOOP_HOME(配置为spark_Home,为winutil使用) SPARK_HOME MAVEN_HOME 并在path中加入以上三个bin
cmd中分别测试 mvn -v java -version spark-shell 安装是否成功
工程构建
建立一个统计 文件中出现某个字符的行数。
建立Maven工程
修改pom.xml为:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dt.spark</groupId>
<artifactId>SparkApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SparkApps</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<maniClass></maniClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>false</includeProjectDependencies>
<classpathScope>compile</classpathScope>
<mainClass>com.dt.spark.SparkApps.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dt.spark</groupId>
<artifactId>SparkApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SparkApps</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<maniClass></maniClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>false</includeProjectDependencies>
<classpathScope>compile</classpathScope>
<mainClass>com.dt.spark.SparkApps.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
修改完成后,需要下载比较多东西来构建工作空间, 此过程后可以先去吃个饭,跑两圈再回来~
编写代码
public static void main( String[] args )
{
String readme="D:\\spark\\CHANGES.txt";
SparkConf conf=new SparkConf().setAppName("tiger's first spark app");
JavaSparkContext sc =new JavaSparkContext(conf);
JavaRDD<String> logData=sc.textFile(readme).cache();
long num=logData.filter(new Function<String,Boolean>(){
public Boolean call(String s){
return s.contains("a");
}
}).count();
System.out.println("the count of word a is "+num);
}
运行程序
编译应用程序
进入workplace项目,运行mvn compile,编译代码
打包程序
运行mvn package,系统将自动将应用程序代码打包成jar。运行成功后,在项目文件夹下的target文件夹下,将生成名为SimApp-1.0-SNAPSHOT.jar。
运行程序
使用spark-submit在本地运行应用 在elipse 下面参数总是出现错误,还在探索中。。
在cmd中输入 spark文件包bin下spark-submit文件所在的路径, 类名(加上包名),jar包所在的路径
D:\Spark_Tools\apache-maven-3.2.3\Maven_Project\SimApp>d:\Spark_Tools\spark-1.1.0-bin-hadoop1\bin\spark-submit --class "aa" --master local[4] bb
aa 是程序的主程序路径 com..xxxxxx.xxxx bb 是你打包的jar 路径 在target下 ,由pom.xml配置得到的。
在输出信息的最后一行,将看到程序的运行结果:
the count of word a is 20132
恭喜你,拿到spark驾照。。可以开车了... 上路需小心,多练习吧~
有问题可以留言,欢迎探讨,我会积极回复!~