1. local模式

$SPARK_HOME/bin/spark-shell --master local
import org.apache.log4j.{Level,Logger} // 导入java log4j的日志相关类
Logger.getLogger("org.apache.spark").setLevel(Level.WARN) //设置日志级别
val data= sc.textFile("file:///home/workspace/software/spark-2.0.0/README.md")
data.collect // 显示所有内容
data.first // 显示第一条记录内容,本例中是String
data.take() // 显示第一条内容,但是是Array
val filterdData=data.filter(line=>line.contains("Spark")) //对数据集进行过滤
filterdData.collect //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
filterdData.count //行数统计
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b) //获取单词数最多的行
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b)) //获取单词数最多的行
val countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,)).reduceByKey((a,b)=>a+b) //单词个数统计
countResult.collect // 显示统计结果,可能比较消耗内存哦
data.map(line=>line.split(" ").size).top() //获取单词个数排名前三的统计结果 data.count //cache前的行数统计
data.cache //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别,可用RDD.unpersist()取消缓存的rdd
data.count //cache后的行数统计 data.saveAsTextFile("file:///home/workspace/countresult") //保存结果到文件系统/home/workspace/countresult文件夹下面

2. Spark standalone模式

先上传文件到hdfs

hdfs dfs -put /home/workspace/software/spark-2.0./README.md  /

spark操作

$SPARK_HOME/bin/spark-shell --master spark://192.168.1.193:7077
import org.apache.log4j.{Level,Logger} // 导入java log4j的日志相关类
Logger.getLogger("org.apache.spark").setLevel(Level.WARN) //设置日志级别
val data= sc.textFile("/README.md")
data.collect // 显示所有内容
data.first // 显示第一条记录内容,本例中是String
data.take() // 显示第一条内容,但是是Array
val filterdData=data.filter(line=>line.contains("Spark")) //对数据集进行过滤
filterdData.collect //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
filterdData.count //行数统计
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b) //获取单词数最多的行
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b)) //获取单词数最多的行
val countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,)).reduceByKey((a,b)=>a+b) //单词个数统计
countResult.collect // 显示统计结果,可能比较消耗内存哦
data.map(line=>line.split(" ").size).top() //获取单词个数排名前三的统计结果 data.count //cache前的行数统计
data.cache //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别,可用RDD.unpersist取消缓存的RDD
data.count //cache后的行数统计 data.saveAsTextFile("/countresult") //保存结果到hdfs的/countresult文件夹下面

最后保存的结果为:

3. yarn模式

$SPARK_HOME/bin/spark-shell --master yarn
import org.apache.log4j.{Level,Logger} // 导入java log4j的日志相关类
Logger.getLogger("org.apache.spark").setLevel(Level.WARN) //设置日志级别
val data= sc.textFile("/README.md")
data.collect // 显示所有内容
data.first // 显示第一条记录内容,本例中是String
data.take() // 显示第一条内容,但是是Array
val filterdData=data.filter(line=>line.contains("Spark")) //对数据集进行过滤
filterdData.collect //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
filterdData.count //行数统计
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b) //获取单词数最多的行
filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b)) //获取单词数最多的行
val countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,)).reduceByKey((a,b)=>a+b) //单词个数统计
countResult.collect // 显示统计结果,可能比较消耗内存哦
data.map(line=>line.split(" ").size).top() //获取单词个数排名前三的统计结果 data.count //cache前的行数统计
data.cache //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别
data.count //cache后的行数统计 data.saveAsTextFile("/countresult") //保存结果到hdfs的/countresult文件夹下面

还有一种mesos部署模式,因为环境没有做部署,没有做测试。

spark-shell参数列表:

[druid@palo101 workspace]$ spark-shell --help
Usage: ./bin/spark-shell [options] Options:
--master MASTER_URL spark://host:port, mesos://host:port, yarn, or local.
--deploy-mode DEPLOY_MODE Whether to launch the driver program locally ("client") or
on one of the worker machines inside the cluster ("cluster")
(Default: client).
--class CLASS_NAME Your application's main class (for Java / Scala apps).
--name NAME A name of your application.
--jars JARS Comma-separated list of local jars to include on the driver
and executor classpaths.
--packages Comma-separated list of maven coordinates of jars to include
on the driver and executor classpaths. Will search the local
maven repo, then maven central and any additional remote
repositories given by --repositories. The format for the
coordinates should be groupId:artifactId:version.
--exclude-packages Comma-separated list of groupId:artifactId, to exclude while
resolving the dependencies provided in --packages to avoid
dependency conflicts.
--repositories Comma-separated list of additional remote repositories to
search for the maven coordinates given with --packages.
--py-files PY_FILES Comma-separated list of .zip, .egg, or .py files to place
on the PYTHONPATH for Python apps.
--files FILES Comma-separated list of files to be placed in the working
directory of each executor. --conf PROP=VALUE Arbitrary Spark configuration property.
--properties-file FILE Path to a file from which to load extra properties. If not
specified, this will look for conf/spark-defaults.conf. --driver-memory MEM Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
--driver-java-options Extra Java options to pass to the driver.
--driver-library-path Extra library path entries to pass to the driver.
--driver-class-path Extra class path entries to pass to the driver. Note that
jars added with --jars are automatically included in the
classpath. --executor-memory MEM Memory per executor (e.g. 1000M, 2G) (Default: 1G). --proxy-user NAME User to impersonate when submitting the application.
This argument does not work with --principal / --keytab. --help, -h Show this help message and exit.
--verbose, -v Print additional debug output.
--version, Print the version of current Spark. Spark standalone with cluster deploy mode only:
--driver-cores NUM Cores for driver (Default: ). Spark standalone or Mesos with cluster deploy mode only:
--supervise If given, restarts the driver on failure.
--kill SUBMISSION_ID If given, kills the driver specified.
--status SUBMISSION_ID If given, requests the status of the driver specified. Spark standalone and Mesos only:
--total-executor-cores NUM Total cores for all executors. Spark standalone and YARN only:
--executor-cores NUM Number of cores per executor. (Default: in YARN mode,
or all available cores on the worker in standalone mode) YARN-only:
--driver-cores NUM Number of cores used by the driver, only in cluster mode
(Default: ).
--queue QUEUE_NAME The YARN queue to submit to (Default: "default").
--num-executors NUM Number of executors to launch (Default: ).
If dynamic allocation is enabled, the initial number of
executors will be at least NUM.
--archives ARCHIVES Comma separated list of archives to be extracted into the
working directory of each executor.
--principal PRINCIPAL Principal to be used to login to KDC, while running on
secure HDFS.
--keytab KEYTAB The full path to the file that contains the keytab for the
principal specified above. This keytab will be copied to
the node running the Application Master via the Secure
Distributed Cache, for renewing the login tickets and the
delegation tokens periodically.

