ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:631.96KB ,
资源ID:10974186      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/10974186.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(hadoop文本词频排序实验报告.docx)为本站上传会员【a199****6536】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

hadoop文本词频排序实验报告.docx

1、hadoop文本词频排序实验报告 大数据技术概论实验报告 文 本 词 频 排 序 姓名: 郭利强 专业: 工程管理专业 学号: 2015E8009064028 1. 实验要求 3 2. 环境说明 3 2.1 系统硬件 3 2.2 系统软件 3 2.3 安装及配置 3 3. 实验设计 10 3.1 设计思路 10 3.2 算法设计 10 3.3 程序

2、和类的设计 11 4. 程序代码 16 4.1 WordCount.java代码 16 4.2 Pair.java代码 19 5. 实验输入和结果 20 5.1 实验输入 20 5.2 实验输出 21 5.3 实验结果分析 23 1. 实验要求 在Eclipse环境下编写WordCount程序,统计所有出现次数k次以上的单词计数,最后的结果按照词频从高到低排序输出。 2. 环境说明 2.1 系统硬件 处理器:Intel Core i3-2350M CPU@2.3GHz×4 内存:2GB 磁盘:60GB 2.2 系统软件 操作系统:Ub

3、untu 14.04 LTS 操作系统类型:32位 Java版本:1.7.0_85 Eclipse版本:3.8 Hadoop插件:hadoop-eclipse-plugin-2.6.0.jar Hadoop:2.6.1 2.3 安装及配置 1.Hadoop配置 1)core-site.xml hadoop.tmp.dir Abase for other te

4、mporary directories. fs.defaultFS hdfs://inspiron:9000 2)hdfs-site.xml dfs.replication 1

5、 dfs.namenode.name.dir dfs.datanode.data.dir dfs.namenode.secondary.http-address 127.

6、0.0.1:50090 The secondary namenode http server address and port. dfs.webhdfs.enabled true Enable WebHDFS (REST API) in Namenodes and Datanodes.

7、ty> 3)maprd-site.xml mapreduce.framework.name yarn mapreduce.jobhistory.address 127.0.0.1:10020 MapReduce JobHistory Se

8、rver IPC host:port mapreduce.jobhistory.webapp.address 127.0.0.1:19888 MapReduce JobHistory Server Web UI host:port mapreduce.jobtracker.http.address

9、lue>127.0.0.1:50030 The job tracker http server address and port the server will listen on. If the port is 0 then the server will start on a free port. 4)yarn-site.xml

10、ption>The hostname of the RM. yarn.resourcemanager.hostname inspiron yarn.nodemanager.aux-services mapreduce_shuffle yarn.nodemanag

11、er.aux-services.mapreduce_shuffle.class org.apache.hadoop.mapred.ShuffleHandler The address of the applications manager interface in the RM. yarn.resourcemanager.address inspiron:803

12、2 The address of the scheduler interface. yarn.resourcemanager.scheduler.address inspiron:8030 yarn.resourcemanager.resource-tracker.address

13、lue>inspiron:8031 The class to use as the resource scheduler. yarn.resourcemanager.scheduler.class org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler

14、y> 5)slaves inspiron 2.eclipse配置 1)安装hadoop开发插件。由于本次实验所使用的hadoop版本较新,编译过程中出现问题太多,所以直接使用了官方发布的2.6.0版本的插件,经过测试可以正常使用。将插件复制至eclipse安装目录下的plugins目录下。 2)进入eclipse->window->preferences配置hadoop安装路径 3.新建Hadoop Location 4.配置完成后在Project Explore及Map/Reduce Location窗口可看到如下界面

15、 3. 实验设计 3.1 设计思路 利用MapReduce框架设计,在Map过程将输入文本拆分成单个的单词,并对单词进行初步统计,将单词及词频组合作为Map过程输出的value值,将Map过程的Key值设为统一固定值。在Reduce过程获取Map过程的输出,拆分value值,获取并汇总统计出所有单词的词频,根据设定值k对统计单词进行筛选,将词频高于设定值k的单词和词频以键值对的形式存入某个容器中,然后将容器的对象按照词频从高到低的顺序排序后以单词和词频键值对的形式输出。如此设计,只需要一个MapReduce过程即可完成词频统计并筛选排序输出。

16、3.2 算法设计 1.在Map过程中,重写map类,利用StringTokenizer类,将map方法中的value值中存储的文本,拆分成一个个的单词,将单词进行初步统计,统计得到的结果存入一个Map集合中。遍历Map集合,将所得单词和词频组成一个字符串,作为Map过程输出的value值,并以形式输出。 2.在Reduce过程中,重写setup方法,获取设定词频。 3.对Map过程输出的形式的键值对,遍历values,拆分并统计出对应单词的词频,以键值对的形式装入一个Map集合中。 4.遍

