Spark操作实战
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操作实战的更多相关文章
- Spark入门实战系列--10.分布式内存文件系统Tachyon介绍及安装部署
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Tachyon介绍 1.1 Tachyon简介 随着实时计算的需求日益增多,分布式内存计算 ...
- Spark入门实战系列--1.Spark及其生态圈简介
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .简介 1.1 Spark简介 年6月进入Apache成为孵化项目,8个月后成为Apache ...
- Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark编程模型 1.1 术语定义 l应用程序(Application): 基于Spar ...
- Spark入门实战系列--3.Spark编程模型(下)--IDEA搭建及实战
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 . 安装IntelliJ IDEA IDEA 全称 IntelliJ IDEA,是java语 ...
- Spark入门实战系列--5.Hive(上)--Hive介绍及部署
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Hive介绍 1.1 Hive介绍 月开源的一个数据仓库框架,提供了类似于SQL语法的HQ ...
- Spark入门实战系列--5.Hive(下)--Hive实战
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.Hive操作演示 1.1 内部表 1.1.1 创建表并加载数据 第一步 启动HDFS ...
- Spark入门实战系列--6.SparkSQL(上)--SparkSQL简介
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .SparkSQL的发展历程 1.1 Hive and Shark SparkSQL的前身是 ...
- Spark入门实战系列--6.SparkSQL(中)--深入了解SparkSQL运行计划及调优
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.1 运行环境说明 1.1.1 硬软件环境 线程,主频2.2G,10G内存 l 虚拟软 ...
- Spark入门实战系列--6.SparkSQL(下)--Spark实战应用
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .运行环境说明 1.1 硬软件环境 线程,主频2.2G,10G内存 l 虚拟软件:VMwa ...
随机推荐
- EXtJS Ext.data.Model
(一).首先我们介绍一下Model类中比较常用的几个属性,如果我们想构建一个Model类,最主要的属性就是(Fields)属性,这个属性接受一个数组.用来设置Model中所包含的字段.定义的格式如下: ...
- Python自建collections模块
本篇将学习python的另一个内建模块collections,更多内容请参考:Python学习指南 collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtupl ...
- 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】
http://www.cnblogs.com/jiekzou/p/5047850.html Oracle的安装包通常都比较大,安装又比较费时,而且如果安装过程中不幸出错,各种蛋疼,即便是安装过N遍的老 ...
- python之路---05 字典 集合
二十.字典 可变数据类型 {key:value}形式 查找效率高 key值必须是不可变的数据类型 1.增删改查 1).增 dic["新key"] = "新v ...
- css 新单位 fr
fr是css刚出的一个新的单位,目前经过测试在chrome和firefox是可以支持的 举个案列,拿一个网格布局来说吧 <!DOCTYPE html> <html lang=&quo ...
- kafka 中的术语
出处:https://tech.meituan.com/2015/01/13/kafka-fs-design-theory.html table th:first-of-type { width: 7 ...
- fastjson总结
1,文件的转成字节数组byte[]的时候,可以直接用fastjson序列化和反序列化 2,用@RequestBody接受json的时候,content-type是否已经application/json ...
- java 标准输入输出System.in与System.out
System.in System.in 是 InputStream 类的实例对象,该对象的创建是由本地(native)方法完成的. public static final InputStream in ...
- GDI+ 库
uses Winapi.GDIPAPI, Winapi.GDIPOBJ{, Winapi.GDIPUTIL}; procedure TForm1.FormPaint(Sender: TObject); ...
- java多线程找素数实例
package ltb20180106; public class FindPrime implements Runnable{ private int prime; private int q; p ...