环境:centos7+hadoop2.5.2

1.使用ECLIPS具打包运行WORDCOUNT实例,统计莎士比亚文集各单词计数(文件SHAKESPEARE.TXT)。

①WorldCount.java 中的main函数修改如下:

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//设置输入文本路径
FileInputFormat.addInputPath(job, new Path("/input"));
//设置mp结果输出路径
FileOutputFormat.setOutputPath(job, new Path("/output/wordcount")); System.exit(job.waitForCompletion(true) ? 0 : 1);
}

②导出WordCount的jar包:
  export->jar file->next->next->Main class里面选择WordCount->Finish。
③使用scp将wc.jar拷贝到node1机器,创建目录:hadoop fs –mkdir /input,将shakespeare.txt上传到hdfs上,运行wc.jar文件:hadoop jar wc.jar
④使用hadoop fs -cat /output/wordcount/part-r-00000 grep|head -n 30 查看前30条输出结果:

2.对于SOGOU_500W_UTF文件,完成下列程序设计内容:

(1) 统计每个用户搜索的关键字总长度

Mapreduce程序:

public class sougou3 {
public static class Sougou3Map extends
Mapper<Object, Text, Text, Text> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
  String line = value.toString();
  String[] vals = line.split("\t");
  String uid = vals[1];
  String search = vals[2];
  context.write(new Text(uid), new Text(search+"|"+search.length()));
}
}
public static class Sougou3Reduce extends
Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
  String result = "";
  for (Text value : values) {
    String strVal = value.toString();
    result += (strVal+" ");
  }
  context.write(new Text(key + "\t"), new Text(result));
  }
  }
}

输出结果:

(2) 统计2011年12月30日1点到2点之间,搜索过的UID有哪些?

Mapreduce程序:

public class sougou1 {

    public static class Sougou1Map extends
Mapper<Object, Text, Text, Text> { public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] vals = line.split("\t");
String time = vals[0];
String uid = vals[1];
//2008-07-10 19:20:00
String formatTime = time.substring(0,4)+"-"+time.substring(4,6)+"-"+time.substring(6,8)+" "
+time.substring(8,10)+":"+time.substring(10,12)+":"+time.substring(12,14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = sdf.parse(formatTime);
Date date1 = sdf.parse("2011-12-30 01:00:00");
Date date2 = sdf.parse("2011-12-30 02:00:00");
//日期在范围区间上
if (date.getTime() > date1.getTime() && date.getTime() < date2.getTime()){
context.write(new Text(uid), new Text(formatTime));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
public static class Sougou1Reduce extends
Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
String result = "";
for (Text value : values) {
result += value.toString()+"|";
}
context.write(key, new Text(result));
}
}
}

输出结果:
左边是用户id,右边分别是时间,以“|”作为分割。

(3) 统计搜索过‘仙剑奇侠’的每个UID搜索该关键词的次数。

Mapreduce程序:

public class sougou2 {
public static class Sougou2Map extends
Mapper<Object, Text, Text, IntWritable> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] vals = line.split("\t");
String uid = vals[1];
String search = vals[2];
if (search.equals("仙剑奇侠")){
context.write(new Text(uid), new IntWritable(1));
}
}
}
public static class Sougou2Reduce extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int result = 0;
for (IntWritable value : values) {
result += value.get();
}
context.write(new Text(key+"\t"), new IntWritable(result));
}
}
}

输出结果:
UID为:6856e6e003a05cc912bfe13ebcea8a04的用户搜索过“仙剑奇侠”共1次。

3.使用MAPREDUCE程序设计实现对文件中下列数据的排序操作78 11 56 87 25 63 19 22 55

Mapreduce程序:

public class Sort {
//map将输入中的value化成IntWritable类型,作为输出的key
public static class Map extends Mapper<Object,Text,IntWritable,NullWritable>{
private static IntWritable data=new IntWritable();
//实现map函数
public void map(Object key,Text value,Context context)
throws IOException,InterruptedException{
String line=value.toString();
data.set(Integer.parseInt(line));
context.write(data, NullWritable.get());
}
} //reduce将输入中的key复制到输出数据的key上,
//然后根据输入的value-list中元素的个数决定key的输出次数
//用全局linenum来代表key的位次
public static class Reduce extends
Reducer<IntWritable,NullWritable,IntWritable,NullWritable>{ //实现reduce函数
public void reduce(IntWritable key,Iterable<NullWritable> values,Context context)
throws IOException,InterruptedException{
for(NullWritable val:values){
context.write(key, NullWritable.get());
}
} } }

输出内容为:

4.学生成绩文件TXT内容(字段用TAB键分隔)如下,使用MAPREDUCE计算每个学生的平均成绩

李平 87 89 98 75
张三 66 78 69 70
李四 96 82 78 90
王五 82 77 74 86
赵六 88 72 81 76

Mapreduce 程序:

public class Score {

    public static class ScoreMap extends
Mapper<Object, Text, Text, NullWritable> { public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
context.write(value, NullWritable.get());
} } public static class ScoreReduce extends
Reducer<Text, NullWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<NullWritable> values,
Context context) throws IOException, InterruptedException {
for (NullWritable nullWritable : values) {
String line = key.toString();
String[] vals = line.split("\t");
String name = vals[0];
int val1 = Integer.parseInt(vals[1]);
int val2 = Integer.parseInt(vals[2]);
int val3 = Integer.parseInt(vals[3]);
int average = (val1 + val2 + val3) / 3;
context.write(new Text(name), new IntWritable(average));
}
}
}
}

