Hadoop_1 入门WordCount

2023-05-16

记录踩得坑以及部署环境流程。

搭建的是伪分布Hadoop

  1. 首先环境需要安装zookeeper。这个好装,不多说
  2. 其次比较复杂的是安装openssh。我的Linux系统是centos 7 mini版本。安装openssh之前的准备工作有很多。
    需要安装的tar包有:
    • libpcap-1.8.1.tar.gz
    • zlib-1.2.8.tar.gz
    • perl-5.22.4.tar.gz
    • openssl-1.0.2j.tar.gz
    • openssh-7.2p2.tar.gz
      顺序是先perl,再zlib。之后就随意了。因为zlib中会依赖perl5
      安装openssh主要目的是设置免密登录。方便hadoop搭建
  3. 安装hadoop。
    需要配置Java环境变量,以及Hadoop的环境变量。Java_HOME有时候加载不到的问题可以百度,把hadoop-evn.cmd配置文件大约第25行改掉就可以了。
    主要注意的是core-site.xml,hdfs-site.xml,yarn-site.xml这三个配置文件的配置
    core-site.xml
<configuration>
	 <property>
	    <name>fs.defaultFS</name>
	    <value>hdfs://xxxx:9000/</value>
	  </property>
	  <property>
	    <name>hadoop.tmp.dir</name>
    <value>/home/u/hadoop-2.7.6/tmp</value>
	  </property>
</configuration>

hdfs-site.xml

<configuration>
  <property>
   <name>dfs.namenode.http-address</name>
   <value>xxxxx:50070</value>
  </property>
  <property>
    <name>dfs.namenode.secondary.http-address</name>
    <value>xxxxxxx:50090</value>
  </property>
  <!-- 指定HDFS副本数量 -->
  <property>
   <name>dfs.replication</name>
   <value>3</value>
  </property>
  <!--指定NameNode的存储路径-->
  <property>
   <name>dfs.namenode.name.dir</name>
   <value>/home/u/hadoop-2.7.6/namenode</value>
  </property>
  <!--指定DataNode的存储路径-->
  <property>
   <name>dfs.datanode.data.dir</name>
   <value>/home/u/hadoop-2.7.6/datanode</value>
  </property>
</configuration>

yarn-site.xml

<configuration>

<!-- Site specific YARN configuration properties -->
   <property>
    <name>yarn.resourcemanager.hostname</name>
    <value>xxx.xxx.x.xxx</value>
   </property>
   <property>
    <name>yarn.nodemanager.aux-services</name>
    <value>mapreduce_shuffle</value>
   </property>
<property>
     <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
     <value>org.apache.hadoop.mapred.ShuffleHandler</value>
</property>
</configuration>

然后到hadoop-2.7.6/sbin/目录下面执行start-all.sh就可以一次性启动所有角色了。2.x启动成功后是这样的:
jps

运行第一个WordCount程序

我看的书是Hadoop实战。里面的程序有点老,导致自己写的WordCount有很多方法ClassNotFound。但是hadoop是自带第一个wordcount程序的。可以看到它的源码。现在先使用它自带的wordcount来测试下这个环境是否可行

  1. 生成输入文件
    echo "I love Java I love Hadoop I love BigData Good Good Study, Day Day Up" > wc.txt
  2. 在Hdfs上创建文件夹,把wc.txt上传到Hdfs
    hdfs dfs -mkdir -p /input/wordcount hdfs dfs -put wc.txt /input/wordcount
  3. 然后就可以执行了。输出文件目录是不存在的
    hadoop jar /home/u/hadoop-2.7.6/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.6.jar wordcount /input/wordcount /output/wordcount

结果:
这里写图片描述

本地WordCount代码

package cn.edu.ruc.cloudcomputing.book.chapter03;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
 
public class WordCount {
 
    //step 1 Mapper Class
    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
 
