Spark版本 : spark-2.4.7-bin-hadoop2.7
官网下载http://spark.apache.org/
然后上传到LInux服务器上
tar -zxvf spark-2.4.7-bin-hadoop2.7.tgz
bin 可执行脚本
conf 配置文件
data 示例程序使用数据
examples 示例程序
jars 依赖 jar 包
python pythonAPI
R R 语言 API
sbin 集群管理命令
yarn 整合yarn需要的文件
./sbin/start-master.sh
例如:start-slave.sh spark://Ice:7077
参数:表示master节点的访问地址(注意host-ip映射)
spark-shell --master spark://Ice:7077 (Spark Scala交互式开发环境,“:quit”退出)
bin/spark-submit --class org.apache.spark.examples.SparkPi --master local[2] ./examples/jars/spark-examples_2.11-2.4.7.jar 100
结果片段:Pi is roughly 3.1416503141650316
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
的核数Spark-shell
Spark-shell
是 Spark
给我们提供的交互式命令窗口(类似于 Scala 的 REPL)
$ bin/spark-shell
Spark context Web UI available at http://Ice:4040
Spark context available as 'sc' (master = local[*], app id = local-1608775323264).
Spark session available as 'spark'.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.4.7
/_/
Using Scala version 2.11.12 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_251)
Type in expressions to have them evaluated.
Type :help for more information.
scala>
wordcount
案例mkdir input
cd input
touch 1.txt
touch 2.txt
bin/spark-shell
可以看到一句Spark context Web UI available at http://Ice:4040
wordcount
程序sc.textFile("./input").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).collect
scala> sc.textFile("/root/sparkdemo/demo1/input").flatMap(.split(" ")).map((, 1)).reduceByKey(_ + _).collect res0: Array[(String, Int)] = Array((are,1), (SQL,1), (world,1), (you,1), (hadoop,1), (how,1), (MapReduce,1), (hello,2), (spark,4), (hi,1), (core,1)) scala>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fCXxgJmX-1609287306339)(image/image2.png)]
textFile("input")
:读取本地文件input文件夹数据;flatMap(_.split(" "))
:压平操作,按照空格分割符将一行数据映射成一个个单词;map((_,1))
:对每一个元素操作,将单词映射为元组;reduceByKey(_+_)
:按照key将值进行聚合,相加;collect
:将数据收集到Driver端展示。