IntelliJ IDEA Maven,Eclispe Maven,sbt 创建spark Scala和Java项目

方式一:使用Eclipse创建Java项目

1、 新建一个项目:

这里写图片描述

2、 选择maven项目:

这里写图片描述

3、选择quickstart

这里写图片描述

4、填写相关信息

这里写图片描述

5、添加maven依赖

这里写图片描述

6、写一个经典的WordCount测试一下(Java代码)

Java代码:

public class WordCount {

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("error");
            System.exit(1);
        }

        SparkConf conf = new SparkConf();
        conf.setAppName("java word count");

        // 本地运行
        // conf.setMaster("local") ;

        // 集群模式运行
        conf.setMaster("spark://master:7077");

        JavaSparkContext sc = new JavaSparkContext(conf);

        sc.addJar("D:\\project\\spark\\target\\spark-0.0.1-SNAPSHOT.jar");

        JavaRDD<String> file = sc.textFile(args[0]);

        JavaRDD<String> word = file.flatMap(
                new FlatMapFunction<String, String>() {

            public Iterator<String> call(String t) throws Exception {
                String[] split = t.split(" ");

                return Arrays.asList(split).iterator();
            }
        });

        JavaPairRDD<String, Integer> mapd = word.mapToPair(
                new PairFunction<String, String, Integer>() {

            public Tuple2<String, Integer> call(String t) throws Exception {
                return new Tuple2(t, 1);
            }
        });

        JavaPairRDD<String, Integer> result = mapd.reduceByKey(
                new Function2<Integer, Integer, Integer>() {

            public Integer call(Integer x, Integer y) throws Exception {

                return x + y;
            }
        });

        result.repartition(1).saveAsTextFile(args[1]);

        sc.close();
        sc.stop();

    }

}

方式二:Intellij IDEA 创建Scala maven项目(Eclipse和此类似)

1、新建一个项目

这里写图片描述

2、 Maven项目

这里写图片描述

3、填入Maven参数

这里写图片描述

4、选择上一步新建的Maven

这里写图片描述

5、填写相关参数,然后下一步,直到项目完成

这里写图片描述

6、然后开始漫长的等待。。。。。。。。

这里写图片描述

7、下载完成后,如果有依赖报错,,,可以到Maven库找到对应的版本,修改版本号即可解决。。。,,,然后添加spark依赖

<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>scala.test</groupId>
  <artifactId>scalaMVN</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2010</inceptionYear>
  <licenses>
    <license>
      <name>My License</name>
      <url>http://....</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.tools.version>2.11</scala.tools.version>
    <scala.version>2.11.8</scala.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.11</artifactId>
      <version>2.0.1</version>
    </dependency>




  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <!--<arg>-make:transitive</arg>-->
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.13</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

9、然后就可以写一个经典的WordCount了。。。。,如果在运行的时候报错(好像是 -make。。。。什么的),可以到pom.xml文件中找到

             <arg>-make:transitive</arg>  将其注释或者删除即可

Scala代码:

object WordCount {
  def main(args: Array[String]) {


    if(args.length<1){
      println("参数不正确")
      System.exit(1)
    }

      val conf = new SparkConf().setAppName("word count").setMaster("spark://master:7077")

     val conf = new SparkConf().setAppName("word count").setMaster("local")

      val sc = new SparkContext(conf)
      sc.addJar("D:\\project\\scalaMVN\\out\\artifacts\\scalaMVN_jar\\scalaMVN.jar")


      val file = sc.textFile(args(0))
      val result = file.flatMap(x=>x.split(" ")).map(x=>(x,1)).reduceByKey((x,y)=>x+y)
     result.saveAsTextFile(args(1))
    sc.stop()
  }
}

方式三:利用sbt创建项目

1 、新建项目,,与前面一样,不在赘述。。。。

2、 新建sbt项目

这里写图片描述

3、这里写图片描述

这里写图片描述

4、老规矩,经典的WordCount,,,,和前面的代码一样,,,,,

PS:个人感觉,用Intellij IDEA + Scala 开发spark项目最为方便。。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值