相关软件安装包及其版本说明如表所示。
软件 | 版本 | 安装包称 |
Oracle VM VirtualBox | 6.1.48 | |
ubuntu | 24.04 | ubuntu-24.04-desktop-amd64.iso |
hadoop | 3.3.5 | hadoop-3.3.5.tar.gz |
JDK | JDK1.8 |
1、数据集
来源:数据来源于阿里云天池:https://tianchi.aliyun.com/dataset/46
2、数据清洗
# 导入pandas库,用于数据处理
import pandas as pd
# 从csv文件中读取数据,命名为df
# 加载数据
df = pd.read_csv('taobao.csv')
# 打印数据框架的信息,包括列名、数据类型和非空值数量
# 检查数据信息,包括每列的非空值数量和数据类型
print(df.info())
# 计算每列的缺失值数量并打印
# 检查每列的缺失值数量
print(df.isnull().sum())
# 删除包含缺失值的行
# 删除有缺失值的行(或者选择填充缺失值)
df = df.dropna()
# 打印处理后的数据框架的前5行
# 最后,查看清洗后的数据
print(df.head())
# 从已清洗的csv文件中读取数据,命名为df2
df.to_csv('已清洗数据.csv', index=False)
import pandas as pd
# 读取 CSV 文件
df = pd.read_csv('已清洗数据.csv')
# 定义映射关系
#将CSV文件中的 behavior_type 列中的数值 (1, 2, 3, 4) 转换为相应的文本标签 (点赞、收藏、购物车、支付)
behavior_map = {
1: '点赞',
2: '收藏',
3: '购物车',
4: '支付'
}
# 将 behavior_type 列中的数值转换为对应的文本
df['behavior_type'] = df['behavior_type'].map(behavior_map)
# 保存修改后的 DataFrame 到新的 CSV 文件
df.to_csv('已清洗数据.csv', index=False)
#输出
print("已清洗数据.csv")
3、Mapreduce词频统计
#启动hadoop
cd /usr/local/hadoop
./sbin/start-all.sh
wordcount.java
package com.iflytek;
import org.apache.hadoop.fs.Path;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import java.util.TreeMap;
import java.util.Map;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
* WordCount程序,用于统计文本中各个位置出现的次数,并输出出现次数最多的前10个位置。
*/
public class WordCount {
/**
* Map阶段的处理器,负责读取输入数据,提取位置信息,并发送到Reducer。
*/
public static class LocationCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 解析输入行,获取位置信息
String line = value.toString();
String[] fields = line.split(",");
String location = fields[1];
// 将位置作为key,发送到Reducer
context.write(new Text(location), new IntWritable(1));
}
}
/**
* Reduce阶段的处理器,负责汇总位置出现的次数,并维护出现次数最多的前10个位置。
*/
public static class LocationCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private TreeMap<Integer, String> countMap = new TreeMap<Integer, String>();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
// 对位置出现的次数进行累加
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
// 将位置和出现次数作为一个键值对加入到TreeMap中
countMap.put(sum, key.toString());
// 如果TreeMap的大小超过了10,移除出现次数最少的位置
if (countMap.size() > 10) {
countMap.remove(countMap.firstKey());
}
}
/**
* 在Reduce任务结束时,将出现次数最多的前10个位置输出。
*/
protected void cleanup(Context context) throws IOException, InterruptedException {
// 反转TreeMap,以便按照出现次数从多到少的顺序输出
for (Map.Entry<Integer, String> entry : countMap.descendingMap().entrySet()) {
context.write(new Text(entry.getValue()), new IntWritable(entry.getKey()));
}
}
}
/**
* 程序的入口点。
* 配置并执行WordCount作业。
*
* @param args 命令行参数,包括输入和输出路径等。
* @throws Exception 如果作业执行过程中出现错误。
*/
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Location Count");
job.setJarByClass(WordCount.class);
job.setMapperClass(LocationCountMapper.class);
job.setReducerClass(LocationCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("/home/hadoop/alibaba/datas/alibaba/input/已清洗数据.csv已清洗数据.csv"));
FileOutputFormat.setOutputPath(job, new Path("/home/hadoop/alibaba/WordCount"));
job.setNumReduceTasks(1);
job.waitForCompletion(true);
}
}
项目已上传至github