mapreduce的使用

以下案例写之前需要导入jar包依赖:

  <dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>

1.单词计数案例:

package com.xyz;

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; import java.io.IOException; /**
* @author 小勇子start
* @create 2021-10-11 16:35
*/
public class WordCount { public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf=new Configuration();
Job job= Job.getInstance(conf);
//这里是对jar包的类,map的类和reduce的类三者的映射
job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
//这里是对map输出的关键字和值对应类型的映射
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//这里是对reduce输出的关键字和值对应类型的映射
job.setOutputKeyClass(Text.class);
job.setOutputKeyClass(IntWritable.class); //数据输入路径(此处为本地测试)
FileInputFormat.setInputPaths(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\mapreduceTest\\src\\data\\test.txt"));
//数据输出路径(此处为本地测试)
FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\mapreduceTest\\src\\data\\out1")); boolean flag=job.waitForCompletion(true);
System.exit(flag?0:1);
} static class Map extends Mapper<LongWritable,Text, Text, IntWritable>{
/*Text的包有很多,注意导的是:org.apache.hadoop.io.Text
在map中前两个数据类型基本固定
后两个代表着要输出的类型,如果有reduce,则以该类型发送给reduce
*/没有reduce则直接以该类型输出到目标
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] str=value.toString().split(",");
for (String s:str) {
context.write(new Text(s),new IntWritable(1));
}
}
} static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
/*
<Text,IntWritable,Text,IntWritable>
前两个是map发过来关键字和值的类型
后两个是reduce输出关键字和值的类型 */
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int len=0;
for (Object s:values) {
len++;
}
context.write(key,new IntWritable(len));
}
}
}

2.数据清洗案例:

package com.xyz2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
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;
/**
* @author 小勇子start
* @create 2021-10-11 17:23
*/
public class RepeatProcessing { public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf=new Configuration();
Job job= Job.getInstance(conf); job.setJarByClass(RepeatProcessing.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); job.setMapOutputValueClass(NullWritable.class);
job.setMapOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
//此处为本地测试
FileInputFormat.setInputPaths(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\mapreduceTest\\src\\data\\test2.txt"));
//此处为本地测试
FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\mapreduceTest\\src\\data\\out2")); boolean flag=job.waitForCompletion(true);
long repeat=job.getCounters().findCounter(Count.repeatCount).getValue(); System.out.println("重复的行数为:"+repeat); System.exit(flag?0:1);
} static enum Count{//枚举计数
repeatCount
} static class Map extends Mapper<LongWritable, Text,Text, NullWritable>{ @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,NullWritable.get());
}
} static class Reduce extends Reducer<Text, NullWritable,Text,NullWritable>{
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
Counter counter=context.getCounter(Count.repeatCount);
int len=0;
for (Object s:values) {
len++;
}
long val= counter.getValue();
val+=len-1;
counter.setValue(val);
context.write(key,NullWritable.get());
}
}
}

3.topN案例:

package com.xyz;

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; import java.io.IOException;
import java.util.*; /**
* @author 小勇子start
* @create 2021-09-30 19:53
*/
public class Task2_4 { static class TaskMap extends Mapper<LongWritable, Text, Text, Text> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split(";");
String filmName = line[0];//电影名称
String filmType = line[6];//电影类型
String[] types = filmType.split("/|、|,");
for (String s : types) {
s=s.trim();
if (s.length() > 2) { for (int i = 2; i < s.length(); i += 2) {
String newStr = s.substring(i - 2, i);
context.write(new Text(newStr), new Text(filmName));
}
} else
context.write(new Text(s), new Text(filmName));
} }
} static class TaskReduce extends Reducer<Text, Text, Text, IntWritable> {
ArrayList<Object[]> arrayList=new ArrayList<Object[]>();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//此处用set是因为Iterable<Text> values中存储的值中有重复的电影名称,需要取不同名字的个数
HashSet set=new HashSet();
for (Text s : values) {
set.add(s);
}
Object[] data={set.size(),key.toString()};
arrayList.add(data);
} @Override
protected void cleanup(Context context) throws IOException, InterruptedException {
Object[] data=arrayList.toArray();
Arrays.parallelSort(data, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Object[] o_1=(Object[]) o1;
Object[] o_2=(Object[]) o2;
return (int)o_2[0]-(int)o_1[0];
}
});
for (Object d:data) {
Object[] d1=(Object[]) d;
context.write(new Text(d1[1].toString()),new IntWritable((int)d1[0]));
}
/*取前3个
for (int i=0;i<3;i++) {
Object[] d1=(Object[]) data[i];
context.write(new Text(d1[1].toString()),new IntWritable((int)d1[0]));
}
*/ }
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(Task2_4.class);
job.setMapperClass(TaskMap.class);
job.setReducerClass(TaskReduce.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\作业\\7月25日晚上任务\\数据\\2-1"));
FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\作业\\7月25日晚上任务\\数据\\2-42")); boolean flag = job.waitForCompletion(true); System.exit(flag ? 0 : 1); }
}

