大数据----基于sogou.500w.utf8数据的MapReduce编程

目录

    • 一、前言
    • 二、准备数据
    • 三、编程实现
      • 3.1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录
      • 3.2、统计rank<3并且order>2的所有UID及数量
      • 3.3、上午7-9点之间,搜索过“赶集网”的用户UID
      • 3.4、通过Rank:点击排名 对数据进行排序
    • 四、参考

一、前言

最近学习大数据的知识,需要做一些有关Hadoop MapReduce的实验
实验内容是在sogou.500w.utf8数据的基础上进行的。
实现以下内容:

  • 1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录
  • 2、统计rank<3并且order>2的所有UID及数量
  • 3、上午7-9点之间,搜索过“赶集网”的用户UID
  • 4、通过Rank:点击排名 对数据进行排序

该实验是在已经搭建好Hadoop集群的基础上进行的,如果还没有搭建,请参考以下文章进行集群搭建

二、准备数据

数据的字段说明
在这里插入图片描述
上传数据
创建目录

hdfs dfs -mkdir /homework

上传文件

hdfs dfs -put -p sogou.500w.utf8 /homework

三、编程实现

3.1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录

1、Mapper

package com.csust.homework1;import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class TaskMapper1 extends Mapper<LongWritable, Text, Text, Text> {Text outputK = new Text();Text outputV = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {String line = value.toString();String[] words = line.split("\t");if (words[2].contains("仙剑奇侠传")) {outputK.set(words[1]);outputV.set(words[2]);context.write(outputK, outputV);}}
}

2、Reduce

package com.csust.homework1;import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class TaskReducer1 extends Reducer<Text,Text,Text,Text> {Text outputV = new Text();@Overrideprotected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {StringBuilder total= new StringBuilder();for (Text value : values) {total.append(value.toString());total.append("\t");}outputV.set(total.toString());context.write(key,outputV);}
}

3、Driver