        private Text mapOutPutKey = new Text();
        private final static IntWritable mapOutPutValue = new IntWritable(1);
        @Override
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            //get lines value
            String lineValue = value.toString();
            String[] strs = lineValue.split(" ");
            for(String str : strs){
                mapOutPutKey.set(str);
                context.write(mapOutPutKey, mapOutPutValue);
            }
        }
    }
     
    //step 2 Reducer Class
    public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
 
        private IntWritable outPutVlaue = new IntWritable();
        @Override
        public void reduce(Text key, Iterable<IntWritable> values,Context context)
                throws IOException, InterruptedException {
 
            //temp : sum
            int sum = 0;
            for(IntWritable value : values){
                sum += value.get();
            }
            outPutVlaue.set(sum);
            context.write(key, outPutVlaue);
        }
    }
 
    //step 3 Driver
    public int run(String[] args) throws Exception, InterruptedException{
         
        //get configuration
        Configuration configuration = new Configuration();
        //get a job
        Job job = Job.getInstance(configuration,this.getClass().getName());
        job.setJarByClass(getClass());
        //get a input path
        Path inPath = new Path(args[0]);
        FileInputFormat.addInputPath(job, inPath);
        //get a output path
        Path outPath = new Path(args[1]);
        FileOutputFormat.setOutputPath(job, outPath);
         
        //Mapper
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
         
        //Reducer
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
         
        //submit job
        boolean isSUccess = job.waitForCompletion(true);
         
        return isSUccess ? 0 : 1;
    }
     
    public static void main(String[] args) throws Exception {
         
        args = new String[]{
            "hdfs://xxxx:9000/input/",
            "hdfs://xxxx:9000/output"
        };
        int status = new WordCount().run(args);
         
        System.exit(status);
    }
}

主要注意端口的配置。eclipse插件端口的配置

在eclipse中打成可执行jar包

环境上运行语句:

hadoop jar wordcount.jar cn.edu.ruc.cloudcomputing.book.chapter03.WordCount
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hadoop_1 入门WordCount 的相关文章