17、历存有单词和词频键值对的Map集合,将其中词频大于设定值k的单词和词频存入一个List集合中。 5.利用Collect.sort()重载方法对List集合进行按照词频由高到低顺序的排序。 6.遍历List集合,将经过排序的List集合中存储的单词和词频写入reduce方法的context变量,以单词和词频键值对的形式输出。 3.3 程序和类的设计 1.定义TokenizerMapper类继承org.apache.hadoop.mapreduce包中Mapper类,并重写map方法。然后利用StringTokenizer类,将map方法中的value值中存储的文本,拆分成一

18、个个的单词,进行初步统计后放入Map集合中,遍历Map集合取出单词及对应词频,将单词和词频组合后,以的形式作为map方法的结果输出,其余的工作都交由MapReduce框架处理。 public static class TokenizerMapper extends Mapper { private final static Text mapValue = new Text(); private Text mapKey = new Text("key"); pu

19、blic void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); Map word2count = new HashMap(); while (itr.hasMoreTokens()) { String nextToken =remov

20、eNonLetters( itr.nextToken()); if (!word2count.containsKey(nextToken)) word2count.put(nextToken, 0); word2count.put(nextToken, word2count.get(nextToken) + 1); } for (Entry entry : word2count.entrySet()) { mapValue.set(entry.getKey() + "\001" + entry.getV

21、alue()); context.write(mapKey, mapValue); } } //去除拆分后字符串中所含非字母字符 public static String removeNonLetters(String original){ StringBuffer aBuffer=new StringBuffer(original.length()); char aCharacter; for(int i=0;i

22、 if(Character.isLetter(aCharacter)){ aBuffer.append(aCharacter); } } return new String(aBuffer); } } 2.定义IntSumReducer类继承org.apache.hadoop.mapreduce包中Reducer类,对Map过程中发送过来的键值对,拆分value值取出单词及对应词频,进行词频统计,筛选出词频高于设定值的单词,并按照词频从高到低的顺序排序后输出。 public static class IntSumReducer ex

23、tends Reducer { private IntWritable outputValue = new IntWritable(); private Text outputKey = new Text(); private int k = 0; @Override protected void setup( Reducer.Context context) throws IOException, Inter

24、ruptedException { super.setup(context); this.k = Integer.parseInt(context.getConfiguration().get("k")); } public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { Map word2count = new HashMap

25、eger>(); for (Text val : values) { String valStr = val.toString(); String[] records = valStr.split("\001"); String word = records[0]; int cnt = Integer.parseInt(records[1]); if (!word2count.containsKey(word)) word2count.put(word, 0); word2count.put(word, wor

26、d2count.get(word) + cnt); } List list = new ArrayList(); for (Map.Entry entry : word2count.entrySet()) { if (entry.getValue() > this.k) { Pair p = new Pair(entry.getKey(), entry.getValue()); list.add(p); } } Collections.sort(l

27、ist, new Comparator() { @Override public int compare(Pair o1, Pair o2) { return o2.getV().compareTo(o1.getV()); } }); for (Pair p : list) { outputKey.set(p.getK()); outputValue.set(p.getV()); context.write(outputKey, outputValue); } }

28、} 1)重写setup方法,获取设定词频,并将其值赋给已声明的变量k。 protected void setup( Reducer.Context context) throws IOException, InterruptedException { super.setup(context); this.k = Integer.parseInt(context.getConfiguration().get("k")); } 2)Map过程输出

29、>中key为统一设定值,舍去不用,而values是[word+split+count]形式的集合,重写reduce方法,遍历values按照设定分隔符拆分后,汇总进行统计,得到某个单词的词频。声明一个Map变量word2count,将统计得到的单词及其词频,以键值对的形式存入word2count中。 Map word2count = new HashMap(); for (Text val : values) { String valStr = val.toString(); String[]

30、 records = valStr.split("\001"); String word = records[0]; int cnt = Integer.parseInt(records[1]); if (!word2count.containsKey(word)) word2count.put(word, 0); word2count.put(word, word2count.get(word) + cnt); } 3)声明一个List集合变量list,遍历word2count,根据词频进行筛选,用词频大于k的单词和词频的值

31、初始化新定义的类Pair的对象,然后将对象存入list中。使用Collect.sort()重载方法对list进行排序。 List list = new ArrayList(); for (Map.Entry entry : word2count.entrySet()) { if (entry.getValue() > this.k) { Pair p = new Pair(entry.getKey(), entry.getValue()); list.add(p); } }

