首页 > 代码库 > Spark 编程模型(中)

Spark 编程模型(中)

技术分享

 

先在IDEA新建一个maven项目

技术分享

我这里用的是jdk1.8,选择相应的骨架

技术分享

技术分享

这里选择本地在window下安装的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.gong.spark</groupId>
  <artifactId>learning-spark</artifactId>
  <version>1.0-SNAPSHOT</version>
  <inceptionYear>2008</inceptionYear>
  <properties>
    <scala.version>2.10.4</scala.version>
    <spark.version>1.6.1</spark.version>
  </properties>

  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.specs</groupId>
      <artifactId>specs</artifactId>
      <version>1.2.5</version>
      <scope>test</scope>
    </dependency>
    <!--spark-->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.10</artifactId>
      <version>${spark.version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <!--
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        -->
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
          <args>
            <arg>-target:jvm-1.5</arg>
          </args>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <downloadSources>true</downloadSources>
          <buildcommands>
            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
          </buildcommands>
          <additionalProjectnatures>
            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
          </additionalProjectnatures>
          <classpathContainers>
            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
          </classpathContainers>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <!-- Run shade goal on package phase -->
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <!-- add Main-Class to manifest file -->
                <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <!--<mainClass>com.dajiang.MyDriver</mainClass>-->
                </transformer>
              </transformers>
              <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <configuration>
          <scalaVersion>${scala.version}</scalaVersion>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

 

 

先在终端下试下打包

技术分享

 

 技术分享

可以看到失败了!!!

把这几个生成默认的东西删除掉

技术分享

 

 再次测试

技术分享

 

成功了

技术分享

 

 把他clean一下

技术分享

技术分享

 

进入自己在虚拟机安装的centos里面的spark

技术分享

 

 

先测试一下spark环境有没有问题

技术分享

技术分享

OK运行成,环境没问题!

                                                                                                                                                           

 

 

 

 

                                                                               1.创建RDD

从集合创建RDD

技术分享

回到idea,在main路径下新建java目录,并且对其以下操作:

技术分享

 

 在test路径下新建java目录,对其以下操作:

技术分享

技术分享

 

建包

技术分享

技术分享

 

在当前包下起个名字,其实也就是在这个包的路径下再建下一级目录

技术分享

技术分享

 

 

技术分享

 

 

 因为我们现在要写的是java程序,所以新建一个java类

技术分享

技术分享

技术分享

 

 写个简单的程序测下运行一下

技术分享

技术分享

OK没问题,可以运行!

 

把这里的这个插件由原来的1.5改成1.8,因为刚刚跑的时候有警告

技术分享

 

 在这里新建一个包,具体怎么操作这里就不重复了

技术分享

 

插入之前写好的MyJavaWordCount.java的代码

技术分享

 

 MyJavaWordCount.java参考代码

package com.gong.spark.chap2_3;

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;

public class MyJavaWordCount {
     public  static  void main(String[] args){
         //参数检查
  if(args.length<2) {
      System.err.println("Usage:MyJavaCount <input> <output>");
      System.exit(1);
  }

         //获取参数
     String input=args[0];
     String output=args[1];

         //创建java版本的SparkContext
         SparkConf conf=new SparkConf().setAppName("MyJavaWordCount");
         JavaSparkContext sc = new JavaSparkContext(conf);

         //读取数据
         JavaRDD<String> inputRdd=sc.textFile(input);

         //进行相关计算
        JavaRDD<String> words=inputRdd.flatMap(new FlatMapFunction<String, String>() {
            public Iterable<String> call(String line) throws Exception {
                return Arrays.asList(line.split(" "));
            }
        });

        JavaPairRDD<String,Integer> result = words.mapToPair(new PairFunction<String, String, Integer>() {
             public Tuple2<String,Integer> call(String word) throws Exception{
                 return new Tuple2(word,1);
             }
         }).reduceByKey(new Function2<Integer, Integer, Integer>() {
             public Integer call(Integer x, Integer y) throws Exception {
                 return x+y;
             }
         });

         //保存结果
        result.saveAsTextFile(output);

         //关闭sc
     sc.stop();
     }
}

 

Spark 编程模型(中)