随机推荐

  • FreeRTOS源码解析——第一章 整体架构

    FreeRTOS源码解析 第一章 FreeRTOS 整体架构 第二章 FreeRTOS 编程规范 第三章 FreeRTOS 内存管理 第四章 FreeRTOS 任务管理 第五章 FreeRTOS 消息队列 第六章 FreeRTOS 软件定时
  • 普通 div 模拟 placeholder

    这是由于我们项目中使用的 quasar 组件里的 editor xff08 实际是 div 元素 xff09 的占位符问题引起的探讨 首先说明一下 xff0c 非表单元素如 div 可以通过加一个 contenteditable 为 tru
  • FreeRTOS源码解析——第二章 编程规范

    FreeRTOS源码解析 第一章 FreeRTOS 整体架构 第二章 FreeRTOS 编程规范 第三章 FreeRTOS 内存管理 第三章 FreeRTOS 内存管理 第四章 FreeRTOS 任务管理 第五章 FreeRTOS 消息队列
  • FreeRTOS源码解析——第三章 内存管理

    FreeRTOS源码解析 第一章 FreeRTOS 整体架构 第二章 FreeRTOS 编程规范 第三章 FreeRTOS 内存管理 第四章 FreeRTOS 任务管理 第五章 FreeRTOS 消息队列 第六章 FreeRTOS 软件定时
  • sonic编译过程

    文章目录 1 0虚拟机选择2 0虚拟机安装3 0 系统配置4 0 安装docker5 0 安装sonic 1 0虚拟机选择 原则上所有的系统都可以编译sonic xff0c 此处我们选择centos 7 7 xff0c 虚拟机的内存空间建议
  • Sonic测试架构介绍

    Sonic项目简介 Sonic Software for Open Networking in the CloudSonic是基于Linux的开源网络操作系统 xff0c 可以跑在多个不同芯片厂商交换机上Sonic在2016年OCP峰会上
  • Sonic_cli常用命令

    用户名 xff1a admin 密码 xff1a YourPaSsWoRd 一 change password admin 64 sonic passwd Changing password for admin current UNIX p
  • SONIC config_db.json文件的前生今世

    config db json的使用 系统启动时从config db json中读取数据并写入CONFIG DB数据库 xff0c 前提是config db json存在 xff1b 保存当前系统的一些配置信息 xff0c 通过config
  • SONiC架构DOCKER组件交互分析

    BGP组件交互分析 内核中的bgp socket收到BGP更新报文 xff0c 然后被上送到bgpd进程bgpd处理该报文 xff0c 并通知zebra进程新增前缀和关联下一跳zebra确定该目的可达后 xff0c 生成一个路由网络链接信息
  • sonic处理netlink事件

    sonic处理netlink事件 sonic在处理路由 xff0c 接口up down 接口地址变化 xff0c team等事件上极大的依赖内核 sonic通过监听rtnl事件来响应linux事件 从而感知相关信息变化 libnl soni
  • sonic配置team与实现机制

    sonic实现team代码框架图 xff1a sonic修改lag模式配置步骤 1 修改文件teamd j2 docker exec it teamd bash cd usr share sonic templates vim teamd
  • asyncComputed 异步计算属性

    asyncComputed 异步计算属性 我们项目中最近使用了异步计算属性 xff0c 个人感觉很好用 xff0c 特此推荐给大家 一 案例 假设这样一个场景 xff1a 一个列表数据 data 是通过接口返回 xff0c 请求参数中有一个
  • sonic管理口信息处理流程

    sonic管理口信息处理流程 管理接口信息配置文件格式 管理信息使用MGMT INTERFACE 表进行配置 对象的key由管理接口名字和IP前缀使用 连接而成 属性 gwaddr用于执行默认路由指向管理口 xff0c 其值为默认网关 属性
  • SONIC VLAN配置流程

    SONIC VLAN配置流程 sonic vlan配置通过订阅config db的键空间事件完成vlan配置信息从config db到内核和硬件 config db json格式如下 xff1a 34 VLAN 34 34 Vlan1000
  • sonic容器构建

    sonic中大量的组件运行在docker容器中 xff0c 用于隔离彼此的运行环境 xff0c 从而解决相互之间的互斥问题 下面我们分析一下sonic中各个容器的构建过程 Dockerfile文件的生成过程 sonic中的容器Dockerf
  • ARM Cortex-M底层技术(九)KEIL MDK 分散加载示例1-更改程序运行基址

    KEIL MDK 分散加载示例1 更改程序运行基址 小编我一向主张在实战中学习 xff0c 不主张直接去去学习规则 amp 定义 xff0c 太枯燥 xff0c 在实际应用中去摸索 xff0c 才会真正理解具体的技术细节 xff0c 下面我
  • 如何修改已提交commit信息

    如何修改已提交commit信息 1 修改commit信息 1 1 修改最后一次提交信息 通过git log查看提交历史信息 xff1a 输入命令 xff1a span class token function git span commit
  • linux 安装mysql 以及常见错误总结

    安装的方法菜鸟教程上有 xff1a 入口 mysqld initialize 命令是出现如下错误Fatal error Please read 34 Security 34 section of the manual to find out
  • elementui下拉框多选报[Vue warn]: <transition-group> children must be keyed: <ElTag>

    elementui下拉框多选 xff0c 选值报错 选中一个值后所有的值都会被选中 经检查 xff0c 是由于我的下拉框的value值为一个对象而非单个值 值为对象时需要填入value key span class token operat
  • Hadoop_1 入门WordCount

    记录踩得坑以及部署环境流程 搭建的是伪分布Hadoop 首先环境需要安装zookeeper 这个好装 xff0c 不多说其次比较复杂的是安装openssh 我的Linux系统是centos 7 mini版本 安装openssh之前的准备工作