MapReduce实战项目——芝加哥的犯罪数据分析

本文利用MapReduce进行警区犯罪数据的分析。通过代码实现了按警区排序找出犯罪数量最少和最多的警区,按警区分组统计逮捕数量,合并分析结果文件,还计算了每个警区的逮捕率,以评估警区治安和破案效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Configuration conf=new Configuration();

Job job=Job.getInstance(conf,“crime1”);

job.setJobName(“crime1”);

job.setJarByClass(Crime.class);

job.setMapperClass(CMapper.class);

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(Text.class);

job.setReducerClass(CReduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job,new Path(args[1]));

job.waitForCompletion(true);

}

}

请添加图片描述

这里是按从小到大的顺序进行警区的排序,由此看出犯罪数量最少的警区是警区1,犯罪数量最多的是警区934,判断出来警区1 的治安比较好。

请添加图片描述

请添加图片描述

2.这里是按警区进行分组,然后统计每一组的逮捕数量,逮捕数量即为警察在案件发生后,成功抓捕犯罪人员的次数。逮捕数量高,说明这个警区的警察抓捕成功率更高,更容易抓到犯罪人员。

//这里的代码实现的是按地区District进行分组,然后统计逮捕数

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Crime {

private static class CMapper extends Mapper<LongWritable , Text, Text, IntWritable>{

Text dis=new Text();

//private final static IntWritable one = new IntWritable();

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{

String line = value.toString();

String[] l = line.split(“,”);

dis.set(l[11]);

String arr=l[8];

String a =“FALSE”;

int b=0;

if (arr.equals(a)){

b=1;

}

else {

b=0;

}

if(b==0){

context.write(dis, new IntWritable(1));

}

}

}

public static class CReduce extends Reducer<Text, IntWritable, Text, IntWritable>{

public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException{

int count = 0;

for (IntWritable val : values) {

count = count +val.get();

}

context.write(new Text(key), new IntWritable(count));

}

}

public static void main(String[] args) throws Exception {

Configuration conf=new Configuration();

Job job=Job.getInstance(conf,“crime1”);

job.setJobName(“crime1”);

job.setJarByClass(Crime.class);

job.setMapperClass(CMapper.class);

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(IntWritable.class);

job.setReducerClass(CReduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job,new Path(args[1]));

job.waitForCompletion(true);

}

}

请添加图片描述

请添加图片描述

3.将上面两个分析的结果文件合并在一起,文件合并,合并后的文件名称为ave

import java.io.IOException;

import java.util.Vector;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.FloatWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Crime {

private static class CMapper extends Mapper<LongWritable , Text, Text, Text>{

private FileSplit inputsplit;

int cnum=0;

int arr1=0;

Text dis=new Text();

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{

inputsplit = (FileSplit)context.getInputSplit();

String filename = inputsplit.getPath().getName();

if(filename.contains(“cnumber”)){

String s1 = value.toString();

String[] split1 = s1.split(“\t”);

dis=new Text(split1[0]);

cnum=Integer.parseInt(split1[1]);

context.write(dis, new Text(“cnumber”+cnum));

}

if(filename.contains(“arr”)){

String s2 = value.toString();

String[] split2 = s2.split(“\t”);

dis=new Text(split2[0]);

arr1=Integer.parseInt(split2[1]);

context.write(dis, new Text(“arr”+arr1));

}

}

}

public static class CReduce extends Reducer<Text, Text, Text, Text>{

public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException{

Vector a = new Vector();

Vector b = new Vector();

for(IntWritable value : values){

String line = value.toString();

if(line.startsWith(“cnumber”)){

a.add(line.substring(“cnumber”.length()));

}

if(line.startsWith(“arr”)){

b.add(line.substring(“arr”.length()));

}

}

for(String w1 : a) {

for(String w2 : b){

context.write(new Text(key+“/t”+w1),new Text(w2));

}

}

}

}

public static void main(String[] args) throws Exception {

Configuration conf=new Configuration();

Job job=Job.getInstance(conf,“crime1”);

job.setJobName(“crime1”);

job.setJarByClass(Crime.class);

job.setMapperClass(CMapper.class);

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(Text.class);

job.setReducerClass(CReduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job,new Path(args[1]));

job.waitForCompletion(true);

}

}

在这里插入图片描述

4.计算每个警区的逮捕率,逮捕率即说明各个警区的破案效率,逮捕率高说明破案速度更快,该警区的治安情况就可能会更好。

这里写了一个ave类,用来放入案件数量和逮捕数量。

在这里插入图片描述

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.DoubleWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.math.BigDecimal;

import java.text.ParseException;

public class Crime {

private static class CMapper extends Mapper<LongWritable , Text, Text, ave>{

private ave a =new ave();

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{

String line=value.toString();

String[] s=line.split(“\t”);

String dis=s[0];

int num1=Integer.parseInt(s[1]);

int arr1=Integer.parseInt(s[2]);

a.setnum(num1);

a.setarr(arr1);

context.write(new Text(dis),a);

}

}

public static class CReduce extends Reducer<Text, ave, Text, DoubleWritable>{

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。

很多人担心学了容易忘,这里教你一个方法,那就是重复学习。

打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。

很多人担心学了容易忘,这里教你一个方法,那就是重复学习。

打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

[外链图片转存中…(img-yIyGzaGD-1713223552827)]

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值