创建SparkContext对象的时候需要传递SparkConf对象,SparkConf至少需要包含spark.master和spark.app.name这两个参数,不然的话程序不能正常运行

object WordCount {

  def main(args: Array[String]) {

    val conf = new SparkConf();
// 设置应用的名称
conf.setAppName("WC")
// 设置master, local代表本地模式,可以直接在IDE中运行,也可以指定local[k],local[*]
conf.setMaster("local")
// spark集群模式,需要打成jar包,提交到spark集群运行
// conf.setMaster("spark://m1:7077") // 设置executor可以使用的内存大小
conf.set("spark.executor.memory", "512m") val sc = new SparkContext(conf) sc.textFile("hdfs://m1:9000/words.txt").flatMap(_.split(" ")).map((_, 1))
.reduceByKey(_+_).saveAsTextFile("hdfs://m1:9000/wcOutPut/")
sc.stop()
} }

maven pom.xml如下

<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>wordcount</groupId>
<artifactId>wordcount</artifactId>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<!-- 定义属性 --> <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.6.3</version>
</dependency> <dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.6.3</version>
</dependency> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.5</version>
</dependency>
</dependencies> <!-- 构建 -->
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<!-- maven管理scala插件-->
<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>
<!-- 在maven构建生命周期的test phase执行一个应用的单元测试 -->
<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> <!-- 使用maven插件对java工程进行打包 -->
<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>cn.itcast.spark.WordCount</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

打包提交spark集群运行

bin/spark-submit \
--class wordcount.WordCount \
--master spark://m1:7077 \
--executor-memory 512M \
--total-executor-cores 2 \
/home/hadoop/wordcount-1.0-SNAPSHOT.jar

本地运行如果hdfs权限有问题,则可以按如下配置

 

  

spark编写word count的更多相关文章

  1. Spark的word count

    word count package com.spark.app import org.apache.spark.{SparkContext, SparkConf} /** * Created by ...

  2. 在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤

    ---恢复内容开始--- 1.首先准备一个需要统计的单词文件 word.txt,我们的单词是以空格分开的,统计时按照空格分隔即可 hello hadoop hello yarnhello zookee ...

  3. Spark: 单词计数(Word Count)的MapReduce实现(Java/Python)

    1 导引 我们在博客<Hadoop: 单词计数(Word Count)的MapReduce实现 >中学习了如何用Hadoop-MapReduce实现单词计数,现在我们来看如何用Spark来 ...

  4. [Spark Core] Spark Shell 实现 Word Count

    0. 说明 在 Spark Shell 实现 Word Count RDD (Resilient Distributed dataset), 弹性分布式数据集. 示意图 1. 实现 1.1 分步实现 ...

  5. Spark:java api实现word count统计

    方案一:使用reduceByKey 数据word.txt 张三 李四 王五 李四 王五 李四 王五 李四 王五 王五 李四 李四 李四 李四 李四 代码: import org.apache.spar ...

  6. MapReduce工作机制——Word Count实例(一)

    MapReduce工作机制--Word Count实例(一) MapReduce的思想是分布式计算,也就是分而治之,并行计算提高速度. 编程思想 首先,要将数据抽象为键值对的形式,map函数输入键值对 ...

  7. [Hive_add_6] Hive 实现 Word Count

    0. 说明 Hive 通过 explode()函数 和 split()函数 实现 WordConut 1. Hive 实现 Word Count 方式一 1.1 思路 将每一行文本变为 Array 数 ...

  8. [MapReduce_1] 运行 Word Count 示例程序

    0. 说明 MapReduce 实现 Word Count 示意图 && Word Count 代码编写 1. MapReduce 实现 Word Count 示意图 1. Map:预 ...

  9. 软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序

    软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序 格式:wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数 ...

随机推荐

  1. RHCS 6.5 由于resource-agents-3.9.2-40.el6版本过低导致rgmanager[61164]: [fs] umount failed - REBOOTING问题的解决

    問題描述: RHEL 6.5版本RHCS在disable或者relocate service的時候,會導致節點重啟,查看日誌顯示umount掛载點失敗,日誌如下: Nov 29 16:03:50 ph ...

  2. [Java 基础]sun.misc.Unsafe

    使用Unsafe可以干一些好事. Unsafe对象初始化 如果你的代码属于trusted的,可以直接使用下面的方式: public static Unsafe getUnsafe() { Class ...

  3. MSSQ调优所需用的语句

    看一下各项指标是否正常,是否有阻塞,这个语句选取了前10个最耗CPU时间的会话语句 [session_id], [request_id], [start_time] AS '开始时间', [statu ...

  4. Html 移动web开发细节处理

    1.-webkit-tap-highlight-color:rgba(255,255,255,0)可以同时屏蔽ios和android下点击元素时出现的阴影.备注:transparent的属性值在and ...

  5. tomcat找不到class的情况分析

    例如:java.lang.ClassNotFoundException: org.apache.axis2.AxisFault 1,真实的缺包,这是使用该jar包的java程序也会一般会直接报错,无法 ...

  6. OS X El Capitan的 U 盘制作过程

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”     0.在 AppStore 下载 OS X El Capitan AppStore ...

  7. strncpy,strcpy

    strncpy不会为des自动添加“\0” strcpy遇空结束,自动添加结束符 结论: 1.使用strcpy时一定不能用于无结束符的字符串,因为strcpy依赖\0判断源字符串的结束 2.使用str ...

  8. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. ubuntu安装配置elasticSearch(vagrant)

    安装jdk sudo apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java s ...

  10. idea intellij 混淆anroid代码

    idea intellij 混淆anroid代码 在project.properties中加入 target=android-14proguard.config=proguard.cfg 点击 Bui ...