当前位置: 首页 > news >正文

网站建设教程实训心得培训网站推荐

网站建设教程实训心得,培训网站推荐,cms 网站,网页开发三件套文章目录 📚实验目的📚实验平台📚实验内容🐇在本地编写程序和调试🥕代码框架思路🥕代码实现 🐇在集群上提交作业并执行🥕在集群上提交作业并执行,同本地执行相比即需修改…

文章目录

  • 📚实验目的
  • 📚实验平台
  • 📚实验内容
    • 🐇在本地编写程序和调试
      • 🥕代码框架思路
      • 🥕代码实现
    • 🐇在集群上提交作业并执行
      • 🥕在集群上提交作业并执行,同本地执行相比即需修改路径。
      • 🥕修改后通过expoet,导出jar包,关注 Main-Class 的设置!
      • 🥕在终端依次输入以下指令,完成提交

📚实验目的

倒排索引(Inverted Index)被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射,是目前几乎所有支持全文索引的搜索引擎都需要依赖的一个数据结构。通过对倒排索引的编程实现,熟练掌握 MapReduce 程序在集群上的提交与执行过程,加深对 MapReduce 编程框架的理解。

📚实验平台

  1. 操作系统:Linux
  2. Hadoop 版本:3.2.2
  3. JDK 版本:1.8
  4. Java IDE:Eclipse

📚实验内容

关于倒排索引
在这里插入图片描述

🐇在本地编写程序和调试

在本地 eclipse 上编写带词频属性的对英文文档的文档倒排索引程序,要求程序能够实现对 stop-words(如 a,an,the,in,of 等词)的去除,能够统计单词在每篇文档中出现的频率。文档数据和停词表可在此链接上下载,在伪分布式环境下完成程序的编写和调试。

在这里插入图片描述

🥕代码框架思路

  • Map():对输入的Text切分为多个word。这里的Map()包含setup()map()。每一次map都伴随着一次setup,进行停词,筛选那些不需要统计的。
  • Combine():将Map输出的中间结果相同key部分的value累加,减少向Reduce节点传输的数据量。
  • Partition():为了将同一个word的键值对发送到同一个Reduce节点,对key进行临时处理。将原key的(word, filename)临时拆开,使Partitioner只按照word值进行选择Reduce节点。基于哈希值的分片方法。
  • Reduce():利用每个Reducer接收到的键值对中,word是排好序的,来进行最后的整合。将word#filename拆分开,将filename与累加和拼到一起,存在str中。每次比较当前的word和上一次的word是否相同,若相同则将filename和累加和附加到str中,否则输出:key:word,value:str,并将新的word作为key继续。
  • 上述reduce()只会在遇到新word时,处理并输出前一个word,故对于最后一个word还需要额外的处理。重载cleanup(),处理最后一个word并输出
    在这里插入图片描述

在这里插入图片描述倒排索引的Map、Combiner、Partitioner部分就和上图一样

  • 一个Map对应一个Combiner,借助Combiner对Map输出进行一次初始整合
  • 一个Combiner又对应一个Partitioner,Partitioner将同一个word的键值对发送到同一个Reduce节点

🥕代码实现

(关注本地路径)

