因为Spark是基于Hadoop上工作的,所以当我们使用Spark框架时,必须要确保Hadoop能够正常运行:
cd /usr/local/hadoop ./sbin/start-all.sh
有BUG,内容如下:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
解决方法:SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder.
cd /usr/local/hadoop/sbin ./start-all.sh
jps
官方网址:https://www.scala-lang.org/download/2.13.10.html
使用wget命令下载scala:
wget https://downloads.lightbend.com/scala/2.13.10/scala-2.13.10.tgz
sudo tar zxvf ~/下载/scala-2.13.10.tgz -C /usr/local/ # 解压 cd /usr/local sudo mv scala-2.13.10 scala # 重命名
# 1.编辑环境变量 sudo vi ~/.bashrc # 2.使其生效 source ~/.bashrc
scala -version
下载网址:https://archive.apache.org/dist/spark/spark-3.2.2/
使用wget命令进行下载:
wget https://archive.apache.org/dist/spark/spark-3.2.2/spark-3.2.2-bin-hadoop3.2.tgz
# 1.解压 sudo tar zxvf ~/下载/spark-3.2.2-bin-hadoop3.2.tgz -C /usr/local # 2.重命名 cd /usr/local sudo mv spark-3.2.2-bin-hadoop3.2 spark
# 1.编辑环境变量 sudo vi ~/.bashrc # 2.使其生效 source ~/.bashrc
进入到配置目录并打开spark-env.sh文件:
cd /usr/local/spark/conf sudo cp spark-env.sh.template spark-env.sh sudo vi spark-env.sh
添加以下内容:
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 export HADOOP_HOME=/usr/local/hadoop export HADOOP_CONF_DIR=/usr/local/hadoop/etc/hadoop export SCALA_HOME=/usr/local/scala export SPARK_HOME=/usr/local/spark export SPARK_MASTER_IP=192.168.3.134 export SPARK_MASTER_PORT=7077 export SPARK_MASTER_WEBUI_PORT=8099 export SPARK_WORKER_CORES=3 export SPARK_WORKER_INSTANCES=1 export SPARK_WORKER_MEMORY=5G export SPARK_WORKER_WEBUI_PORT=8081 export SPARK_EXECUTOR_CORES=1 export SPARK_EXECUTOR_MEMORY=1G export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HADOOP_HOME/lib/native
cd /usr/local/spark/conf sudo vi workers.template
发现slaves文件里为localhost即本机地址,当前为伪分布式,因此不用修改!但要执行以下:
sudo cp workers.template slaves
启动sbin目录下的start-master.sh以及start-slaves.sh(前提是hadoop已启动):
cd /usr/local/spark sudo ./sbin/start-master.sh sudo ./sbin/start-slaves.sh
报错!!!
通过运行Spark自带的示例,验证Spark是否安装成功:
cd /usr/local/spark ./bin/run-example SparkPi
报错信息如下:
2022-11-01 20:49:24,377 WARN util.Utils: Your hostname, leoatliang-virtual-machine resolves to a loopback address: 127.0.1.1; using 192.168.3.134 instead (on interface ens33)
参考博客:Spark启动:WARN util.Utils: Your hostname, … resolves to a loopback address: …; using … instead
修改配置文件,配置 SPARK_LOCAL_IP 变量即可:
cd /usr/local/spark sudo vim conf/spark-env.sh # 添加以下内容: export SPARK_LOCAL_IP=192.168.3.134 # 自己输出对应的IP
再次测试:
BUG解决!!!
执行时会输出非常多的运行信息,输出结果不容易找到,可以通过 grep 命令进行过滤:
./bin/run-example SparkPi 2>&1 | grep "Pi is"
cd /usr/local/spark ./bin/spark-shell