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 ...
随机推荐
- leetcode第一刷_Insert Interval
这道题的难度跟微软的那道面试题相当. 要在集合中插入一段新的集合,相当于求两个集合的并了.对于新增加一段集合的情况,分为以下几种: 1. 增加段跟原来的全然相交,也即他的起点和终点都在被包括在原来的段 ...
- 程序设计实践 (Brian W. Kernighan Rob Pike 著)
第1章 风格 1.1 名字 1.2 表达式和语句 1.3 一致性和习惯用法 1.4 函数宏 1.5 神秘的数 1.6 注释 1.7 为何如此费心 第2章 算法与数据结构 2.1 检索 2.2 排序 2 ...
- day40 css高级选择器
一.高级选择器 高级选择器分为:后代选择器.子代选择器.并集选择器.交集选择器 后代选择器 使用空格表示后代选择器.顾名思义,父元素的后代(包括儿子,孙子,重孙子) .container p{ col ...
- gevent原理
未看 http://blog.csdn.net/yueguanghaidao/article/details/24281751
- HBase源码分析之WAL
WAL(Write-Ahead Logging)是数据库系统中保障原子性和持久性的技术,通过使用WAL可以将数据的随机写入变为顺序写入,可以提高数据写入的性能.在hbase中写入数据时,会将数据写入内 ...
- python装饰器学习笔记
定义:本质上就是个函数,(装饰器其他函数)就是为了给其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 #-*-coding:utf-8-*- 1 imp ...
- ElasticeSearch(五)分布式索引架构
关于分布式架构 首先将ES默认每个索引是5个分片,这样做得目的是两个一个索引的时候速度更快(将数据写到小分片的尾部比写入大分片尾部更加快):另外一个是当数据量达到一定程度之后,分片查询,在汇总(sca ...
- insert into on duplicate key update
问题 有一个表,建表语句如下: CREATE TABLE `tbl_host` ( `id` bigint(64) NOT NULL AUTO_INCREMENT, `ip` varchar(255) ...
- XML 增、删、改
作为一个小型的数据存储传递的工具——XML,大家肯定也不陌生,今天就关于XML的一些简单操作做些总结. 这些都是在控制台进行操作的····· 1.创建XML 1)创建普通XML static void ...
- 黄聪:jquery+Datatables出现数据过长,表格不自动换行,columns设置width失效的办法
添加下面的CSS代码即可: table.dataTable.nowrap th, table.dataTable.nowrap td{white-space: normal !important;}