栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

大数据hadoop-wordcount实例(手把手教学)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

大数据hadoop-wordcount实例(手把手教学)

  1. 安装ideal
  2. 配置jdk
  3. 配置主机的hadoop环境变量,D:1_softwarehadoop-3.1.0 hadoop-3.1.0文件建议安装到没有中文,空格的文件目录下。

在path下添加环境变量:

  1. 网页若不可以直接创建文件夹,则使用
    [atguigu@hadoop102 ~]$ hadoop fs -mkdir /input
    再网页查看创建结果:

  2. 使用vim命令创建wordcount.txt文件夹,并将文件夹上传至HDFS的/input 文件夹下。
    [atguigu@hadoop102 ~]vim wordcount.txt (内容随便吧)
    [atguigu@hadoop102 ~]hadoop fs -put wordcount.txt /input

  3. 使用idea编译器编写wordcount案例
    ① 创建maven工程,更改maven路径

② ,修改pom.xml文件。



    4.0.0

    com.aiguigu
    MapReduceDemo
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            org.apache.hadoop
            hadoop-client
            2.9.1
        
        
            junit
            junit
            4.12
        
        
            org.slf4j
            slf4j-log4j12
            1.7.30
        
    

    
        
				org.apache.maven.plugins
            maven-compiler-plugin
            3.6.1
            
                1.8
                1.8
            
        
        
      org.apache.maven.plugins
      maven-assembly-plugin
      3.0.0
            
                
                    jar-with-dependencies
                
            
            
                
                    make-assembly
                    package
                    
                        single
                    
                
            
        
    


③编写wordcountDriver、wordcountMaper、wordcountReducer代码,点击maven->package 打包运行,在target文件中生成*.jar文件。复制到桌面上,并重命名为wc.jar。
wordcountMapper:

**package com.atguigu.mapreduce.wordcount2;**(自己换一下包名)
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class WordCountMapper extends Mapper {

    
    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException {
        //获取一行
        String line = value.toString();
        //对字符串进行切割
        String[] words = line.split(" ");
        //循环写出,有context写出
        for (String word : words) {
            //封装
            outK.set(word);
            //写出
            context.write(outK,outV);
        }
    }
}

wordcountReducer:

package com.atguigu.mapreduce.wordcount2;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer{
    private IntWritable outV = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException {

        int sum = 0;
        //atguigu(1,1)
        //累加
        for (IntWritable value : values) {
            sum += value.get();
        }

        outV.set(sum);
        //写出
        context.write(key,outV);
    }
}

wordcountDriver:

package com.atguigu.mapreduce.wordcount2;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        //1.获取job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2.设置jar包路径
        job.setJarByClass(WordCountDriver.class);

        //3.关联mapper的reduce
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //4设置map输出的KV类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //5.设置最终输出的KV类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

		      //6.设置输入路径和输出路径。
        

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //7.提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}

  1. 通过xftp将wc.jar文件上传至hadoop文件夹(注意文件夹名称不重要,主要看文件夹的内容,里边有bin、etc、等)下,

  2. 使用命令:
    首先复制wordcountDriver 的路径

以我的为例:复制的路径为com.atguigu.mapreduce.wordcount2.WordCountDriver
[jmxx@master hadoop] hadoop jar wc.jar com.atguigu.mapreduce.wordcount2.WordCountDriver /input /output
注意:/input文件夹下一定要有文件(之前咱们已经在hdfs上创建了wordcoiunt.txt)
/output 一定不能存在output文件夹(它自己创建)
9. 网页显示结果(hdfs),点击查看part-r-000000文件,

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号