Loading

基于云服务器和idea的大数据hadoop的开发环境搭建

基于云服务器和idea的大数据hadoop的开发环境搭建

工具版本

windows10/win11
Ubuntu 16.04.7 LTS
idea 2021.1.3--破解教程
hadoop 2.10.1
maven 3.6.0
jdk

  • windows:jdk18.0.1.1
  • Ubuntu: jdk18.0.1.1
  • idea: jdk1.8

1.云服务器的hadoop环境配置

参考教程

  1. 安装java

    • 按照第一个来就行
  2. Hadoop用户设置

    • 注意
      • 如果在Root下安装的java和hadoop并配置的环境变量,在hadoop用户下也要配置一遍环境变量
      • 记得特意记下自己的hadoop的密码
      • ssh的登陆密码是root的密码
  3. 安装SSH

  4. 安装hadoop

    • 按照第一个来就行
  5. HDFS伪分布式配置

    • 按照第一个来,可以参考下第二个的原理说明
    • 注意jdk版本更换为自己的就行 并且core-site.xml中的localhost改为服务器的内网IP

2. win10本地的配置

1. 安装java

2. 安装hadoop

3. 安装maven

  • 可以先看下Maven的原理教程:Maven基础
  • 下载安装包,配置环境变量即可,推荐低一点的版本
  • 修改配置文件:主要是配置镜像还有本地仓库配置
    镜像配置:
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
<mirror>
    <id>repo2</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
    <id>ibiblio</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
    <id>jboss-public-repository-group</id>
    <mirrorOf>central</mirrorOf>
    <name>JBoss Public Repository Group</name>
    <url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
    <id>google-maven-central</id>
    <name>Google Maven Central</name>
    <url>https://maven-central.storage.googleapis.com
    </url>
    <mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
    <id>maven.net.cn</id>
    <name>oneof the central mirrors in china</name>
    <url>http://maven.net.cn/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

本地仓库配置:

  • 将idea的maven配置为自己安装的:一定要从File->New Project Ssttings进行配置,不要直接从Settings进行配置,注意一定要点击Apply确定,才会设置成功(有点坑人)

  • 各环境的配置结果如下

3. IDEA连接Linux上的Hadoop

参考:IDEA连接Linux上的Hadoop并对HDFS进行操作

注意

  • 一定要添加服务器的安全组规则,让9000端口放行
  • 如果是HADOOP_HOME报错,那就是环境变量的问题,注意重启win
  • 还有一种报错可能是因为你没有再hdfs中创建文件夹所以无法连接

4.测试:WordCount

参考:IDEA连接Linux上的Hadoop并对HDFS进行操作
注意根据自己的hadoop版本做一些变化

服务器访问应该多加的步骤

参考:本地用HDFS的javaAPI访问云服务器Hadoop过程及问题(总结)_hdfs client访问云端

  • 因为各节点的访问在伪分布的配置中默认用的是内网ip地址,所以我们访问就会报错,需要是各节点允许通过域名访问

1.代码结构

2.源码

  1. WordCount.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.log4j.BasicConfigurator;

import java.io.IOException;
import java.util.StringTokenizer;


