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. zsh:no matches found 问题解决

    解决方法: ~/.zshrc 文件加入: setopt no_nomatch 之后,更新配置 source ~/.zshrc        

  2. powerdesigner16.5 破解

    powerdesigner16.5 破解 方法: 破解方法 1.将下载下来的PowerDesigner165_破解文件.rar进行解压,之后找到pdflm16.dll破解文件,并将pdflm16.dl ...

  3. day 29 socketsetserver 模块

    1.FTP上传/下载服务端/客户端. --------------------------------------------------------------------------------- ...

  4. async await 的 实质 本质

    async await  的 实质 就是 用 “状态机” 来 取代 函数层层调用 . async await  的 本质 是 语法糖,  和 提高性能 什么的 没什么关系 . 为了避免理解歧义, 我把 ...

  5. MySQL 学习资料

    MySQL 学习资料: MySQL 学习资料 网址 MySQL 教程(菜鸟教程) http://www.runoob.com/mysql/mysql-tutorial.html MySQL 教程(极客 ...

  6. 获取TypeError:__init __()缺少1个需要的位置参数:'on_delete'当试图添加父表后面的子表

    解决办法:https://stackoverflow.com/questions/44026548/getting-typeerror-init-missing-1-required-position ...

  7. 【转】Python metaclass

    转自: http://ju.outofmemory.cn/entry/32434 在回答了 yield关键字和 decorator的问题之后,我更明白了,我决定非常详细地回答这个问题. 读前警告:这个 ...

  8. hyperledge fabric里order-kafka过程分析

    Broadcast主要接收Peer的数据并在Orderer里面生成一系列数据块,主要流程见下图: Broadcast过程分析:Peer(客户端)通过GRPC发起通信,与Orderer连接成功之后,便可 ...

  9. 线程event事件函数实现红绿灯

    #!/usr/bin/env python # -*- coding: utf-8 -*- # author aliex-hrg import threading,time event = threa ...

  10. java线程池ThreadPoolExecutor类使用详解

    在<阿里巴巴java开发手册>中指出了线程资源必须通过线程池提供,不允许在应用中自行显示的创建线程,这样一方面是线程的创建更加规范,可以合理控制开辟线程的数量:另一方面线程的细节管理交给线 ...