栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

educoder-HDFS和MapReduce综合实训

educoder-HDFS和MapReduce综合实训

第1关:WordCount 词频统计
import java.io.IOException;
import java.util.StringTokenizer;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {
      
  public static class TokenizerMapper 
       extends Mapper{
    
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();//Text实现了BinaryComparable类,可以作为key值

      

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
    

         

    //
    
    StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    

    }
  }

  public static class IntSumReducer 
       extends Reducer {
    private IntWritable result = new IntWritable();

     
    public void reduce(Text key, Iterable values, 
                       Context context
                       ) throws IOException, InterruptedException {
    //
    
    int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
    

    //

    
    result.set(sum);
    context.write(key, result);
    
    }
}
  public static void main(String[] args) throws Exception {
        
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    
    if (otherArgs.length != 2) {
         System.err.println("Usage: wordcount  ");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");//Job(Configuration conf,String jobName)设置job名称
    job.setJarByClass(WordCount.class);//为job设置Mapper类
      
      //
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);//为job设置Combiner类
      //
    job.setReducerClass(IntSumReducer.class);
      //
    job.setOutputKeyClass(Text.class);
      //
    job.setOutputValueClass(IntWritable.class);
      
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//为map-reduce任务设置InputFormat实现类,设置输入路径
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为map-reduce任务设置OutputFormat实现类,设置输出路径
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

第 2 关:HDFS 文件读写
import java.io.IOException;
import java.sql.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class hdfs {
    public static void main(String[] args) throws IOException {
//throws IOException捕获异常声明

//

        Configuration conf = new Configuration();  //实例化设置文件,configuration类实现hadoop各模块之间值的传递
        FileSystem fs = FileSystem.get(conf);  //是hadoop访问系统的抽象类,获取文件系统, FileSystem的get()方法得到实例fs,然后fs调动create()创建文件,open()打开文件       
        System.out.println(fs.getUri());
//实现文件读写主要包含以下步骤:
//读取hadoop文件系统配置
//实例化设置文件,configuration类实现hadoop各模块之间值的传递
//FileSystem是hadoop访问系统的抽象类,获取文件系统, FileSystem的get()方法得到实例fs,然后fs调动create()创建文件,调用open()打开文件,调用close()关闭文件


//

        Path file = new Path("/user/hadoop/myfile");



        if (fs.exists(file)) {

             System.out.println("File exists.");

        } else
            {
//
                

        FSDataOutputStream outStream = fs.create(file); //获取文件流 
        outStream.writeUTF("china cstor cstor cstor china"); //使用文件流写入文件内容


                

        }


//

// 提示:FSDataInputStream实现接口,使Hadoop中的文件输入流具有流式搜索和流式定位读取的功能
        FSDataInputStream inStream = fs.open(file);  
        String data = inStream.readUTF(); 


//输出文件状态
//FileStatus对象封装了文件的和目录的元数据,包括文件长度、块大小、权限等信息
        FileSystem hdfs = file.getFileSystem(conf);
        FileStatus[] fileStatus = hdfs.listStatus(file);
        for(FileStatus status:fileStatus)
         {
           System.out.println("FileOwer:"+status.getOwner());//所有者
           System.out.println("FileReplication:"+status.getReplication());//备份数
           System.out.println("FileModificationTime:"+new Date(status.getModificationTime()));//目录修改时间
           System.out.println("FileBlockSize:"+status.getBlockSize());//块大小
        }

        System.out.println(data);
        System.out.println("Filename:"+file.getName());
        inStream.close();
        fs.close();
    }
  }
第 3 关:倒排索引
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.util.Iterator;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.GenericOptionsParser;

public class InvertedIndex {
    public static class InvertedIndexMapper extends Mapper 
    {
        public void map(LongWritable key, Text value, Context context)  
        throws IOException, InterruptedException 

        {   
            FileSplit fileSplit = (FileSplit)context.getInputSplit();
            String fileName = fileSplit.getPath().getName();

            String word;
            IntWritable frequence=new IntWritable();
            int one=1;
            Hashtable   hashmap=new Hashtable();//key关键字设置为String
            StringTokenizer itr = new StringTokenizer(value.toString());

//

         for(;itr.hasMoreTokens(); )   
            {     

                word=itr.nextToken();  
                if(hashmap.containsKey(word)){  
                    hashmap.put(word,hashmap.get(word)+1);  
             }else{  
                    hashmap.put(word, one);                         

                }  

            }  

                                

for(Iterator it=hashmap.keySet().iterator();it.hasNext();){
                word=it.next();
                frequence=new IntWritable(hashmap.get(word));
                Text fileName_frequence = new Text(fileName+"@"+frequence.toString());//以 的格式输出
                context.write(new Text(word),fileName_frequence);
            }

        }
    }

    public static class InvertedIndexCombiner extends Reducer{
         protected void reduce(Text key,Iterable values,Context context)
         throws IOException ,InterruptedException{ 
//


            String fileName="";  
            int sum=0;  
            String num;  
            String s;  
            for (Text val : values) {  

                    s= val.toString();  
                    fileName=s.substring(0, val.find("@"));  
                    num=s.substring(val.find("@")+1, val.getLength());      //提取“doc1@1”中‘@’后面的词频  
                    sum+=Integer.parseInt(num);  
            }  
            IntWritable frequence=new IntWritable(sum);  
            context.write(key,new Text(fileName+"@"+frequence.toString()));  

                

        }
    }

    public static class InvertedIndexReducer extends Reducer 
    {   @Override
        protected void reduce(Text key, Iterable values, Context context)
                throws IOException, InterruptedException 
        {   Iterator it = values.iterator();
            StringBuilder all = new StringBuilder();
            if(it.hasNext())  all.append(it.next().toString());
            for(;it.hasNext();) {
                all.append(";");
                all.append(it.next().toString());                   
            }
//


 context.write(key, new Text(all.toString()));  

        
        }
    }

    public static void main(String[] args) 
    {
        if(args.length!=2){
            System.err.println("Usage: InvertedIndex  ");
            System.exit(2);
        }

      try {
                Configuration conf = new Configuration();
                String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

                Job job = new Job(conf, "invertedindex");
                job.setJarByClass(InvertedIndex.class);
                job.setMapperClass(InvertedIndexMapper.class);
            //

                job.setCombinerClass(InvertedIndexCombiner.class); 

                                
                job.setReducerClass(InvertedIndexReducer.class);

                job.setOutputKeyClass(Text.class);
            //

               job.setOutputValueClass(Text.class);  

                                    
                FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
                FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

                System.exit(job.waitForCompletion(true) ? 0 : 1);

        } catch (Exception e) { 
            e.printStackTrace();
        }
    }
}
第 4 关:网页排序—— PageRank 算法
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.StringTokenizer;
import java.util.Iterator;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class PageRank {

  public static class MyMapper   extends Mapper
  {
        private Text id = new Text();
        public void map(Object key, Text value, Context context ) throws IOException, InterruptedException
        {
            String line = value.toString();
//判断是否为输入文件
            if(line.substring(0,1).matches("[0-9]{1}"))
            {
                  boolean flag = false;
                  if(line.contains("_"))
                  {
                        line = line.replace("_","");
                        flag = true;
                  }
//对输入文件进行处理
                  String[] values = line.split("t");
                  Text t = new Text(values[0]);
                  String[] vals = values[1].split(" ");
                  String url="_";//保存url,用作下次计算
                  double pr = 0;
                  int i = 0;
                  int num = 0;

                  if(flag)
                  {
                      i=2;
                      pr=Double.valueOf(vals[1]);
                      num=vals.length-2;
                  }
                  else
                  {
                      i=1;
                      pr=Double.valueOf(vals[0]);
                      num=vals.length-1;
                  }

                  for(;i
  {
              private Text result = new Text();
              private Double pr = new Double(0);

         public void reduce(Text key, Iterable values,  Context context  ) throws IOException, InterruptedException
         {
              double sum=0;
              String url="";

//

  for(Text val:values)  
              {  
                      //发现_标记则表明是url,否则是外链pr,要参与计算  
                  if(!val.toString().contains("_"))  
                  {  
                      sum=sum+Double.valueOf(val.toString());  
                  }  
                  else  
                 {  
                      url=val.toString();  
                  }  
              }  
              pr=0.15+0.85*sum;  
              String str=String.format("%.3f",pr);  
              result.set(new Text(str+" "+url));  
              context.write(key,result);  


            


//



    

          }
 }

    public static void main(String[] args) throws Exception
    {
             String paths="file:///tmp/input/Wiki0";//输入文件路径,不要改动
            String path1=paths;
            String path2="";

            for(int i=1;i<=5;i++)//迭代5次
              {
                System.out.println("This is the "+i+"th job!");
                System.out.println("path1:"+path1);
                System.out.println("path2:"+path2);
                Configuration conf = new Configuration();
                Job job = new Job(conf, "PageRank");
                path2=paths+i;    
                job.setJarByClass(PageRank.class);
                job.setMapperClass(MyMapper.class);
        //

job.setCombinerClass(MyReducer.class); 

                    
                job.setReducerClass(MyReducer.class);
                job.setOutputKeyClass(Text.class);
                job.setOutputValueClass(Text.class);
                FileInputFormat.addInputPath(job, new Path(path1));
                FileOutputFormat.setOutputPath(job, new Path(path2));
                path1=path2;      
             job.waitForCompletion(true);
            System.out.println(i+"th end!");
        }
      } 
 }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号