4.join案例(自定义Writable)

package com.xyz;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; /**
* @author 小勇子start
* @create 2021-10-12 10:32
*/
public class JoinDemo {
static class MyDataWritable implements Writable {
private String flag;//标记
private String data;//一行数据 @Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(flag);
dataOutput.writeUTF(data);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
this.flag=dataInput.readUTF();
this.data=dataInput.readUTF();
}
public String getFlag(){
return flag;
}
public void setFlag(String flag){
this.flag=flag;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
} static class Map extends Mapper<LongWritable, Text, IntWritable,MyDataWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] str=value.toString().split(",");
int id=Integer.valueOf(str[0]);
String fileName=((FileSplit)context.getInputSplit()).getPath().getName();
MyDataWritable myData=new MyDataWritable();
myData.setData(value.toString());
if(fileName.contains("customers"))
myData.setFlag("kh");
else
myData.setFlag("order");
context.write(new IntWritable(id),myData);
}
} static class Reduce extends Reducer<IntWritable,MyDataWritable,Text, NullWritable>{
@Override
protected void reduce(IntWritable key, Iterable<MyDataWritable> values, Context context) throws IOException, InterruptedException {
String khData="";
String oData="";
for (MyDataWritable m:values) {
if (m.getFlag().equals("kh"))
khData=m.getData();
else
oData=m.getData();
}
String newData=khData+","+oData;
context.write(new Text(newData),NullWritable.get());
}
} public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf=new Configuration();
Job job= Job.getInstance(conf); job.setJarByClass(JoinDemo.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); job.setMapOutputValueClass(MyDataWritable.class);
job.setMapOutputKeyClass(IntWritable.class); job.setOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
//此处为本地测试
FileInputFormat.setInputPaths(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\joinDemo\\DemoOne\\join\\"));
//此处为本地测试
FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\小勇子\\Desktop\\大数据培训\\练习\\joinDemo\\DemoOne\\join\\out1"));
boolean flag=job.waitForCompletion(true);
System.exit(flag?0:1);
}
}

5.时间日期格式化

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* @author 小勇子start
* @create 2021-10-11 17:53
*/
public class DateTimeFormat {
public static void main(String[] args) throws ParseException {
String dateStr="2021.09.05";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd"); Date oldDate=sdf.parse(dateStr);
String newDateStr=sdf1.format(oldDate);
Date newDate=sdf1.parse(newDateStr); System.out.println(oldDate);//结果:Sun Sep 05 00:00:00 CST 2021
System.out.println(newDateStr);//结果:2021-09-05
System.out.println(newDate);//结果:Sun Sep 05 00:00:00 CST 2021
}
}

