jdk版本:1.8.0_241
scala版本:2.13.1
hadoop版本:2.7.6
IntelliJ IDEA版本:2019.3.4(community Edition)
使用IDEA作为开发环境(默认已安装),该软件中自带maven,无需自己下载,将IDEA中的maven路径添加到环境变量即可
IDEA中的maven路径: {IDEA根路径}/Contents/plugins/maven/lib/maven3/bin
例如我的默认maven安装目录: /Applications/IntelliJ\ IDEA\ CE.app/Contents/plugins/maven/lib/maven3/bin/
在 ~/.bash_profile配置
MAVEN_HOME=/Applications/IntelliJ\ IDEA\ CE.app/Contents/plugins/maven/lib/maven3/
PATH=$MAVEN_HOME/bin:$PATH
这里稍微贴下 setting.xml,setting.xml具体的配置还要根据个体的研发环境来定。
输入如下命令:mvn help:system,在用户目录下出现.m2文件夹,其中 settings.xml是我个人的 maven安装目录下conf(/Applications/IntelliJ\ IDEA\ CE.app/Contents/plugins/maven/lib/maven3/conf/)中拷贝过来的,如下所示:
在settings.xml中并修改本地仓库路径,本地仓库路径与实际使用的repository目录一致,例如 我的IDEA默认使用${user.home}/.m2/repository (见上图),故 settings.xml中localReposity为用户创建的目录${user.home}/.m2/repository;
<proxy>
<id>dev</id>
<active>true</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>127.0.0.1</host>
<port>12639</port>
<nonProxyHosts></nonProxyHosts>
</proxy>
<mirror>
<id>oa_releases</id>
<mirrorOf>oa_releases</mirrorOf>
<name>oa_releases</name>
<url>http://maven.oa.com/nexus/content/groups/public/</url>
</mirror>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>oa_releases</id>
<name>internal repository</name>
<url>http://maven.oa.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
配置好settings.xml后修改Intellij IDEA的maven配置,repository & settings.xml的目录需要与 IDEA Preference->Build...->Maven的配置目录一致,如下图:
添加镜像仓库,更新setting.xml中的servers配置
maven 仓库适用于存储第三方依赖的地方,对应于pom.xml <dependency>标签
maven编译时,首先查找本地仓库(${user.home}/.m2/repository),若本地仓库没有对应的依赖库,会从外部的远程仓库下载,同时缓存在本地仓库中;
为了提高下载速度,会在两个resposity之间配置镜像仓库,在maven配置文件(setting.xml)里配置了<mirrors><mirror>..........</mirror></mirrors>,即定义了两个Repository之间的镜像关系。
注意先创建scala project再转换为maven project的方式,因为package name会包含main;创建的时候先选择maven或java project,通过“Add Framework Support…”再引入Scala SDK或maven的方式比较好,最终效果和图中给出的差不多,但是目录结构会有差异。
在~/.bash_profile中添加如下指令:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
export SCALA_HOME=/Users/<你的名字>/tools/scala-2.12.6
export PATH=$JAVA_HOME/bin:$SCALA_HOME/bin:$PATH
jdk(hadoop与java版本兼容性参考https://cwiki.apache.org/confluence/display/hadoop/HadoopJavaVersions)、ssh;
执行ssh localhost若提示:
localhost: ssh: connect to host localhost port 22: Connection refused
则需要:
/usr/bin/ruby -e "$(cat ~/Desktop/install)"
启动方式:
sudo /usr/sbin/sshd
若启动失败并提示:
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
则,执行以下操作后重新启动:
sudo cp ~/.ssh/id_rsa /etc/ssh/ssh_host_rsa_key
sudo cp ~/.ssh/id_rsa.pub /etc/ssh/ssh_host_rsa_key.pub
启动后通过ps -ef | grep ssh检查ssh服务是否存在
~/.ssh/id_rsa.pub
~/.ssh/id_rsa
~/.ssh/authorized_keys
若没有id_rsa.*文件,则执行以下操作:
$ ssh-keygen -t rsa
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/authorized_keys
添加本机ip到localhost的映射(sudo vim /etc/hosts),如下所示
10.72.xx.xx localhost
这一步是为了可以使用形如:hdfs://10.72.xx.xx:9000/file/path的格式访问hdfs,否则只能使用localhost。
下载hadoop-2.7.6安装包,解压到~/tools/hadoop-2.7.6;
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
以上为hdfs相关配置,若要使用yarn,还需进行以下配置
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>
注:默认存储路径为/tmp/hadoop-/,可以通过配置 “dfs.namenode.name.dir”、 “dfs.datanode.data.dir” 显式指定,参考文档https://hadoop.apache.org/docs/r2.7.6/hadoop-project-dist/hadoop-common/ClusterSetup.html#Configuring_the_Hadoop_Daemons;
./bin/hdfs namenode -format
./sbin/start-dfs.sh
./sbin/mr-jobhistory-daemon.sh start historyserver
./sbin/start-yarn.sh
启动后通过jps命令查看启动的服务,应包含:DataNode、NameNode、SecondaryNameNode(若启动了yarn,还包括:NodeManager、ResourceManager;若启动了历史服务,还包括:JobHistoryServer);
a.历史任务:http://localhost:19888/
b.集群状态:http://localhost:8088/
c.NameNode状态:http://localhost:50070/
通过以下命令测试hdfs是否可用:
./bin/hadoop fs -ls /
./bin/hadoop fs -ls hdfs://localhost:9000/
./bin/hadoop fs -ls hdfs://10.72.xxx.xxx:9000/
./bin/hdfs dfs -ls /
./bin/hdfs dfs -ls hdfs://localhost:9000/
./bin/hdfs dfs -ls hdfs://10.72.xxx.xxx:9000/
若服务可用,需要创建以下目录:
hdfs://localhost:9000/user/<username>
./sbin/stop-dfs.sh
./sbin/stop-yarn.sh
./sbin/mr-jobhistory-daemon.sh stop historyserver
注:若配置了yarn,则官网(https://hadoop.apache.org/docs/r2.7.6/hadoop-project-dist/hadoop-common/SingleCluster.html#Execution)中下面的例子是无法成功执行的;若只配置了hdfs,是没有问题的:
bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.6.jar grep input output 'dfs[a-z.]+'
若使用spark对本地文件进行测试学习,可以不用安装上面的hadoop环境,若要结合hdfs使用spark,则可以参考上面的步骤搭建hadoop。
下载安装包https://spark.apache.org/downloads.html 并解压(如:~/tools/spark-2.3.1-bin-hadoop2.7);
./sbin/start-master.sh
./sbin/start-slave.sh <spark://xxxx-xxx:7077>
下面开发一个超级简单的rdd任务,逻辑(统计hdfs文件中包含单词form的行及行数,并将结果保存到hdfs)参考官网:https://spark.apache.org/docs/latest/quick-start.html#basics。
package com.tencent.omg
import org.apache.spark.{SparkConf,SparkContext}
object MvnRddTest {
def main(args: Array[String]): Unit = {
val dataPath = "hdfs://localhost:9000/usr/test/LICENSE.txt"
val resultPath = "hdfs://localhost:9000/usr/test/result"
val sc = new SparkContext(new SparkConf().setAppName("Mvn-Rdd-Test"))
try{
val accm = sc.longAccumulator("LineAccumulator")
val rdd = sc.textFile(dataPath,2)
val sparkDs = rdd.filter(
line => if(line.contains("form")){
accm.add(1)
true
} else false
)
sparkDs.saveAsTextFile(resultPath)
println(s"Lines that contains 'form' number is: ${accm.value}")
}catch {
case e:Exception => e.printStackTrace()
}finally {
sc.stop()
}
}
}
注:运行该示例需要上传一份文件到(二)的hdfs中,例中的LICENSE.txt来自hadoop安装包。
<groupId>com.tg.omg</groupId>
<artifactId>mvn-rdd-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- spark core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.3.1</version>
</dependency>
<!-- hdfs tool -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-assembly</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- build java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.tencent.omg.MvnRddTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- build scala -->
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
双击Maven Projects–>mvn-rdd-test–>Lifecycle–>package即可完成打包,“mvn-rdd-test-1.0-SNAPSHOT-jar-with-dependencies.jar”即为包含依赖项的包。
注:pom中引入的这两个build插件是必须的,分别用于build java和scala。
./bin/spark-submit --master spark://xxxx-xxx:7077 --class com.tencent.omg.MvnRddTest ~/Document/IdeaProjects/mvn-rdd-test/target/mvn-rdd-test-1.0-SNAPSHOT-jar-with-dependencies.jar
可以到hdfs上查看运行结果,终端会打印计数器的值。
注意:使用maven打包,不要使用Build Artifacts方式打包。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。