1.下载上传spark2.0.1-bin-hadoop2.7.0.tgz
2.tar -zxvf spark2.0.1-bin-hadoop2.7.0.tgz
3.重命名文件夹 mv spark-2.0.1-bin-hadoop2.7 spark-2.0.1
4.进入conf文件夹 cd spark-2.0.1/conf
5.cp spark-env.sh.template spark-env.sh
6.修改spar-env.sh 26行 SPARK_LOCAL_IP=10.9.9.141
7.进入bin目录cd ../bin
8.执行sh spark-shell --master=local
单机模式完成!
进入之后可以运行scala语句
[root@hadoop01 bin]# spark-shell --master=local
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel).
18/12/07 09:47:32 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
18/12/07 09:47:33 WARN SparkContext: Use an existing SparkContext, some configuration may not take effect.
Spark context Web UI available at http://10.9.9.141:4040
Spark context available as 'sc' (master = local, app id = local-1544147252910).
Spark session available as 'spark'.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.0.1
/_/
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a1=Array(1,2,3,4)
a1: Array[Int] = Array(1, 2, 3, 4)
scala> a1.max
res0: Int = 4
scala> a1.task(2)
<console>:26: error: value task is not a member of Array[Int]
a1.task(2)
^
scala> a1.take(2)
res2: Array[Int] = Array(1, 2)
scala> val rc=sc.parallelize(a1,2)
rc: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:26
scala> rc.glom.collect
res3: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))
RDD本身是一个特殊的集合类型,有容错机制,分区机制(可以分布式存储,处理)
可以从普通集合中获取RDD:
scala> val l1=List("hello","world","hello","hadoop","spark")
l1: List[String] = List(hello, world, hello, hadoop, spark)
scala> val rl=sc.parallelize(l1,3)
rl: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[2] at parallelize at <console>:26
scala> rl.glom.collect
collect collectAsync
scala> rl.glom.collect
res4: Array[Array[String]] = Array(Array(hello), Array(world, hello), Array(hadoop, spark))
scala> rl.partitions.size
res5: Int = 3
scala> rl.collect
res6: Array[String] = Array(hello, world, hello, hadoop, spark)
scala> val r3=sc.makeRDD(List(1,2,3,4),2)
r3: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[4] at makeRDD at <console>:24
scala> r3.glom.collect
res7: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))
scala>
来源于外部文件:
scala> val data=sc.textFile("file:///home/a.txt")
data: org.apache.spark.rdd.RDD[String] = file:///home/a.txt MapPartitionsRDD[7] at textFile at <console>:24
scala> data.collect
res8: Array[String] = Array(1 apple, 2 huawei, hello world)
scala> val data2=sc.textFile("file:///home/a.txt",2)
data2: org.apache.spark.rdd.RDD[String] = file:///home/a.txt MapPartitionsRDD[9] at textFile at <console>:24
scala> data2.glom.collect
res9: Array[Array[String]] = Array(Array(1 apple, 2 huawei), Array(hello world))
scala> data.glom.collect
res10: Array[Array[String]] = Array(Array(1 apple, 2 huawei, hello world))
scala>
scala> val result=data.flatMap{_.split("\t")}.groupBy{word=>word}.mapValues{_.size}
result: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[15] at mapValues at <console>:26
scala> result.collect
res11: Array[(String, Int)] = Array((2,1), (hello,1), (apple,1), (huawei,1), (1,1), (world,1))
scala> val result2=data2.flatMap{_.split("\t")}.groupBy{word=>word}.mapValues{_.size}
result2: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[19] at mapValues at <console>:26
scala> result2.collect
res12: Array[(String, Int)] = Array((2,1), (hello,1), (apple,1), (world,1), (huawei,1), (1,1))
scala> result2.glom.collect
res13: Array[Array[(String, Int)]] = Array(Array((2,1), (hello,1), (apple,1), (world,1)), Array((huawei,1), (1,1)))
scala> result.glom.collect
res14: Array[Array[(String, Int)]] = Array(Array((2,1), (hello,1), (apple,1), (huawei,1), (1,1), (world,1)))
scala>
来源于hdfs:
scala> val data3=sc.textFile("hdfs://hadoop01:9000/a.txt",3)
data3: org.apache.spark.rdd.RDD[String] = hdfs://hadoop01:9000/a.txt MapPartitionsRDD[23] at textFile at <console>:24
scala> data3.glom.collect
res18: Array[Array[String]] = Array(Array(1 apple, 2 huawei), Array(hello world), Array(), Array())
scala>
RDD操作:transformations(变换)---懒操作 actions(执行)
Spark集群部署
1.将spark分发到其它服务器中
scp -r /home/software/spark-2.1.0 root@hadoop02:/home/software
scp -r /home/software/spark-2.1.0 root@hadoop03:/home/software
2.将每个服务器中spark-2.1.0中conf下的spark-env.sh的文件进行修改
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This file is sourced when running various Spark programs.
# Copy it as spark-env.sh and edit that to configure Spark for your site.
# Options read when launching programs locally with
# ./bin/run-example or ./bin/spark-submit
# - HADOOP_CONF_DIR, to point Spark towards Hadoop configuration files
SPARK_LOCAL_IP=10.9.9.141
# - SPARK_PUBLIC_DNS, to set the public dns name of the driver program
# - SPARK_CLASSPATH, default classpath entries to append
# Options read by executors and drivers running inside the cluster
# - SPARK_LOCAL_IP, to set the IP address Spark binds to on this node
# - SPARK_PUBLIC_DNS, to set the public DNS name of the driver program
# - SPARK_CLASSPATH, default classpath entries to append
SPARK_LOCAL_DIRS=/home/software/spark-2.1.0/tmp
export JAVA_HOME=/home/software/jdk1.8
# - MESOS_NATIVE_JAVA_LIBRARY, to point to your libmesos.so if you use Mesos
# Options read in YARN client mode
# - HADOOP_CONF_DIR, to point Spark towards Hadoop configuration files
# - SPARK_EXECUTOR_INSTANCES, Number of executors to start (Default: 2)
# - SPARK_EXECUTOR_CORES, Number of cores for the executors (Default: 1).
# - SPARK_EXECUTOR_MEMORY, Memory per Executor (e.g. 1000M, 2G) (Default: 1G)
# - SPARK_DRIVER_MEMORY, Memory for Driver (e.g. 1000M, 2G) (Default: 1G)
# Options for the daemons used in the standalone deploy mode
# - SPARK_MASTER_HOST, to bind the master to a different IP address or hostname
# - SPARK_MASTER_PORT / SPARK_MASTER_WEBUI_PORT, to use non-default ports for the master
# - SPARK_MASTER_OPTS, to set config properties only for the master (e.g. "-Dx=y")
# - SPARK_WORKER_CORES, to set the number of cores to use on this machine
# - SPARK_WORKER_MEMORY, to set how much total memory workers have to give executors (e.g. 1000m, 2g)
# - SPARK_WORKER_PORT / SPARK_WORKER_WEBUI_PORT, to use non-default ports for the worker
# - SPARK_WORKER_INSTANCES, to set the number of worker processes per node
# - SPARK_WORKER_DIR, to set the working directory of worker processes
# - SPARK_WORKER_OPTS, to set config properties only for the worker (e.g. "-Dx=y")
# - SPARK_DAEMON_MEMORY, to allocate to the master, worker and history server themselves (default: 1g).
# - SPARK_HISTORY_OPTS, to set config properties only for the history server (e.g. "-Dx=y")
# - SPARK_SHUFFLE_OPTS, to set config properties only for the external shuffle service (e.g. "-Dx=y")
# - SPARK_DAEMON_JAVA_OPTS, to set config properties for all daemons (e.g. "-Dx=y")
# - SPARK_PUBLIC_DNS, to set the public dns name of the master or workers
# Generic options for the daemons used in the standalone deploy mode
# - SPARK_CONF_DIR Alternate conf dir. (Default: ${SPARK_HOME}/conf)
# - SPARK_LOG_DIR Where log files are stored. (Default: ${SPARK_HOME}/logs)
# - SPARK_PID_DIR Where the pid file is stored. (Default: /tmp)
# - SPARK_IDENT_STRING A string representing this instance of spark. (Default: $USER)
# - SPARK_NICENESS The scheduling priority for daemons. (Default: 0)
SPARK_LOCAL_IP=10.9.9.141(改为每个服务器自己的ip)
SPARK_LOCAL_DIRS=/home/software/spark-2.1.0/tmp
export JAVA_HOME=/home/software/jdk1.8
3.将每个服务器中spark-2.1.0中conf下的slaves的文件进行修改 修改为如下:
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# A Spark Worker will be started on each of the machines listed below.
hadoop01
hadoop02
hadoop03
这样就算配置完成了