6.mapreduce项目打jar包

  1. 右键项目 Open Module Settings
  2. 找到左侧 Artifacts,点击中间的 +号,选择JAR,然后选择“”“From Module With dependicies”
  3. 选择要执行的Main方法
  4. 点击idea上面的Builde菜单,选择Builder Artifacts。然后builder即可(如果改了代码,直接点击Rebuild
  5. 打包后将jar包上传至linux中

7.运行jar包

  1. 在linux中创建一个文本文件,然后上传至hdfs中
  2. 开始运行程序: hadoop jar xxxx.jar com.xyzy.test1.MyWordDriver /xxx.txt /out1

如果你程序中输入,输出路径是写死的(即不是用的args[0]这样的方式),那么/xxx.txt 和/out1就不需要了 ;在上传后jar包所在的位置执行上述命令,否则jar包前面使用绝对路径。

mapreduce的使用的更多相关文章

  1. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  2. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  3. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  4. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  5. [Hadoop in Action] 第5章 高阶MapReduce

    链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter   1.链接MapReduce作业   [顺序链接MapReduce作业]   mapreduce-1 | mapr ...

  6. MapReduce

    2016-12-21  16:53:49 mapred-default.xml mapreduce.input.fileinputformat.split.minsize 0 The minimum ...

  7. 使用mapreduce计算环比的实例

    最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...

  8. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  9. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  10. MapReduce剖析笔记之六:TaskTracker初始化任务并启动JVM过程

    在上面一节我们分析了JobTracker调用JobQueueTaskScheduler进行任务分配,JobQueueTaskScheduler又调用JobInProgress按照一定顺序查找任务的流程 ...

随机推荐

  1. day22-web开发会话技术04

    WEB开发会话技术04 14.Session生命周期 14.1生命周期说明 public void setMaxInactiveInterval(int interval):设置session的超时时 ...

  2. ES文件浏览器局域网传输文件分析

    软件下载链接 1.前言 我之前从手机上传输到电脑上一些apk进行分析,都是使用es文件浏览器这款软件获取 app,传输方面使用QQ,这样很麻烦,走外网流量暂且不提,总是感觉浪费掉了局域网这个环境.简单 ...

  3. Linux 中的文件简单说明

    Linux 中的文件简单说明 作者:Grey 原文地址: 博客园:Linux 中的文件简单说明 CSDN:Linux 中的文件简单说明 说明 本文基于 CentOS 7 根目录(/)下文件夹主要作用 ...

  4. Linux系统下安装tomcat步骤

    安装参考教程:https://www.cnblogs.com/li150dan/p/12535067.html 说明:jdk自动安装后路径是/usr/lib/jvm 在"vim /etc/p ...

  5. 【HarmonyOS】ArkTS Native开发——使用 system函数创建文件

    ​ ArkTS是HamronyOS优选的主力语言,但官方文档指南中对于Native应用开发并没有详细的描述,只有一篇Codelab可以学习(简易Native C++ 示例(ArkTS) (huawei ...

  6. 出现报错:The field admin.LogEntry.user was declared with a lazy reference to 'api.user', but app 'api' isn't installed.解决方法

  7. global与nonlocal、函数名用法、闭包函数、装饰器

    今日内容回顾 目录 今日内容回顾 global与nonlocal 函数名的多种用法 闭包函数 装饰器简介 装饰器推导流程 装饰器模板 装饰器语法糖 练习 global与nonlocal 函数名的多种用 ...

  8. Pytorch 基本操作

    Pytorch 基础操作 主要是在读深度学习入门之PyTorch这本书记的笔记.强烈推荐这本书 1. 常用类numpy操作 torch.Tensor(numpy_tensor) torch.from_ ...

  9. Mybatis-9.28

    Mybatis-9.28 环境: JDK1.8 Mysql 5.7 maven 3.6.1 IDEA 回顾: JDBC Mysql Java基础 Maven Junit SSM框架:配置文件的. 最好 ...

  10. 【转载】EXCEL VBA 通过VBA中的Union合并多个Range选择区域

    在Excel中,Union的功能是合并两个或两个以上的选择区域,合并成为一个更大的区域. 所合并的多个选择区域,这些选择区域,可以是不连续的,也可以是连续的.一般情况下,要使用Union,可通过如下来 ...