32、 Collections.sort(list, new Comparator() { @Override public int compare(Pair o1, Pair o2) { return o2.getV().compareTo(o1.getV()); } }); 4)遍历list,将经过排序的集合中存储的单词和词频写入reduce方法的context变量 for (Pair p : list) { outputKey.set(p.getK()); outputValue.

33、set(p.getV()); context.write(outputKey, outputValue); } 3.主方法main,定义Job对象负责管理和运行一个计算任务,并通过Job的一些方法对任务的参数进行相关的设置。 public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String master="127.0.0.1"; conf.set("fs.defaultFS", "hdfs://12

34、7.0.0.1:9000"); conf.set("hadoop.job.user", "hadoop"); conf.set("mapreduce.framework.name","yarn"); conf.set("yarn.resourcemanager.address", master+":8032"); conf.set("yarn.resourcemanager.scheduler.address", master+":8030"); conf.set("mapred.jar","wordcount.jar"); S

35、tring[] otherArgs = new GenericOptionsParser(conf, args) .getRemainingArgs(); if (otherArgs.length < 3) { System.err.println("Usage: wordcount [...] "); System.exit(2); } Job job = new Job(conf, "word count"); job.setMapperClass(TokenizerMapper.class); job.s

36、etReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); // 获取设定值K for (int i = 0; i < otherArgs.length - 2; ++i) { (job, new Path(otherArgs[i])); } job.getConfiguration().set("k", otherArgs[otherArgs.length - 1]); (job, ne

37、w Path( otherArgs[otherArgs.length - 2])); System.exit(job.waitForCompletion(true) ? 0 : 1); } 1)定义Configuration对象conf,设置配置信息后,使用conf对象初始化Job对象。 Configuration conf = new Configuration(); String master="127.0.0.1"; conf.set("fs.defaultFS", "hdfs://127.0.0.1:9000"); conf.s

38、et("hadoop.job.user", "hadoop"); conf.set("mapreduce.framework.name","yarn"); conf.set("yarn.resourcemanager.address", master+":8032"); conf.set("yarn.resourcemanager.scheduler.address", master+":8030"); conf.set("mapreduce.jobhistory.address", master+":10020"); conf.se

39、t("mapred.jar","wordcount.jar"); String[] otherArgs = new GenericOptionsParser(conf, args) .getRemainingArgs(); if (otherArgs.length < 3) { System.err.println("Usage: wordcount [...] "); System.exit(2); } Job job = new Job(conf, "word count"); 2)调用setMapperC

40、lass(TokenizerMapper.class)方法设置TokenizerMapper.class作为map过程处理类,调用setReducerClass(IntSumReducer.class)方法设置IntSumReducer.class作为reduce过程处理类。 job.setMapperClass(TokenizerMapper.class); job.setReducerClass(IntSumReducer.class); 3)调用setOutputKeyClass(Text.class)方法和setOutputValueClass(Text.class)方法设

41、置Job输出结果数据类型。 job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); 4)调用job.getConfiguration().set()方法获取词频设定参数k job.getConfiguration().set("k", otherArgs[otherArgs.length - 1]); 5)调用()方法和()分别设置获取并设置输入输出路径。 (job, new Path(otherArgs[i])); (job, new Path( otherArgs[otherAr

42、gs.length - 2])); 6)调用job.waitForCompletion()方法执行任务 System.exit(job.waitForCompletion(true) ? 0 : 1); 4. 程序代码 4.1 WordCount.java代码 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this

43、 work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this in compliance * with the License. You may obtain a copy of the License at * * * * Unless require

44、d by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the L

45、icense. */ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; import org.apache.hadoop.co

46、nf.Configuration; 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.lib.input.; import org.apache.hadoop.m

47、apreduce.lib.output.; import org.apache.hadoop.util.GenericOptionsParser; import org.apache.hadoop.fs.Path; public class WordCount { public static class TokenizerMapper extends Mapper { private final static Text mapValue = new Text(); private Text mapKe

48、y = new Text("key"); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); Map word2count = new HashMap(); while (itr.hasMoreTokens()) {

49、 String nextToken =removeNonLetters( itr.nextToken()); if (!word2count.containsKey(nextToken)) word2count.put(nextToken, 0); word2count.put(nextToken, word2count.get(nextToken) + 1); } for (Entry entry : word2count.entrySet()) { mapValue.set(entry.ge

50、tKey() + "\001" + entry.getValue()); context.write(mapKey, mapValue); } } public static String removeNonLetters(String original){ StringBuffer aBuffer=new StringBuffer(original.length()); char aCharacter; for(int i=0;i

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服