通过mapreduce程序统计旅游订单(wordcount升级版)

news/2024/4/30 8:08:42

通过mapreduce程序统计旅游订单(wordcount升级版)

本文将结合一个实际的MapReduce程序案例,探讨如何通过分析旅游产品的预订数据来揭示消费者的偏好。

程序概览

首先,让我们来看一下这个MapReduce程序的核心代码。这个程序的目的是处理一个包含旅游产品预订信息的文本文件,并统计每个产品特性的出现次数。Map阶段的代码如下:

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {private Text word = new Text();public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {if (key.get() > 0) { // 跳过表头String line = value.toString();String[] fields = line.split("\t");if (fields.length > 1 && !fields[1].isEmpty()) {String[] arrstr = Arrays.copyOfRange(fields, 8, fields.length - 1);for(String str:arrstr){if(StringUtils.isNotBlank(str)){word.set(str);context.write(word, new IntWritable(1));}}}}}
}

Reduce阶段的代码如下:

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}context.write(key, new IntWritable(sum));}
}

在这里插入图片描述

全部代码

package org.example;
import java.io.IOException;
import java.util.Arrays;import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;public class KeyWord{public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {//        private final static IntWritable one = new IntWritable(1);private Text word = new Text();public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {if (key.get() > 0) { // 跳过表头String line = value.toString();String[] fields = line.split("\t");if (fields.length > 1 && !fields[1].isEmpty()) {String[] arrstr = Arrays.copyOfRange(fields, 8, fields.length - 1);for(String str:arrstr){if(StringUtils.isNotBlank(str)){word.set(str);context.write(word, new IntWritable(1));}}
//                    int a;
//                    if(StringUtils.isNotBlank(fields[4])){
//                        a = Integer.parseInt(fields[4]);
//                    }else{
//                        a=0;
//                    }}}}}public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}context.write(key, new IntWritable(sum));}}public  void keyWorsds() throws Exception {Configuration conf = new Configuration();Job job = Job.getInstance(conf, "Word Count on Second Field");job.setJarByClass(KeyWord.class);job.setMapperClass(Map.class);job.setReducerClass(Reduce.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setInputFormatClass(org.apache.hadoop.mapreduce.lib.input.TextInputFormat.class);job.setOutputFormatClass(org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.class);org.apache.hadoop.mapreduce.lib.input.FileInputFormat.addInputPath(job, new Path("/Users/shareit/ds_task_am/wordcount/src/main/resources/mapreduce数据(1).txt"));org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.setOutputPath(job, new Path("/Users/shareit/ds_task_am/wordcount/producttotalhuman"));job.waitForCompletion(true);}
}

结论

通过MapReduce程序对旅游产品预订数据的分析,我们能够洞察到消费者的偏好和行为模式。这些信息对于旅游企业来说是宝贵的,可以帮助他们更好地定位市场,设计符合消费者需求的产品,并最终提高客户满意度和市场份额。随着数据分析技术的不断进步,旅游行业将能够更加精准地满足消费者的需求,推动行业的持续发展。
如有遇到问题可以找小编沟通交流哦。另外小编帮忙辅导大课作业,学生毕设,代写各种mapreduce程序等。不限于python,java,大数据,模型训练等。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11539.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

链表面试题

删除链表中等于给定值 val 的所有节点 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(int val, ListNode next) { this.val val; this.next next; }* }*/ …

VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程

引言劝退 VSCode&#xff0c;全称为Visual Studio Code&#xff0c;是由微软开发的一款轻量级&#xff0c;跨平台的代码编辑器。大家能来搜用VSCode配置c/c&#xff0c;想必也知道VSCode的强大&#xff0c;可以手握一个VSCode同时编写如C&#xff0c;C&#xff0c;C#&#xff…

nginx的https与动态负载均衡

nginx的https 证书可以根据你的域名和服务器服务商去进行签发 , 比如 : 阿里云 腾讯云 百度云 华为云等 这里使用的是腾讯云 : 下载证书 : 选择 nginx: 下载之后传递到服务器上。 下面开始配置nginx的https: 1. 解压下载的证书包 cd /etc/ssl unzip xxcc.dwa_nginx.zip mv…

鸿蒙OS开发实例:【应用事件打点】

简介 传统的日志系统里汇聚了整个设备上所有程序运行的过程流水日志&#xff0c;难以识别其中的关键信息。因此&#xff0c;应用开发者需要一种数据打点机制&#xff0c;用来评估如访问数、日活、用户操作习惯以及影响用户使用的关键因素等关键信息。 HiAppEvent是在系统层面…

安装Docker(CentOS)

Docker 分为 CE 和 EE 两大版本。CE 即社区版&#xff08;免费&#xff0c;支持周期 7 个月&#xff09;&#xff0c;EE 即企业版&#xff0c;强调安全&#xff0c;付费使用&#xff0c;支持周期 24 个月。 Docker CE 分为 stable test 和 nightly 三个更新频道。 官方网站上…

蓝桥杯 - 受伤的皇后

解题思路&#xff1a; 递归 回溯&#xff08;n皇后问题的变种&#xff09; 在 N 皇后问题的解决方案中&#xff0c;我们是从棋盘的顶部向底部逐行放置皇后的&#xff0c;这意味着在任何给定时间&#xff0c;所有未来的行&#xff08;即当前行之下的所有行&#xff09;都还没…