使用scala编写一个简单实例到spark集群运行

实际工作上很少在虚拟机上直接使用spark-shell去编写程序,更多的是在IDEA等编辑器上将写好的程序打包,使用spark-submit提交到集群上去执行。

我们使用scala去编写程序,不会的自己百度学下,不解释。

1,安装jdk

   因为scala也是运行在jvm上的,所以需要安装jdk。(jdk安装方法不解释,自己百度,建议安装1.7以上版本)

2,安装scala

笔者安装的是scala 2.10.6版本,需要jdk1.7及以上版本支持。

提供一个可参考的scala下载地址 https://www.scala-lang.org/download/2.10.6.html (方便起见建议下载windows msi版直接安装)

设置系统变量,添加一个SCALA_HOME,设置值为SCALA指定的安装目录,

在Path路径的末尾加

 ;%SCALA_HOME%\bin;%SCALA_HOME%\jre\bin;

在CLASSPATH路径末尾添加 

;%SCALA_HOME%\bin;%SCALA_HOME%\lib\dt.jar;%SCALA_HOME%\lib\tools.jar.;

配置后按 win + R 输入cmd 召唤窗口输入 scala -version ,查看是否配置安装成功。

3,在IDEA上使用编写程序

创建一个maven项目,创建过程不解释,提供一个我的 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>cn.itcast.spark</groupId>
    <artifactId>hello-spark</artifactId>
    <version>1.0</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>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

编写一个wordCount小程序,代码如下:

package cn.itcast.spark
import org.apache.spark. {SparkConf, SparkContext}
/**
  * Created by mrwanghc on 2018/7/17.
  */
object WordCount {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("WC")
    val sc = new SparkContext(conf)
    sc.textFile(args(0)).flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,false).saveAsTextFile(args(1))
    sc.stop()
  }
}

写完后将项目打包:

这里会生成两个包,一个是只包含代码的简洁包,另一个是包含jar包依赖的大包,保险起见我们使用打包即可。

将包上传到spark集群上, spark集群的搭建启动这里不解释,之前文章有详解,请自行查看。

在spark目录下输入命令

bin/spark-submit --master spark://weekend02:7077 --class cn.itcast.spark.WordCount --executor-memory 512m --total-executor-cores 2 /home/bigdata/hello-spark-1.0.jar hdfs://weekend02:9000/wc hdfs://weekend02:9000/out2

--master 指定集群master

--class 指定类所在地址

--executor-memory 512m 指定每个work运行内存为512m

--total-executor-cores 2    指定总共提供2个核处理给所有work

/home/bigdata/hello-spark-1.0.jar 提供上传的jar包所在目录

hdfs://weekend02:9000/wc         提供所需分析的文件所在hdfs中的目录

hdfs://weekend02:9000/out2      提供处理完后的文件要放到hdfs中某目录

输入完这条命令回车运行,若不报错,则完活!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值