package com.csust.homework1;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class TaskDriver1 {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {//1:创建一个job任务对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//如果打包运行出错,则需要加该配置job.setJarByClass(TaskDriver1.class);job.setMapperClass(TaskMapper1.class);job.setReducerClass(TaskReducer1.class);//设置Mapper输出job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//设置最终输出类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//设置路径FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/homework"));FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/homework1_out"));//提交任务boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

4、程序运行
在这里插入图片描述

5、运行结果
在这里插入图片描述

3.2、统计rank<3并且order>2的所有UID及数量

1、Mapper

package com.csust.homework2;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class TaskMapper2 extends Mapper<LongWritable, Text, Text, IntWritable> {Text outK = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {String data = value.toString();String[] words = data.split("\t");if (Integer.parseInt(words[3])<3 && Integer.parseInt(words[4])>2) {outK.set(words[1]); context.write(outK, new IntWritable(1)); }}
}

2、Reduce

package com.csust.homework2;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class TaskReducer2 extends Reducer<Text, IntWritable, Text, IntWritable> {@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {int total = 0;for (IntWritable value : values) {total += value.get();}context.write(key,new IntWritable(total));}
}

3、Driver

package com.csust.homework2;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class TaskDriver2 {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {//创建一个job任务对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//如果打包运行出错,则需要加该配置job.setJarByClass(TaskDriver2.class);// 关联Mapper和Reducer的jarjob.setMapperClass(TaskMapper2.class);job.setReducerClass(TaskReducer2.class);//设置Mapper输出的kv类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);//设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);//设置输入和输出路径FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/homework"));FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/homework2_out"));//提交任务boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

4、运行程序
在这里插入图片描述

5、运行结果
在这里插入图片描述

3.3、上午7-9点之间,搜索过“赶集网”的用户UID

1、Mapper

package com.csust.homework3;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class TaskMapper3 extends Mapper<LongWritable, Text, Text, Text> {Text outputK = new Text();Text outputV = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {String line = value.toString();String[] words = line.split("\t");SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");try {Date time = format.parse(words[0]);Calendar calendar = Calendar.getInstance();calendar.setTime(time);int hour = calendar.get(Calendar.HOUR_OF_DAY);if (hour >= 7 && hour < 9) {if (words[2].contains("赶集网")) {//UIDoutputK.set(words[1]);outputV.set(words[2]);context.write(outputK, outputV);}}} catch (ParseException e) {e.printStackTrace();}}
}

2、Reduce

package com.csust.homework3;import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class TaskReducer3 extends Reducer<Text,Text,Text,Text> {Text outputV = new Text();@Overrideprotected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {StringBuilder total= new StringBuilder();for (Text value : values) {total.append(value.toString());total.append("\t");}outputV.set(total.toString());context.write(key,outputV);}
}

3、Driver

package com.csust.homework3;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class TaskDriver3 {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {//创建一个job任务对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//如果打包运行出错,则需要加该配置job.setJarByClass(TaskDriver3.class);// 关联Mapper和Reducer的jarjob.setMapperClass(TaskMapper3.class);job.setReducerClass(TaskReducer3.class);//设置Mapper输出的kv类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);//设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);//设置输入和输出路径FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/homework"));FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/homework3_out"));//提交任务boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

4、 运行程序
在这里插入图片描述

5、运行结果

在这里插入图片描述

3.4、通过Rank:点击排名 对数据进行排序

1、Mapper

package com.csust.sort;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TaskMapper4 extends Mapper<LongWritable, Text, IntWritable, Text> {Text outputV = new Text();@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, IntWritable, Text>.Context context) throws IOException, InterruptedException {String line = value.toString();String[] words = line.split("\t");IntWritable outputK = new IntWritable(Integer.parseInt(words[3])); List<String> list = new ArrayList<String>(Arrays.asList(words));list.remove(3);String data = String.join("\t", list); outputV.set(data);context.write(outputK, outputV); }
}

2、Reduce

package com.csust.sort;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class TaskReducer4 extends Reducer<IntWritable, Text, Text, IntWritable> {@Overrideprotected void reduce(IntWritable key, Iterable<Text> values, Reducer<IntWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {for (Text value : values) {context.write(value, key); }}
}

3、Driver

package com.csust.sort;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;
public class TaskDriver4 {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {//创建一个job任务对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);//如果打包运行出错,则需要加该配置job.setJarByClass(TaskDriver4.class);// 关联Mapper和Reducer的jarjob.setMapperClass(TaskMapper4.class);job.setReducerClass(TaskReducer4.class);//设置Mapper输出的kv类型job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(Text.class);//设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// 设置排序方式job.setSortComparatorClass(MyComparator.class);//设置输入和输出路径FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/homework"));FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/homework4_out"));//提交任务boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

4、Commparator

package com.csust.sort;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.WritableComparable;
public class MyComparator extends IntWritable.Comparator{public int compare(WritableComparable a, WritableComparable b) {return -super.compare(a, b);}public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {return -super.compare(b1, s1, l1, b2, s2, l2);}
}

5、运行程序
在这里插入图片描述

6、 运行结果
在这里插入图片描述

四、参考

集群搭建
MapReduce实现单词统计
MapReduce统计手机号

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java源代码分析与生成

源代码分析&#xff1a;可使用ANTLRANTLR是开源的语法分析器&#xff0c;可以用来构造自己的语言&#xff0c;或者对现有的语言进行语法分析。JavaParser 对Java代码进行分析CodeModel 用于生成Java代码(但对于已有代码的支持可能有问题) 转载于:https://www.cnblogs.com/laoni…

springboot 整合mybatis实现curd

springboot 整合mybatispom文件mvc 架构application.properties 扩展配置&#xff0c;druid配置类项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-mybatis-05pom文件 <!--整合mybatis--><dependency><groupId…

关于android 调用网页隐藏地址栏

首先创建项目&#xff0c;在main.xml里 添加好WebView控件R.id为webview1。 HelloWebView.java 代码 package liu.ming.com; import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebVie…

spring security 认证与权限控制

目录1. 使用授权和认证的必要性2. spring security 与 shiro 与 过滤器&#xff0c;拦截器3. 具体配置使用项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-security-061. 使用授权和认证的必要性 什么是安全框架&#xff0c…

CodeIgniter框架下载辅助函数的一个小bug

if (strpos($_SERVER[HTTP_USER_AGENT], "MSIE") ! FALSE){header(Content-Type: .$mime); // <---1)这里header(Content-Disposition: attachment; filename".$filename.");header(Expires: 0);header(Cache-Control: must-revalidate, post-check0, p…

win7 cmd命令行窗口 宽度无法变大 自由调整大小

偶然遇到了这个问题,百度查到了解决方案,执行一个bat批处理命令. mode con lines40 mode con cols160 color 250 cls cmd转载于:https://www.cnblogs.com/DreamDrive/p/4017970.html

springboot 与shiro整合

shiro~ shiro快速入门springboot 整合shiro核心目标清爽pom用户认证授权认证&#xff0c;与数据库交互shiro configuration核心controller 获取shiro 中的token页面控制功能的隐藏和显示https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-shiro-07sh…

jvm内存设置

摘抄自&#xff1a;http://www.cnblogs.com/fyj218/archive/2011/07/19/2110570.html 在eclipse根目录下打开eclipse.ini&#xff0c;默认内容为&#xff08;这里设置的是运行当前开发工具的JVM内存分配&#xff09;&#xff1a;-vmargs-Xms40m-Xmx256m-vmargs表示以下为虚拟机…

swagger接口文档使用

swagger接口文档一&#xff0c;swagger简介前后端分离swagger 诞生二&#xff0c;springboot集成swagger依赖编写helloworld接口配置swagger > config 配置类测试运行三&#xff0c;配置swaggerswagger 配置扫描接口如何做到只在生产环境中启动swagger&#xff1f;配置api文…

异步任务,邮箱任务,定时任务

task~异步任务邮箱任务定时任务源码下载异步任务 开启多线程&#xff0c;我飞了。 package cn.bitqian.service;import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;/*** 异步任务 目的 多线程&#xff0c;交给spring托…

iframe在ipad safari的显示

今 天要在web中嵌套一个网址或本地HTML&#xff0c;用到了iframe&#xff0c;在电脑上设置scrolling‘auto’&#xff0c;宽度高度&#xff0c;会有滚动条出现。而在 ipad上会全部显示整个网页的宽度高度。scrolling属性无效。原来在html5中的iframe已经只有剩下src的属性。 但…

IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误&#xff0c;该错误简单的说&#xff0c;是由于 "ViewController" 还没有被加载&#xff0c;就调用该 ViewController 或者 ViewController 内的方法时&#xff0c;就会报这个错误。在不同…

maven传递依赖

目录1. 依赖传递2. 什么是依赖冲突3. 怎么解决4. 项目聚合maven是一个项目管理的工具&#xff0c;从项目的构建到项目开发&#xff0c;再到项目的测试&#xff0c;项目上线&#xff0c;都可一键管理。1. 那么&#xff0c;还有maven是如何管理项目中所用到的jar版本冲突&#xf…

使用apache FileUtils下载文件

目录工具代码实现测试工具 <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>或者 https://mvnrepository.com/artifact/commons-io/commons-io/2.7 然后放…

二分图多重匹配

在二分图的最大匹配中&#xff0c;每个点&#xff08;不管是X集合还是Y集合&#xff09;最多只能与一条匹配边相关联&#xff0c; 然而&#xff0c;经常有这种问题&#xff0c;即二分图的一个点可以和多条匹配边相关联&#xff0c;但有上限&#xff0c;即cap[i]表示点i最多能和…

springmvc,spring,hibernate5.0整合

目录1. pom依赖2. web.xml3. spring核心配置文件3.1 jdbc配置信息3.2 sping 配置文件4. 实体映射5. 项目结构5.1 curd5.2 页面6. 测试1. spring版本 5.1.5 RELEASE 2. hibernate版本 5.3.9.Final 3. 数据源使用c3p0项目使用eclipse2017 maven构建, 完成学生的新增&#xff0c;…

MYSQL 查看表上索引的 1 方法

前期准备&#xff1a; create table T9(A int ,B text,C text,fulltext index fix_test_for_T8_B(B));#在定义表的时候加索引 create unique index ix_test_for_T8_A on T9(A);#加朴素索引 create fulltext index fix_test_for_T8_C on T9(C);#加全文索引 --------------------…

springmvc 结合ajax批量新增

目录1. 需要注意的问题2. 页面代码3. controller定义参数接收1. 需要注意的问题 mvc框架的处理日期问题ResponseBody响应对象是自定义对象&#xff0c;响应不是jsonResopnseBody响应自定义对象时&#xff0c;日期为是long类型的数结束数据方法的参数&#xff0c;该如何定义&am…