public class WordCount {
    public static class Map extends Mapper<Object,Text,Text,IntWritable>{
        private static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            StringTokenizer st = new StringTokenizer(value.toString());
            while(st.hasMoreTokens()){
                word.set(st.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        private static IntWritable result = new IntWritable();
        public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{
            int sum = 0;
            for(IntWritable val:values){
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    static {
        try {
            // 填入自己的文件的路径
            System.load("D:\\Environment\\hadoop\\hadoop-2.10.1\\hadoop-2.10.1\\bin\\hadoop.dll");//建议采用绝对地址,bin目录下的hadoop.dll文件路径
        } catch (UnsatisfiedLinkError e) {
            System.err.println("Native code library failed to load.\n" + e);
            System.exit(1);
        }
    }

    public static void main(String[] args) throws Exception{
        BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境。

        // 填入自己的环境变量中配置的value值
        System.setProperty("HADOOP_USER_NAME", "hadoop");

        Configuration conf = new Configuration();
        // 设置客户端访问datanode使用hostname来进行访问
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 设置core-site.xml中的属性fs.defaultFS和属性值,注意主机名必须和设置的hosts主机名一致
        //conf.set("fs.defaultFS","hdfs://39.105.30.159:9000");

        String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
        if(otherArgs.length != 2){
            System.err.println("Usage WordCount <int> <out>");
            System.exit(2);
        }
        Job job = new Job(conf,"word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

2.core-site.xml

<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed 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. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
        <property>
             <name>hadoop.tmp.dir</name>
             <value>file:/usr/local/hadoop/tmp</value>
             <description>Abase for other temporary directories.</description>
        </property>
        <property>
             <name>fs.defaultFS</name>
             <value>hdfs://hadooptest:9000</value>
        </property>
</configuration>
  1. hdfs-site.xml
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed 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. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
        <property>
             <name>dfs.replication</name>
             <value>1</value>
        </property>
        <property>
             <name>dfs.namenode.name.dir</name>
             <value>file:/usr/local/hadoop/tmp/dfs/name</value>
        </property>
        <property>
             <name>dfs.datanode.data.dir</name>
             <value>file:/usr/local/hadoop/tmp/dfs/data</value>
        </property>
        <!--解决 问题-->
        <property>
            <name>dfs.client.use.datanode.hostname</name>
            <value>true</value>
            <description>only cofig in clients</description>
        </property>

</configuration>
  1. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>WordCount_2_4-13</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.10.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--后来添加的-->
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.10.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-common</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!-- <dependency>
             <groupId>com.github.sakserv</groupId>
             <artifactId>hadoop-mini-clusters</artifactId>
             <version>0.0.13</version>
         </dependency>-->

        <!--解决关于slf4j的mutiple bindings问题 -->
        <!--<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>-->

    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.hhrz.mapreduce.demo.JobMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3.可能会遇到的问题

  1. maven相关
    • 无法注入依赖:可能是idea版本和maven的匹配问题,尝试降低maven版本

    • 无法产生src等文件目录:查看自己的maven配置

    • 一直找不到包:查看自己要导的包是否导入,如果没有导入那就是pom.xml的依赖有为题,注意查清楚自己要导入的类在哪个包中,以及对应版本

    • Plugins报错

      • 在本地仓库找到后删除,再生成
  2. 连接结点的问题
WARN org.apache.hadoop.hdfs.client.impl.BlockReaderFactory - I/O error constructing remote block reader
java.net.ConnectException: Connection timed out: no further information
  • 这就是因为没有在服务器开放NameNode节点对应端口造成的问题,需要在服务器控制台,添加50070端口的安全组规则

5. 总结和反思

花费较大精力的地方

  • HDFS连接:win环境变量的问题
  • maven: 与IDEA版本不太匹配,最开始一直没有出现正常的项目结构,配置maven的时候比较粗心,没有apply获知没有从New Project Settings进行设置
  • 测试:内网ip的问题;节点端口的开放问题;还是对于hadoop的原理理解的不够清晰
  1. 不只是一味的搜索,而放弃了思考
  2. 对bug保持好奇,平静地探索

6. 参考文章

基于Linux的Hadoop伪分布式安装_linux安装hadoop伪分布
在阿里云中搭建大数据实验环境_厦大数据库实验室博客
IDEA连接Linux上的Hadoop并对HDFS进行操作
win10不重启使环境变量修改生效
IDEA 无限重置 30 天试用期图文教程(永久激活,亲测有用) - 异常教程
本地用HDFS的javaAPI访问云服务器Hadoop过程及问题(总结)
通过类名查找Maven依赖_根据类名查jar artifactid
依赖管理 - 廖雪峰的官方网站

posted @ 2023-04-15 16:48  hiaGeng  阅读(133)  评论(0)    收藏  举报