hadoop mapreduce csv

hadoop mapreduce csv

题目:有一个csv文件,记录了在网课中发言了的学生的名字和所在国家,要求:找到每个国家里有几个学生在这堂课上发言了。例:China:3。
测试文件长这个样子:
在这里插入图片描述

分析

这道题,本质上跟mapreduce的入门程序wordcount并没有什么区别,其实就是丢弃第一列(学生姓名),只需要保留国家名称就好,剩下的就跟wordcount是一样的了。对于我本人来说难点主要就是不熟悉csv文件,不好对csv文件进行处理。

关于csv文件

什么是csv文件-360百科

Java读取csv文件的多种方式

看了很多,依然不是很懂csv,只知道列是用英文逗号来分割的,行是用换行分割的,有研究的可以给我讲一下哈。所以就采取了一种投机取巧的办法。由于列是用英文逗号来分割的,不巧的是Name中也有英文逗号,巧的是每一个Name中都有,于是就采用逗号分割,将文件分割成三列,第三列就是Participati。将表头直接当作一行数据过滤掉。

WordCount程序代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;
import java.io.IOException;


public class WordCount {
    public static void main(String[] args) throws Exception {
        //1.创建配置对象
        Configuration conf = new Configuration();
        //2.创建操作对象
        Job job = Job.getInstance(conf, "WordCount");
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //必须手动指定map的输出类型,除非与map的输出类型一致
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置文件输入
        FileInputFormat.addInputPath(job, new Path("D:\\wc\\in"));
        //设置文件输出
        FileOutputFormat.setOutputPath(job, new Path("D:\\wc\\out"));
        //解决输出路径已经存在的问题
        FileSystem fileSystem = FileSystem.get(conf);
        Path outputPath = new Path("D:\\wc\\out");
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }

        //3.执行
        job.waitForCompletion(true);
    }
}
//两个阶段:Map,Reduce
//map:为了分块处理文件;reduce:聚合map的结果


    /**
     * keyIn:InputFormat读取文件后输入到Mapper中的key的类型,默认为LongWritable,TextInputFormat->RecordReader【LongWritable】
     * ValueIn:InputFormat读取文件后输入到Mapper中的value的类型Text【一行内容】
     * KeyOUT:自己从Map输出的key的类型【词频统计:Key可以使用word】
     * VALUEOUT:自己从Map输出的value的类型【词频统计:Value可以使用IntWritable,Longwritable】Hadoop有自己的序列化类型
     * Text->String,IntWritable->Integer
     * 序列化?
     * 将内存的东西序列化【写入,编程磁盘存储的东西:二进制】
     * 反序列化?
     * 将磁盘的东西读入内存的时候使用的一种转化方式
     */
      class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        //1.重写map方法

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //单词的拆分
            String[] words = value.toString().split("\t");
            for (String word : words) {
                context.write(new Text(word), new IntWritable(1));
            }
        }
    }

         class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
            //2.重写reduce方法
            //执行reduce方法之前,hadoop会自动的将相同的key的value进行分组

            /**
             * a 1
             * b 1
             * c 1
             * a 1
             * a 1
             * b 1
             * <p>
             * a[1 1 1]
             * b[1 1]
             * c[1]
             */
            @Override
            protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
                  int count = 0;
                for (IntWritable value:values) {
                    count += value.get();

                }
                context.write(key,new IntWritable(count));

            }
        }




此题代码:

package com.bigdata.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;

import java.io.IOException;

public class Part {
    public static void main(String[] args) throws Exception {
        //1.创建配置对象
        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        conf.set("yarn.app.mapreduce.am.resource.mb","256");
        //2.创建操作对象
        Job job = Job.getInstance(conf, "Part");
        job.setMapperClass(PartMapper.class);
        job.setReducerClass(PartReducer.class);
        //必须手动指定map的输出类型,除非与map的输出类型一致
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);


        //设置文件输入
        FileInputFormat.addInputPath(job, new Path("D:\\wc\\in"));//本地测试
        //设置文件输出
        FileOutputFormat.setOutputPath(job, new Path("D:\\wc\\out"));//本地测试


        //设置文件输入
        //FileInputFormat.addInputPath(job, new Path("hdfs://node01:9000/flume/dir"));//集群测试
        //设置文件输出
        //FileOutputFormat.setOutputPath(job, new Path("hdfs://node01:9000/flume/out"));//集群测试
        //解决输出路径已经存在的问题
        FileSystem fileSystem = FileSystem.get(conf);
        Path outputPath = new Path("D:\\wc\\out");//本地测试
        // Path outputPath = new Path("hdfs://node01:9000/flume/out");//集群测试
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }

        //3.执行
        job.waitForCompletion(true);
    }
}
//两个阶段:Map,Reduce
//map:为了分块处理文件;reduce:聚合map的结果


/**
 * keyIn:InputFormat读取文件后输入到Mapper中的key的类型,默认为LongWritable,TextInputFormat->RecordReader【LongWritable】
 * ValueIn:InputFormat读取文件后输入到Mapper中的value的类型Text【一行内容】
 * KeyOUT:自己从Map输出的key的类型【词频统计:Key可以使用word】
 * VALUEOUT:自己从Map输出的value的类型【词频统计:Value可以使用IntWritable,Longwritable】Hadoop有自己的序列化类型
 * Text->String,IntWritable->Integer
 * 序列化?
 * 将内存的东西序列化【写入,编程磁盘存储的东西:二进制】
 * 反序列化?
 * 将磁盘的东西读入内存的时候使用的一种转化方式
 */
class PartMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    //1.重写map方法

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //数据的拆分
        String line = value.toString();
        //过滤掉表头
        if (line.contains("Name")) {
            return;
        }
        
        String[] words = line.split(",");
        String [] pf = words[2].split("\n");
        
        for (String word : pf) {
            context.write(new Text(word), new IntWritable(1));
        }
    }
}


class PartReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    //2.重写reduce方法
    //执行reduce方法之前,hadoop会自动的将相同的key的value进行分组

    /**
     * a 1
     * b 1
     * c 1
     * a 1
     * a 1
     * b 1
     * <p>
     * a[1 1 1]
     * b[1 1]
     * c[1]
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for (IntWritable value : values) {
            count += value.get();

        }
        Text k =new Text();
        k.set(key+":");
        context.write(k, new IntWritable(count));

    }
}
  • 注意:Mapper和Reducer均为外部类

pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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>Exam</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <hadoop.version>3.2.2</hadoop.version>
        <slf4j.version>1.7.25</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    </dependencies>

   
</project>

打包到集群运行的步骤

(1)在pom.xml中添加下列依赖

 <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.bigdata.mr.Part</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- 主类名 -->
                            <mainClass>com.com.bigdata.mr.Part</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

        </plugins>
    </build>

(2)修改集群路径
(3)打包
在这里插入图片描述
(4)jar包所在位置 ,Exam为项目名称
在这里插入图片描述

(5)集群运行命令:hadoop jar jar包名称

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值