在Intellij IDEA 中编写WordCount程序并打包到集群测试

9 篇文章 0 订阅

在前面两篇博客的基础上:

https://blog.csdn.net/weixin_40096730/article/details/102782195

https://blog.csdn.net/weixin_40096730/article/details/89921176

package com.likexinTest.BigData.Spark

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

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

    //1.创建 SparkConf 并设置 App 名称
    val conf = new SparkConf().setAppName("WC")
    //2.创建 SparkContext,该对象是提交 Spark App 的入口
    val sc = new SparkContext(conf)
    //3.使用 sc 创建 RDD 并执行相应的 transformation 和 action
//    sc.textFile(args(0)).flatMap(_.split("  ")).map((_,
//      1)).reduceByKey(_+_, 1).sortBy(_._2, false).saveAsTextFile(args(1))

    //读取数据
    val line = sc.textFile(args(0))
    //压平
    val words = line.flatMap(_.split(" "))
    //map(word,1)
    val k2v = words.map((_,1))
    //reduceByKey(word,x)
    val result = k2v.reduceByKey(_+_)
    //展示输出
    result.saveAsTextFile(args(1))

    //4.关闭连接
    sc.stop()
  }
}

pom.xml文件

<?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.LikexinTest1</groupId>
    <artifactId>SparkDemo1</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>WordCount</finalName>
        <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>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>WordCount</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

打包工程!!

这里出现了error: scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found.错误

这里将JDK的路径选择好,(安装JDK后选择会自动识别路径)

打包完成!!!

将jar包存入服务器目录:

在IDEA中将自己的object选择copy refrence

现在读取hdfs上的测试文档,wordcount结果输出到out目录:

运行spark-submit

bin/spark-submit \
--class com.likexinTest.BigData.Spark.WordCount \
--master yarn \
--deploy-mode client \
/usr/local/BigDataApp/spark-2.1.1-bin-hadoop2.7/likexinTest/WordCount-jar-with-dependencies.jar \
hdfs://192.168.187.100:9000/hello.txt \
hdfs://192.168.187.100:9000/out

输出结果:

part-00000和part-00001都下载下来并查看,每次结果都一样但是两个part的数据量可能不一致。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值