package index;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;public class index
{public static class Map extends Mapper<Object, Text, Text, IntWritable> {/*** setup():读取停词表到vector stop_words中*/Vector<String> stop_words;//停词表protected void setup(Context context) throws IOException {stop_words = new Vector<String>();//初始化停词表Configuration conf = context.getConfiguration();//读取停词表文件BufferedReader reader = new BufferedReader(new InputStreamReader(FileSystem.get(conf).open(new Path("hdfs://localhost:9000/user/hadoop/input/stop_words_eng.txt"))));String line;while ((line = reader.readLine()) != null) {//按行处理StringTokenizer itr=new StringTokenizer(line);while(itr.hasMoreTokens()){//遍历词,存入vectorstop_words.add(itr.nextToken());}}reader.close();}/*** map():对输入的Text切分为多个word* 输入:key:当前行偏移位置     value:当前行内容* 输出:key:word#filename    value:1*/protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {FileSplit fileSplit = (FileSplit) context.getInputSplit();String fileName = fileSplit.getPath().getName();//获取文件名,转换为小写String line = value.toString().toLowerCase();//将行内容全部转为小写字母//只保留数字和字母String new_line="";for(int i = 0; i < line.length(); i ++) {if((line.charAt(i)>=48 && line.charAt(i)<=57) || (line.charAt(i)>=97 && line.charAt(i)<=122)) {//按行处理new_line += line.charAt(i);} else {//其他字符保存为空格new_line +=" ";}}line = new_line.trim();//去掉开头和结尾的空格StringTokenizer strToken=new StringTokenizer(line);//按照空格拆分while(strToken.hasMoreTokens()){String str = strToken.nextToken();if(!stop_words.contains(str)) {//不是停词则输出key-value对context.write(new Text(str+"#"+fileName), new IntWritable(1));}}}}public static class Combine extends Reducer<Text, IntWritable, Text, IntWritable> {/*** 将Map输出的中间结果相同key部分的value累加,减少向Reduce节点传输的数据量* 输入:key:word#filename    value:1* 输出:key:word#filename    value:累加和(词频)*/protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum ++;}context.write(key, new IntWritable(sum));}}public static class Partition extends HashPartitioner<Text, IntWritable> {/*** 为了将同一个word的键值对发送到同一个Reduce节点,对key进行临时处理* 将原key的(word, filename)临时拆开,使Partitioner只按照word值进行选择Reduce节点* 基于哈希值的分片方法*/public int getPartition(Text key, IntWritable value, int numReduceTasks) {//第三个参数numPartitions表示每个Mapper的分片数,也就是Reducer的个数String term = key.toString().split("#")[0];//获取word#filename中的wordreturn super.getPartition(new Text(term), value, numReduceTasks);//按照word分配reduce节点       }}public static class Reduce extends Reducer<Text, IntWritable, Text, Text> {/*** Reduce():利用每个Reducer接收到的键值对中,word是排好序的,来进行最后的整合* 将word#filename拆分开,将filename与累加和拼到一起,存在str中* 每次比较当前的word和上一次的word是否相同,若相同则将filename和累加和附加到str中,否则输出:key:word,value:str,并将新的word作为key继续* 输入:*         key                  value*    word1#filename 1        [num1,num2,...]*    word1#filename 2        [num1,num2,...]*    word2#filename 1        [num1,num2,...]* 输出:*    key:word   value:<filename1,词频><filename2,词频>...<total,总词频>*/private String lastfile = null;//存储上一个filenameprivate String lastword = null;//存储上一个wordprivate String str = "";//存储要输出的value内容private int count = 0;private int totalcount = 0;protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {String[] tokens = key.toString().split("#");//将word和filename存在tokens数组中if(lastword == null) {lastword = tokens[0];}if(lastfile == null) {lastfile = tokens[1];}if (!tokens[0].equals(lastword)) {//此次word与上次不一样,则将上次的word进行处理并输出str += "<"+lastfile+","+count+">;<total,"+totalcount+">.";context.write(new Text(lastword), new Text(str));//value部分拼接后输出lastword = tokens[0];//更新wordlastfile = tokens[1];//更新filenamecount = 0;str="";for (IntWritable val : values) {//累加相同word和filename中出现次数count += val.get();//转为int}totalcount = count;return;}if(!tokens[1].equals(lastfile)) {//新的文档str += "<"+lastfile+","+count+">;";lastfile = tokens[1];//更新文档名count = 0;//重设count值for (IntWritable value : values){//计数count += value.get();//转为int}totalcount += count;return;}//其他情况,只计算总数即可for (IntWritable val : values) {count += val.get();totalcount += val.get();}}/*** 上述reduce()只会在遇到新word时,处理并输出前一个word,故对于最后一个word还需要额外的处理* 重载cleanup(),处理最后一个word并输出*/public void cleanup(Context context) throws IOException, InterruptedException {str += "<"+lastfile+","+count+">;<total,"+totalcount+">.";context.write(new Text(lastword), new Text(str));super.cleanup(context);}}public static void main(String args[]) throws Exception {Configuration conf = new Configuration();conf.set("fs.defaultFS", "hdfs://localhost:9000");if(args.length != 2) {System.err.println("Usage: Relation <in> <out>");System.exit(2);}Job job = Job.getInstance(conf, "InvertedIndex");//设置环境参数job.setJarByClass(index.class);//设置整个程序的类名job.setMapperClass(Map.class);//设置Mapper类job.setCombinerClass(Combine.class);//设置combiner类job.setPartitionerClass(Partition.class);//设置Partitioner类job.setReducerClass(Reduce.class);//设置reducer类job.setOutputKeyClass(Text.class);//设置Mapper输出key类型job.setOutputValueClass(IntWritable.class);//设置Mapper输出value类型FileInputFormat.addInputPath(job, new Path(args[0]));//输入文件目录FileOutputFormat.setOutputPath(job, new Path(args[1]));//输出文件目录System.exit(job.waitForCompletion(true) ? 0 : 1);//参数true表示检查并打印 Job 和 Task 的运行状况}}

补充:当我们新建一个Package和Class后运行时,可能会出现如下报错(主要是在MapReduce编程输入输出里会遇到)
在这里插入图片描述
解决办法

  • “Run As”选中“Run Configurations…”

在这里插入图片描述

  • 然后在“Arguments”里输入input output,然后再run就行了。

在这里插入图片描述


🐇在集群上提交作业并执行

集群的服务器地址为 10.102.0.198,用户主目录为/home/用户名,hdfs 目录为/user/用户名。集群上的实验文档存放目录为 hdfs://10.102.0.198:9000/input/. 英文停词表文件存放位置为hdfs://10.102.0.198:9000/stop_words/stop_words_eng.txt。

🥕在集群上提交作业并执行,同本地执行相比即需修改路径。

在这里插入图片描述

🥕修改后通过expoet,导出jar包,关注 Main-Class 的设置!

  • 选中index.java右键Export。
    在这里插入图片描述
  • 如下图选中JAR file后点Next。
    在这里插入图片描述
  • 确认选中index及其src,JAR的命名要和class名一样,比如这里是index.java,就是class index,也就是index.jar。然后点Next。
    在这里插入图片描述
  • 到如下页面,再点Next。

在这里插入图片描述

  • Main class那点Browse,选中index。

在这里插入图片描述

  • 如下图。
    在这里插入图片描述
  • 最后点finish完成导出,可在文件夹里找到index.jar。双击index.jar,在它的METS-INT里头查看Main-Class是否设置成功。
    在这里插入图片描述
    在这里插入图片描述

🥕在终端依次输入以下指令,完成提交

  • 使用 scp InvertedIndex.jar 用户名@10.102.0.198:/home/用户名 命令将本地程序提交到 Hadoop 集群
  • 通过 ssh 用户名@10.102.0.198 命令远程登录到 Hadoop 集群进行操作;
  • 使用 hadoop jar InvertedIndex.jar /input /user/用户名/output 命令在集群上运行 Hadoop 作业,指定输出目录为自己 hdfs 目录下的 output。
  • 使用 diff 命令判断自己的输出结果与标准输出的差异
scp index.jar bigdata_学号@10.102.0.198:/home/bigdata_学号
ssh bigdata_学号@10.102.0.198
hadoop jar index.jar /input /user/bigdata_学号/output
diff <(hdfs dfs -cat /output/part-r-00000) <(hdfs dfs -cat /user/bigdata_学号/output/part-r-00000)

在浏览器中打开 http://10.102.0.198:8088,可以查看集群上作业的基本执行情况。

在这里插入图片描述


文章转载自:
http://rosedrop.qkxt.cn
http://trockenbeerenauslese.qkxt.cn
http://acanthocephalan.qkxt.cn
http://maker.qkxt.cn
http://recessional.qkxt.cn
http://carnificial.qkxt.cn
http://rosetta.qkxt.cn
http://nonreproductive.qkxt.cn
http://catercornered.qkxt.cn
http://malty.qkxt.cn
http://fichu.qkxt.cn
http://irgun.qkxt.cn
http://generalized.qkxt.cn
http://cram.qkxt.cn
http://sale.qkxt.cn
http://thromboxane.qkxt.cn
http://unholy.qkxt.cn
http://pouch.qkxt.cn
http://superannuated.qkxt.cn
http://epicureanism.qkxt.cn
http://azov.qkxt.cn
http://decametre.qkxt.cn
http://de.qkxt.cn
http://seed.qkxt.cn
http://tridymite.qkxt.cn
http://curacoa.qkxt.cn
http://nicotian.qkxt.cn
http://morphophoneme.qkxt.cn
http://ramp.qkxt.cn
http://socker.qkxt.cn
http://midwinter.qkxt.cn
http://sf.qkxt.cn
http://glasswort.qkxt.cn
http://inexpectant.qkxt.cn
http://camelback.qkxt.cn
http://triternate.qkxt.cn
http://enwomb.qkxt.cn
http://eglestonite.qkxt.cn
http://coralliferous.qkxt.cn
http://lacertilian.qkxt.cn
http://cysticercosis.qkxt.cn
http://manoeuvrable.qkxt.cn
http://aiwa.qkxt.cn
http://gi.qkxt.cn
http://coly.qkxt.cn
http://savant.qkxt.cn
http://ipc.qkxt.cn
http://tft.qkxt.cn
http://suprahepatic.qkxt.cn
http://slowish.qkxt.cn
http://knell.qkxt.cn
http://tapescript.qkxt.cn
http://outrow.qkxt.cn
http://barrel.qkxt.cn
http://juke.qkxt.cn
http://chloroethene.qkxt.cn
http://psid.qkxt.cn
http://sdmi.qkxt.cn
http://mercapto.qkxt.cn
http://jordanian.qkxt.cn
http://silicide.qkxt.cn
http://sprowsie.qkxt.cn
http://fertiliser.qkxt.cn
http://hamshackle.qkxt.cn
http://creditiste.qkxt.cn
http://shipment.qkxt.cn
http://inutterable.qkxt.cn
http://ungracious.qkxt.cn
http://feudist.qkxt.cn
http://nonobjective.qkxt.cn
http://incohesive.qkxt.cn
http://pseudomonas.qkxt.cn
http://constitutor.qkxt.cn
http://utopism.qkxt.cn
http://legislatress.qkxt.cn
http://impolicy.qkxt.cn
http://discommodious.qkxt.cn
http://devisor.qkxt.cn
http://uniplanar.qkxt.cn
http://kinda.qkxt.cn
http://voidable.qkxt.cn
http://eurygnathous.qkxt.cn
http://boojum.qkxt.cn
http://withstand.qkxt.cn
http://intraparty.qkxt.cn
http://amygdalate.qkxt.cn
http://vocation.qkxt.cn
http://hexahydric.qkxt.cn
http://midlothian.qkxt.cn
http://nocuousness.qkxt.cn
http://euryhygric.qkxt.cn
http://lachrymal.qkxt.cn
http://subindex.qkxt.cn
http://dat.qkxt.cn
http://cudbear.qkxt.cn
http://lather.qkxt.cn
http://diabetologist.qkxt.cn
http://privily.qkxt.cn
http://microscale.qkxt.cn
http://circumstance.qkxt.cn
http://www.dt0577.cn/news/115636.html

相关文章:

  • 个人网站设计论文模板合肥百度推广公司哪家好
  • 网站维护工作的基本内容google免费入口
  • 网站建设com合肥做网站的公司有哪些
  • 网页搜索关键词seo网站诊断价格
  • 网上接单做网站微信指数怎么看
  • 大型 网站 建设 公司许昌正规网站优化公司
  • 南开大学 网站开发技术 刘冲关键词优化的策略有哪些
  • 网站检测器临沂色度广告有限公司
  • 网站建设中代码seo全网营销
  • 网站中全景是怎么做的高报师培训机构排名
  • 关于公司网站的建设的问卷百度快照怎么没有了
  • 网站建设系统全球网站排名前100
  • 手机wap网站如何建设千万不要去电商公司上班
  • 网站建好了 如何推广网站推广途径
  • 廊坊手机网站网站推广优化c重庆
  • 专用车网站建设哪家专业网络营销策略的内容
  • 黄山网站建设免费咨询线下推广方案
  • 江苏省苏州市相城区最新疫情需要优化的网站有哪些
  • wordpress怎么修改关键字标题优化方法
  • wordpress的文章写好后无法访问自己怎么优化网站
  • wordpress主题赚钱湖南优化公司
  • 怎样做 网站的快捷链接百度问答兼职怎么做
  • 网站的风格分析宁波专业seo服务
  • 做动图的网站免费推广网站2023mmm
  • 卢松松的网站seo网络推广方法
  • 做网站爬闪网站建站系统
  • 一级a做爰片免费网站 新闻现在百度推广有用吗
  • 集团微信网站方案策划直播:韩国vs加纳直播
  • 做网站什么费用百度seo关键词排名技术
  • 做公司网站的公司市场营销策划方案案例