windows下idea编写WordCount程序,并打jar包上传到hadoop集群运行(傻瓜版)

通常会在IDE中编制程序,然后打成jar包,然后提交到集群,最常用的是创建一个Maven项目,利用Maven来管理jar包的依赖。

一、生成WordCount的jar包

1. 打开IDEA,File→New→Project→Maven→Next→填写Groupld和Artifactld→Next→Finish

2. 配置Maven的pom.xml(配置好pom.xml以后,点击Enable Auto-Import即可):

<?xml version="1.0" encoding="UTF-8"?>
<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>com.wu</groupId>
    <artifactId>sparkWordCount</artifactId>
    <version>1.0-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>
        <scala.compat.version>2.10</scala.compat.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>1.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.10</artifactId>
            <version>1.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.2</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.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.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.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.wu.WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

注意:这里需要修改Hadoop版本

3:将src/main/java和src/test/java分别修改成src/main/scala和src/test/scala,与pom.xml中的配置保持一致();

操作:java→Refactor→Rename

4:新建一个com.bie包,再新建一个scala class,类型为Object,spark程序如下:

package com.wu

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

object WordCount {
  def main(args: Array[String]): Unit = {
    //创建SparkConf()并且设置App的名称
    val conf = new SparkConf().setAppName("wordCount");
    //创建SparkContext,该对象是提交spark app的入口
    val sc = new SparkContext(conf);
    //使用sc创建rdd,并且执行相应的transformation和action
    sc.textFile(args(0)).flatMap(_.split(" ")).map((_ ,1)).reduceByKey(_ + _,1).sortBy(_._2,false).saveAsTextFile(args(1));
    //停止sc,结束该任务
    sc.stop();
  }
}

5. 修改pom.xml中的mainClass,使其和自己的类路径对应起来:

6. 使用Maven打包:点击IDEA右侧的Maven Project选项,点击Lifecycle,选择clean和package,然后点击Run Maven Build:

等待编译完成,选择编译成功的jar包,target/sparkWordCount-1.0-SNAPSHOT.jar

二、运行

1. 打开xshell,文件→新建连接

新建好后输入用户名和密码,建立连接。

2. 使用Xftp新建文件传输(Ctrl+Alt+F),将刚刚生成的jar包和WordCount拖拽至 /home/hdfs目录下

3. 使用Xshell将WordCount.txt上传至hdfs系统

切换至hdfs用户:[root@data6 ~]# su hdfs

到spark的bin目录下:[hdfs@data6 root]$ cd /home/hdfs/software/spark/bin

在hdfs系统中新建input文件夹:hadoop fs -mkdir /input

查看是否新建成功:[hdfs@data6 bin]$ cd /home/hdfs/software/hadoop/bin     #转到该目录下

                                [hdfs@data6 bin]$ ./hadoop fs -ls /

将txt文件上传至input文件夹:[hdfs@data6 root]$ cd /home/hdfs/software/spark/bin      #转回到该目录

                                               [hdfs@data6 bin]$ hadoop fs -put /home/hdfs/WordCount.txt /input

查看是否上传成功:[hdfs@data6 bin]$ cd /home/hdfs/software/hadoop/bin     #转到该目录下

                                [hdfs@data6 bin]$ ./hadoop fs -ls /input

返回hdfs用户根目录:cd ~

使用spark-submit命令提交Spark应用:[hdfs@data6 ~]$ /home/hdfs/software/spark/bin/spark-submit --class com.bie.WordCount sparkWordCount-1.0-SNAPSHOT.jar hdfs://data2.cshdp.com:9000/input/WordCount.txt hdfs://data2.cshdp.com:9000/output

查看运行结果:[hdfs@data6 bin]$ cd /home/hdfs/software/hadoop/bin     #转到该目录下

                         [hdfs@data6 bin]$ ./hadoop fs -ls /output

                         [hdfs@data6 bin]$ ./hadoop fs -cat /output/part-00000   #查看文件内容

猜你喜欢

转载自blog.csdn.net/W950507PW123456789/article/details/83374954