输出结果为

相关资料:

链接:http://pan.baidu.com/s/1dFD7mdr 密码:xwu8

大数据(1):基于sogou.500w.utf8数据的MapReduce程序设计的更多相关文章

  1. 大数据(2):基于sogou.500w.utf8数据hive的实践

    一.环境的搭建 1.安装配置mysql rpm –ivh MySQL-server-5.6.14.rpm rpm –ivh MySQL-client-5.6.14.rpm 启动mysql 创建hive ...

  2. 大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践

    1. HBase安装部署操作 a) 解压HBase安装包tar –zxvf hbase-0.98.0-hadoop2-bin.tar.gzb) 修改环境变量 hbase-env.shexport JA ...

  3. 大数据实时处理-基于Spark的大数据实时处理及应用技术培训

    随着互联网.移动互联网和物联网的发展,我们已经切实地迎来了一个大数据 的时代.大数据是指无法在一定时间内用常规软件工具对其内容进行抓取.管理和处理的数据集合,对大数据的分析已经成为一个非常重要且紧迫的 ...

  4. 大数据下基于Tensorflow框架的深度学习示例教程

    近几年,信息时代的快速发展产生了海量数据,诞生了无数前沿的大数据技术与应用.在当今大数据时代的产业界,商业决策日益基于数据的分析作出.当数据膨胀到一定规模时,基于机器学习对海量复杂数据的分析更能产生较 ...

  5. 【T-BABY 夜谈大数据】基于内容的推荐算法

    这个系列主要也是自己最近在研究大数据方向,所以边研究.开发也边整理相关的资料.网上的资料经常是碎片式的,如果要完整的看完可能需要同时看好几篇文章,所以我希望有兴趣的人能够更轻松和快速地学习相关的知识. ...

  6. SpringMVC + ehcache( ehcache-spring-annotations)基于注解的服务器端数据缓存

    背景 声明,如果你不关心java缓存解决方案的全貌,只是急着解决问题,请略过背景部分. 在互联网应用中,由于并发量比传统的企业级应用会高出很多,所以处理大并发的问题就显得尤为重要.在硬件资源一定的情况 ...

  7. 基于IBM Bluemix的数据缓存应用实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:IBM® Data Cache for Bluemix 是快速缓存服务.支持 Web 和 ...

  8. 使用C#处理基于比特流的数据

    使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...

  9. 微软开放技术(中国)携 CKAN 和 OData 技术引入基于 Azure 的开放数据平台

    今天,微软开放技术(中国)通过微软公有云Azure引入一个全新的开放数据平台,该平台基于开源数据门户解决方案 CKAN,以及由微软开放技术(中国)特别针对中国市场优化的ODATA插件来增强其国际化和本 ...

随机推荐

  1. PHP秒杀系统全方位设计(二)

    商品页面开发 静态化展示页面[效率要比动态PHP高很多,PHP程序需要解析等步骤,本身就需要很多流程,整个下来PHP的处理花的时间和资源要多] 商品状态的控制 开始前.进行中.库存不足.结束 数据逻辑 ...

  2. 【BZOJ1565】 植物大战僵尸

    Description Input Output 仅包含一个整数,表示可以获得的最大能源收入.注意,你也可以选择不进行任何攻击,这样能源收入为0. Sample Input 3 2 10 0 20 0 ...

  3. 网卡驱动引起openstack的mtu问题

    一套Pike版本的openstack测试环境,使用vlan模式的网络,数据网网卡使用的是绿联的usb百兆网卡,遇到了虚拟机网络异常的问题.同一个vlan下,不同宿主机上的两台虚拟机,相互之间可以pin ...

  4. Ubuntu上搭建Hadoop环境(单机模式+伪分布模式)

    首先要了解一下Hadoop的运行模式: 单机模式(standalone)        单机模式是Hadoop的默认模式.当首次解压Hadoop的源码包时,Hadoop无法了解硬件安装环境,便保守地选 ...

  5. 文件无法复制的原因-IT33

    Win7系统复制数据至其他硬盘或者是移动存储设备是,有时会发生无法复制文件过大的情况.这里先大致介绍一下硬盘文件系统分为NFTS格式和FAT32格式这两种,其中FAT32仅支持单次移动4G以下容量的数 ...

  6. Netty的并发编程实践5:不要依赖线程优先级

    当有多个线程同时运行的时候,由线程调度器来决定哪些线程运行.哪些等待以及线程切换的时间点,由于各个操作系统的线程调度器实现大相径庭,因此,依赖JDK自带的线程优先级来设置线程优先级策略的方法是错误和非 ...

  7. freemarker写select组件(三)

    freemarker写select组件 1.宏定义 <#macro select id datas value="" key="" text=" ...

  8. Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ExpressionParser

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  9. java.sql.SQLException:No suitable driver found for http://localhost:3306/school

    1.错误描述 java.sql.SQLException:No suitable driver found for http://localhost:3306/school 2.错误原因 Class. ...

  10. MySQL语法大全整理的自学笔记

    select * from emp; #注释 #--------------------------- #----命令行连接MySql--------- #启动mysql服务器 net start m ...