Spark操作实战的更多相关文章

  1. Spark入门实战系列--10.分布式内存文件系统Tachyon介绍及安装部署

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Tachyon介绍 1.1 Tachyon简介 随着实时计算的需求日益增多,分布式内存计算 ...

  2. Spark入门实战系列--1.Spark及其生态圈简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .简介 1.1 Spark简介 年6月进入Apache成为孵化项目,8个月后成为Apache ...

  3. Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark编程模型 1.1 术语定义 l应用程序(Application): 基于Spar ...

  4. Spark入门实战系列--3.Spark编程模型(下)--IDEA搭建及实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 . 安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语 ...

  5. Spark入门实战系列--5.Hive(上)--Hive介绍及部署

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Hive介绍 1.1 Hive介绍 月开源的一个数据仓库框架,提供了类似于SQL语法的HQ ...

  6. Spark入门实战系列--5.Hive(下)--Hive实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.Hive操作演示 1.1 内部表 1.1.1 创建表并加载数据 第一步   启动HDFS ...

  7. Spark入门实战系列--6.SparkSQL(上)--SparkSQL简介

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .SparkSQL的发展历程 1.1 Hive and Shark SparkSQL的前身是 ...

  8. Spark入门实战系列--6.SparkSQL(中)--深入了解SparkSQL运行计划及调优

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.1  运行环境说明 1.1.1 硬软件环境 线程,主频2.2G,10G内存 l  虚拟软 ...

  9. Spark入门实战系列--6.SparkSQL(下)--Spark实战应用

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .运行环境说明 1.1 硬软件环境 线程,主频2.2G,10G内存 l  虚拟软件:VMwa ...

随机推荐

  1. dev 域名与 Chrome

    前几天在相开发时使用什么的样域名比较好呢? 之前使用到 localhost,但只是本地开发,使用 test.com 感觉又不专业. 后来发现了一些指向 127.0.0.1 的域名,比如 localte ...

  2. 深入探讨JavaScript如何实现深度复制(deep clone)

    在代码复用模式里面有一种叫做“复制属性模式”(copying properties pattern).谈到代码复用的时候,很有可能想到的是代码的继承性(inheritance),但重要的是要记住其最终 ...

  3. maven 阿里云 国内镜像 中央仓库

    修改maven根目录下的conf文件夹中的setting.xml文件,具体内容和示意图如下: <mirror> <id>alimaven</id> <name ...

  4. 记一个视频播放器插件 video.js

    最近在看扣丁学堂上面的一些视频, 突然对他用的视频播放器有点兴趣, 他也是采用的 ts切片播放, 如果使用传统的video标签是无法实现的 他使用的插件叫做 video.js 官网地址 官网提供的播放 ...

  5. nexus 私有 maven 仓库的搭建

    下载地址 页面 : https://help.sonatype.com/repomanager3/download 首先需要安装jdk .安装棒法我 前面的文章有写.https://www.cnblo ...

  6. 代码阅读笔记:【C-COT】

    [C-COT]:Danelljan M, Robinson A, Khan F S, et al. Beyond correlation filters: Learning continuous co ...

  7. 无界面运行Jmeter压测脚本 --后知者

    原文作者---后知者 原文地址:http://www.cnblogs.com/houzhizhe/p/8119735.html [后知者的故事]:针对单一接口压测时出现了从未遇到的问题,设好并发量后用 ...

  8. 性能优化之永恒之道(实时sql优化vs业务字段冗余vs离线计算)

    在项目中,随着时间的推移,数据量越来越大,程序的某些功能性能也可能会随之下降,那么此时我们不得不需要对之前的功能进行性能优化.如果优化方案不得当,或者说不优雅,那可能将对整个系统产生不可逆的严重影响. ...

  9. kindle电子书下载搜索

    kindle电子书下载搜索 https://sokindle.com/ https://sobooks.cc/

  10. CentOS 6 上安装 pip、setuptools

    通常 python 升级后,会带来一系列的出错信息,例如缺少pip.zlib.setuptools等,虽然你已经把python升级到了2.7版本,但是使用的依赖.模块还是旧的,所以要在新的 pytho ...