通过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,大数据,模型训练等。

相关推荐
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
人类群星闪耀时7 小时前
物联网与大数据:揭秘万物互联的新纪元
大数据·物联网·struts
桃林春风一杯酒13 小时前
HADOOP_HOME and hadoop.home.dir are unset.
大数据·hadoop·分布式
桃木山人13 小时前
BigData File Viewer报错
大数据·java-ee·github·bigdata
B站计算机毕业设计超人14 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
数造科技14 小时前
紧随“可信数据空间”政策风潮,数造科技正式加入开放数据空间联盟
大数据·人工智能·科技·安全·敏捷开发
逸Y 仙X17 小时前
Git常见命令--助力开发
java·大数据·git·java-ee·github·idea
caihuayuan418 小时前
PHP建立MySQL持久化连接(长连接)及mysql与mysqli扩展的区别
java·大数据·sql·spring
B站计算机毕业设计超人18 小时前
计算机毕业设计Hadoop+Spark+DeepSeek-R1大模型民宿推荐系统 hive民宿可视化 民宿爬虫 大数据毕业设计(源码+LW文档+PPT+讲解)
大数据·hadoop·爬虫·机器学习·课程设计·数据可视化·推荐算法
(; ̄ェ ̄)。18 小时前
在nodejs中使用ElasticSearch(二)核心概念,应用
大数据·elasticsearch·搜索引擎