注:按照自己saprk 安装包版本安装
$tar zxf spark-1.6.1-bin-2.5.0-cdh5.3.6.tgz
-C /opt/modules/
重命名mv spark-env.sh.template spark-env.sh
JAVA_HOME=/opt/modlues/jdk1.7.0_67
SCALA_HOME=/opt/modlues/scala-2.10.4
HADOOP_CONF_DIR=
/opt/modlues/hadoop-2.5.0/etc/hadoop
$sbin/hadoop-daemon.sh start namenode
$sbin/hadoop-daemon.sh start datanode
$sbin/start-master.sh
$start-slaves.sh
命令强制离开
$bin/hadoop dfsadmin -safemode leave
//hdfs path
val rdd = sc.textFile(“/input/wc.txt”)
//这是在内存中处理 flatmap函数 这是匿名函数
按行读取分割split
val wcrdd=rdd.flatMap(line=>line.split(“\t”)).map(word
=>(word,1)).reduceByKey((a,b)=>(a+b))
//保存到HDFS
wcrdd.saveAsTextFile(“wc-spark”)
val wcrdd=sc.textFile(“/input/wc.txt”).flatMap(line
=>line.split(“\t”)).map(word=>(word,1)).reduceByKey((a,b)
=>(a+b)).saveAsTextFile(“wc-spark”)
val rdd = sc.textFile(“/input/wc.txt”)
//分割这个行
val linerdd=rdd.flatMap(line=>line.split(“\t”))
//元组对出现一个 就是一个元组对
val kvrdd=linerdd.map(word=>(word,1))
val wcrdd=kvrdd.reduceByKey((a,b)=>(a+b))
wcrdd.saveAsTextFile(“wc-spark”)
val rdd = sc.textFile(“/input/wc.txt”)
val wcrdd=rdd.flatMap(_.split(“\t”)).map
((_,1)).reduceByKey((_+_))
wcrdd.saveAsTextFile(“wc-spark”)
spark sc:SparkContext(上下文)
Spark context available as sc.
val rdd = sc.textFile(“/input/wc.txt”).filter
(line=>line.contains(“hadoop”))
val wcrdd=rdd.flatMap(_.split(“\t”)).map
((_,1)).reduceByKey((_+_))
wcrdd.foreach(word=>println(word))
//查看函数
wcrdd.first
wcrdd.take(3)
wcrdd.top(3)
wcrdd.collect 查看结果
wcrdd.count
//缓存
wcrdd.cache
wcrdd.count//使缓存生效
分析:(spark,2)=》(2,spark)》sortByKey(false)=》
(2,spark)=》(spark,2)
代码:
val rdd = sc.textFile(“/input/wc.txt”)
val wcrdd=rdd.flatMap(_.split(“\t”)).map
((_,1)).reduceByKey((_+_))
val sortrdd=wcrdd.map(word=>(word._2,word._1)).
sortByKey(false).map(word=>(word._2,word._1))
sortrdd.collect
//top:自带排序
val sortrdd=wcrdd.map(word=>(word._2,word._1)).top(3)
1、sparp-evn
//主节点配置
SPARK_MASTER_IP=li(主机名)
SPARK_MASTER_PORT=7077
SPARK_MASTER_WEBUI_PORT=8080
//从节点配置
SPARK_WORKER_CORES=2
SPARK_WORKER_MEMORY=2g
SPARK_WORKER_PORT=7078
SPARK_WORKER_WEBUI_PORT=8081
SPARK_WORKER_INSTANCES=1
2、slaves
写入hostname名称:自己主机名
3、启动服务
$sbin/start-master.sh
$start-slaves.sh
4、spark-shell常用命令行参数
帮助信息查看:
$bin/spark-shell –help
(1)spark运行在本地模式下:
$bin/spark-shell –master local
或者$bin/spark-shell
//本地
bin/spark-shell \
–class org.apache.spark.examples.SparkPi \
–master local \
–jars lib/spark-examples-1.6.1-hadoop2.5.0-cdh5.3.6.jar \
100
//集群
bin/spark-shell \
–class org.apache.spark.examples.SparkPi \
–master spark://bigdata.beifeng.com:7077 \
–jars lib/spark-examples-1.6.1-hadoop2.5.0-cdh5.3.6.jar \
100
spark-shell和spark-submit
一般执行脚本时使用spark-shell;
一般向集群提交job是使用spark-submit。
spark-shell的本质是spark-submit
spark-shell和spark-submit通知只能使用一个
//本地
bin/spark-submit \
–class org.apache.spark.examples.SparkPi \
–master local \
lib/spark-examples-1.6.1-hadoop2.5.0-cdh5.3.6.jar \
100
//集群
bin/spark-submit \
–class org.apache.spark.examples.SparkPi \
–master spark://bigdata.beifeng.com:7077 \
lib/spark-examples-1.6.1-hadoop2.5.0-cdh5.3.6.jar \
100
//spark开发时模式的使用
一般本地模式适合本地开发测试。集群模式适合生产环境
1、spark-env.sh
SPARK_HISTORY_OPTS=”-Dspark.history.fs.logDirectory
=hdfs://192.168.1.1:8020/user/li/spark-events”
注:spark-events目录需要创建
2、spark-default.conf
spark.eventLog.enabled
true
spark.eventLog.dir
hdfs://192.168.1.1:8020/user/li/spark-events
注:spark.eventLog.dir 目录和
-Dspark.history.fs.logDirectory保持一致
3、启动history服务
$sbin/start-history-server.sh