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. 怎样解题 (G. 波利亚 著)

    第一部分 (已看) 目的 1. 帮助学生 2. 问题,建议,思维活动 3. 普遍性 4. 常识 5. 教师和学生,模仿和实践 主要部分,主要问题 6. 四个阶段 7. 理解题目 8. 例子 9. 拟订 ...

  2. 关于 TypeReference 的解释

    首先 TypeReference  是描述 一个复杂 泛型的工具类. TypeReference 很多类库都有,用 fastjson 的 举例,大概就这个意思. 例子: Response<Fee ...

  3. jp@gc - Stepping Thread Group配置解释描述

    测试环境 apache-jmeter-2.13   插件: https://jmeter-plugins.org/downloads/old/ http://pan.baidu.com/s/1gfC1 ...

  4. Kafka Stream

    Kafka Stream是Apache Kafka从0.10版本引入的一个新Feature(当前:1.0.0-rc0,参见:https://github.com/apache/kafka/releas ...

  5. C/S,B/S的应用区别

    C/S,B/S的应用区别 C/S即大家熟知的客服机和服务器结构通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销 B/S结构即浏览器和服务 ...

  6. 查询某个SPID,session_id对应的执行sql.

    select er.session_id, CAST(csql.text AS varchar(255)) AS CallingSQL from master.sys.dm_exec_requests ...

  7. tomcat源码 分析 Catalina

    通过查看分析启动脚本,发现最终调用的入口是org.apache.catalina.startup包下面的Bootstrap#main public static void main(String ar ...

  8. PHP 下载中文乱码解决

    利用 iconv() 函数解决乱码 $file_name = iconv("utf-8","gb2312",$file_name); 原文链接 http://m ...

  9. .NET数据请求序列化

    这是一个.ner框架中经常用到的数据映射类,此类主要承接业务应用做数据的增删查改操作.通过序列化将查询数据映射为需要的类. 由于经常会用到,这里收藏一下. data用作SQL数据库链接操作: Repo ...

  10. P2670扫雷

    链接 这是一个并不像搜索的题(其实它是个循环) 对于输入的a数组,一个一个遍历下来,如果a[i][j]是雷,那(i,j)周围8个点对应的位置雷数就+1(用b数组记录),注意不能超出边界,输出时,如果a ...