Local 模式就是指的只在一台计算机上来运行 Spark. 通常用于测试的目的来使用 Local 模式, 实际的生产环境中不会使用 Local 模式.
/opt/software/
/opt/module/
[bigdata@hadoop002 software]$ tar -zxvf spark-2.1.1-bin-hadoop2.7.tgz -C /opt/module
[bigdata@hadoop002 module]$ cp -r spark-2.1.1-bin-hadoop2.7 spark-local
如果有权限问题,可以修改为root,方便学习时操作,实际中使用运维分配的用户和权限即可。
chown -R root /opt/module/spark-local
chgrp -R root /opt/module/spark-local
bin 可执行脚本
conf 配置文件
data 示例程序使用数据
examples 示例程序
jars 依赖 jar 包
python pythonAPI
R R 语言 API
sbin 集群管理命令
yarn 整合yarn需要的文件
[bigdata@hadoop002 spark-local]$ bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master local[2] \
./examples/jars/spark-examples_2.11-2.1.1.jar 100
注意:
如果你的shell
是使用的zsh
, 则需要把local[2]
加上引号:'local[2]'
说明:
使用spark-submit
来发布应用程序.
spark-submit
语法./bin/spark-submit \
--class <main-class> \
--master <master-url> \
--deploy-mode <deploy-mode> \
--conf <key>=<value> \
... # other options
<application-jar> \
[application-arguments]
master
指定 master
的地址,默认为local
. 表示在本机运行.class
你的应用的启动类 (如 org.apache.spark.examples.SparkPi
)deploy-mode
是否发布你的驱动到 worker
节点(cluster
模式) 或者作为一个本地客户端 (client
模式) (default: client
)conf
: 任意的 Spark 配置属性, 格式key=value
. 如果值包含空格,可以加引号"key=value"
application-jar
: 打包好的应用 jar
,包含依赖. 这个 URL
在集群中全局可见。 比如hdfs:// 共享存储系统
,
如果是 file:// path
, 那么所有的节点的path
都包含同样的jar
application-arguments
: 传给main()
方法的参数executor-memory 1G
指定每个executor
可用内存为1G
total-executor-cores 6
指定所有executor
使用的cpu
核数为6个executor-cores
表示每个executor
使用的 cpu
的核数Master URL
的说明Master URL | Meaning |
---|---|
local | Run Spark locally with one worker thread (i.e. no parallelism at all). |
local[K] | Run Spark locally with K worker threads (ideally, set this to the number of cores on your machine). |
local[*] | Run Spark locally with as many worker threads as logical cores on your machine. |
spark://HOST:PORT | Connect to the given Spark standalone cluster master. The port must be whichever one your master is configured to use, which is 7077 by default. |
mesos://HOST:PORT | Connect to the given Mesos cluster. The port must be whichever one your is configured to use, which is 5050 by default. Or, for a Mesos cluster using ZooKeeper, use mesos://zk://… To submit with --deploy-mode cluster, the HOST:PORT should be configured to connect to the MesosClusterDispatcher. |
yarn | Connect to a YARNcluster in client or cluster mode depending on the value of --deploy-mode. The cluster location will be found based on the HADOOP_CONF_DIR or YARN_CONF_DIR variable. |
也可以使用run-examples来运行
[bigdata@hadoop002 spark-local]$bin/run-example SparkPi 100
Spark-shell
Spark-shell
是 Spark
给我们提供的交互式命令窗口(类似于 Scala 的 REPL)
本案例在 Spark-shell
中使用 Spark
来统计文件中各个单词的数量.
mkdir input
cd input
touch 1.txt
touch 2.txt
[bigdata@hadoop002 spark-local]$ bin/spark-shell
wordcount
程序scala> sc.textFile("./input").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).collect
textFile("input")
:读取本地文件input文件夹数据;flatMap(_.split(" "))
:压平操作,按照空格分割符将一行数据映射成一个个单词;map((_,1))
:对每一个元素操作,将单词映射为元组;reduceByKey(_+_)
:按照key将值进行聚合,相加;collect
:将数据收集到Driver端展示。本次的分享就到这里了