使用eclipse开发spark程序 wordcount 事例

建立一个maven工程,把src下的java文件夹改为scala或者新添加scala文件夹

项目结构如下:

这里写图片描述

pom配置文件:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mySpark01</groupId>
    <artifactId>mySpark01</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.10.6</scala.version>
        <spark.version>1.6.1</spark.version>
        <hadoop.version>2.6.4</hadoop.version>
    </properties>

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

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>${spark.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</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-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lijie.spark.WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

WordCount类如下:

package cn.touna

import org.apache.spark.{SparkConf, SparkContext}

/**
  * User: lijie
  * Email:lijiewj51137@touna.cn 
  * Date:  
  * Time:  
  */
class WordCount {

}


object WordCount {
  def main(args: Array[String]): Unit = {
    var arg = Array("hdfs://lijie:9000/sz/access.log.fensi", "hdfs://lijie:9000/sz/out")
    //    val conf = new SparkConf().setMaster("spark://lijie:7077").setAppName("wd")
    val conf = new SparkConf().setMaster("local").setAppName("wd")
    val sc = new SparkContext(conf)
    println(1111)
    //    sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).aggregateByKey(0)(_ + _, _ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
    //    sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
    //    sc.textFile(arg(0)).flatMap(_.split(" ")).map((_,1)).combineByKey(x => x,(a:Int,b:Int) => a+b, (c:Int,d:Int) => c+d).sortBy(_._2,true).saveAsTextFile(arg(1))
    sc.textFile(arg(0)).flatMap(_.split(" ")).map((_, 1)).foldByKey(0)(_ + _).sortBy(_._2, true).saveAsTextFile(arg(1))
    println(2222)
    sc.stop
  }
}

执行打包操作

这里写图片描述

提交到服务器上面执行

这里写图片描述

执行命令如下:

[hadoop@cdhnode1 ~]$ /home/hadoop/app/spark-1.3.1-bin-hadoop2.6/bin/spark-submit --master spark://cdhnode1:7077 --class com.lijie.spark.WorldCount --executor-memory 512m --total-executor-cores 2 ./original-mySpark01-0.0.1-SNAPSHOT.jar 

执行结果:

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值