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

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

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

程序概览

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

java 复制代码
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阶段的代码如下:

java 复制代码
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));
    }
}

全部代码

java 复制代码
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,大数据,模型训练等。

相关推荐
bin91534 小时前
【EXCEL数据处理】000013 案例 EXCEL筛选与高级筛选。
大数据·信息可视化·数据挖掘·数据分析·excel·数据可视化·数据筛选
璞华Purvar6 小时前
服贸会上的科技闪耀之星:璞华易研PLM系统引领产品研发潮流
大数据·人工智能·科技
小码农叔叔6 小时前
【大数据】Flink CDC 实时同步mysql数据
大数据·mysql·flink
B站计算机毕业设计超人7 小时前
计算机毕业设计hadoop+spark天气预测 天气可视化 天气大数据 空气质量检测 空气质量分析 气象大数据 气象分析 大数据毕业设计 大数据毕设
大数据·hive·hadoop·机器学习·spark·网络爬虫·数据可视化
雪碧没有冰块9 小时前
Flink源码剖析
大数据·flink
kkivivu10 小时前
家庭用超声波清洗机好用吗?推荐四款性能绝佳的超声波清洗机!
大数据
大G哥11 小时前
ELK日志收集之ES的DSL查询语句
大数据·elk·elasticsearch·搜索引擎·jenkins
青云交11 小时前
大数据新视界 --大数据大厂之数据质量评估指标与方法:提升数据可信度
大数据·sql·一致性·完整性·数据质量评估·数据可信度·准确性·时效性
黄焖鸡能干四碗11 小时前
【需求分析】软件系统需求设计报告,需求分析报告,需求总结报告(原件PPT)
大数据·人工智能·安全·测试用例·需求分析
YONG823_API13 小时前
电商平台数据批量获取自动抓取的实现方法分享(API)
java·大数据·开发语言·数据库·爬虫·网络爬虫