使用MapReduce求出各年销售笔数、各年销售总额

2023-11-24 20:20

本文主要是介绍使用MapReduce求出各年销售笔数、各年销售总额,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1)将上面的数据文件上传到hdfs

hdfs dfs -put sales.csv /input/

2)采用Eclipse/IDEA创建一个Maven工程,同时修改pom.xml文件,增加dependencies,/dependencies、build,/build节点,内容如下:

    <dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.7</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>2.7.7</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-mapreduce-client-core</artifactId><version>2.7.7</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.7</version></dependency>
    <plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><archive><manifest><!-- main()所在的类,注意修改 --><mainClass>org.example.SoldMain</mainClass></manifest></archive></configuration></plugin></plugins>

3)开始开发java代码,需要4个类:

首先是主输出类SoldMain(代码如下):

package org.example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
public class SoldMain {public static void main(String[] args) throws Exception {
//1. 创建一个job和任务入口(指定主类)
Job job = Job.getInstance(new Configuration());job.setJarByClass(SoldMain.class);//2. 指定job的mapper和输出的类型<k2 v2>
job.setMapperClass(SoldMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Sold.class);
//3. 指定job的reducer和输出的类型<k4  v4>job.setReducerClass(SoldReduce.class);job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//4.指定job的输入和输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//5. 执行jobjob.waitForCompletion(true);}
}

然后是SoldMapper类(代码如下):

package org.example;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class SoldMapper extends Mapper<LongWritable, Text, Text, Sold> {@Override
protected void map(LongWritable k1, Text v1,
Context context)
throws IOException, InterruptedException {//字段名 prod_id,cust_id,time,channel_id,promo_id,quantity_sold,amount_sold
//数据类型:Int,Int,Date, Int,Int ,Int ,float(10,2),
//数据: 13,987,1998-01-10,3,999,1,1232.16
String data = v1.toString();
String[] words = data.split(",");
//数据: t1=987,1998-01-10,3,999,1,1232.16String t1 = StringUtils.substringAfter(data, ",");
//数据: t2=1998-01-10,3,999,1,1232.16 
String t2 = StringUtils.substringAfter(t1, ",");
//取年份为偏移量,数据: words2[0]=1998,words2[1]=01,words2[2]=10,3,999,1,1232.16
String[] words2 = t2.split("-");
//        StringUtils.substringAfter("dskeabcedeh", "e");
//        /*结果是:abcedeh*/Sold sold = new Sold();sold.setTime(words[2]);//数组word[]
sold.setQuantity_sold(Integer.parseInt(words[5]));
sold.setAmount_sold(Float.valueOf(words[6]));
context.write(new Text(words2[0]), sold);//数组word2[],word2[0]代表年份作为k2}
}

接着是SoldReduce类(代码如下):

package org.example;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class SoldReduce extends Reducer<Text, Sold, Text, Text> {protected void reduce(Text k3, Iterable<Sold> v3, Context context) throws IOException, InterruptedException {
int total1 = 0;
float total2 = 0;
for (Sold sold : v3) {
total1 = total1 + sold.getQuantity_sold();
total2 = total2 + sold.getAmount_sold();
}
String total = "销售笔数:" + Integer.toString(total1) + "," + "销售总额:" + Float.toString(total2);
context.write(k3, new Text(total));
}
}

最后是Sold类(代码如下):

package org.example;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class Sold implements Writable {
//字段名 prod_id,cust_id,time,channel_id,promo_id,quantity_sold,amount_sold//数据类型:Int,Int,Date, Int,Int ,Int ,float(10,2),//数据: 13, 987, 1998/1/10, 3, 999,1, 1232.16
//由以上定义变量
private int prod_id;
private int cust_id;
private String time;
private int channel_id;
private int promo_id;
private int quantity_sold;
private float amount_sold;//奖金
//序列化方法:将java对象转化为可跨机器传输数据流(二进制串/字节)的一种技术
public void write(DataOutput out) throws IOException {out.writeInt(this.prod_id);out.writeInt(this.cust_id);out.writeUTF(this.time);out.writeInt(this.channel_id);out.writeInt(this.promo_id);out.writeInt(this.quantity_sold);out.writeFloat(this.amount_sold);
}
//反序列化方法:将可跨机器传输数据流(二进制串)转化为java对象的一种技术public void readFields(DataInput in) throws IOException {this.prod_id = in.readInt();this.cust_id = in.readInt();this.time = in.readUTF();this.channel_id = in.readInt();this.promo_id = in.readInt();this.quantity_sold = in.readInt();this.amount_sold = in.readFloat();}
public int getProd_id() {return prod_id;}public void setProd_id(int prod_id) {this.prod_id = prod_id;}public int getCust_id() {return cust_id;}public void setCust_id(int cust_id) {this.cust_id = cust_id;
}public String getTime() {return time;
}
public void setTime(String time) {
this.time = time;
}
public int getChannel_id() {return channel_id;
}public void setChannel_id(int channel_id) {this.channel_id = channel_id;}
public int getPromo_id() {return promo_id;
}public void setPromo_id(int promo_id) {this.promo_id = promo_id;
}public int getQuantity_sold() {
return quantity_sold;}
public void setQuantity_sold(int quantity_sold) {
this.quantity_sold = quantity_sold;}
public float getAmount_sold() {
return amount_sold;
}public void setAmount_sold(float amount_sold) {this.amount_sold = amount_sold;}
}

4)使用命令(如下)打包:

mvn clean package

5)将jar包通过xftp传输到linux下,在hadoop环境运行jar包,命令如下:

hadoop jar annualTotal-0.0.1-SNAPSHOT.jar  /input/sales.csv  /output/sales

jar包名和输入输出名请自行修改
6)查看执行结果(命令如下):

hdfs dfs -cat /output/sales/part-r-00000

输出路径请自行查看
结果

这篇关于使用MapReduce求出各年销售笔数、各年销售总额的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/422521

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(