zoukankan      html  css  js  c++  java
  • 大数据实训第4天

    Hadoop自带的例子中,有一个计算Pi值的例子。

    这个程序的原理是这样的。假如有一个边长为1的正方形。以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形。在正方形里随机生成若干的点,则有些点是在扇形内,有些点是在扇形外。正方形的面积是1,扇形的面积是0.25*Pi。设点的数量一共是n,扇形内的点数量是nc,在点足够多足够密集的情况下,会近似有nc/n的比值约等于扇形面积与正方形面积的比值,也就是nc/n= 0.25*Pi/1,即Pi = 4*nc/n。

    package mapreduce1;
    /*
     *  @create by 孙丙海
     *  2019年9月3日
       *       利用MapReduce计算pi值
     * */
    import java.io.IOException;  
    import java.util.StringTokenizer;  
    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 PI.Pi;  
    
    
        public class WordCount {  
            public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
                Job job = Job.getInstance();  
                job.setJobName("WordCount");  
                job.setJarByClass(WordCount.class);  
                job.setMapperClass(doMapper.class);
                job.setReducerClass(doReducer.class);  
                job.setOutputKeyClass(Text.class);  
                job.setOutputValueClass(IntWritable.class);  
                Path in = new Path("hdfs://192.168.43.34:9000/user/hadoop/pi.txt");   //输入路径
                Path out = new Path("hdfs://192.168.43.34:9000/user/hadoop/output_pi");  //输出路径
                FileInputFormat.addInputPath(job, in);  
                FileOutputFormat.setOutputPath(job, out);  
                System.exit(job.waitForCompletion(true) ? 0 : 1);  
            }  
            public static class doMapper extends Mapper<Object, Text, Text, IntWritable>{  
                private static final IntWritable one = new IntWritable(1);  
                @Override  
                protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
                    String line =  value.toString();    
                    String word = line.toString();         //读取每个map的数值
                    //System.out.println(word);
                    int num = Integer.parseInt(word);      //转化为int类型 
                    //System.out.println(num);
                    int[] base = {2,5};
                    Pi test = new Pi(base);  
                    int a= 0;         // 是否在扇形区域内的标志符  1:在扇形区域内 2:不在扇形区域内
                    int count = 0;  // 统计在扇形区域内点的个数
                    for(int x = 0; x < num; x++){
                        double[] t = test.getNext();
                        if(t[0]*t[0]+t[1]*t[1]<1) {  //在扇形区域内
                            a=1;
                            count++;                 //在扇形区域内的个数加+
                        }
                        else {                       //不在扇形区域内
                            a=2;
                        }
                    
                    }
                    double result= count*4.00000000/num;    //每个map计算出pi的值
                    String strresule = String.valueOf(result); 
                    Text textresult = new Text();              /*转换类型为Text */
                    textresult.set(strresule);                
                    context.write(textresult, one);         //写入
                }  
            }  
            public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable>{   //reduce 整合输出
                private IntWritable result = new IntWritable();  
                @Override  
                protected void reduce(Text key, Iterable<IntWritable> values, Context context)  
                throws IOException, InterruptedException {  
                int sum = 0;  
                for (IntWritable value : values) {  
                    sum += value.get();  
                }  
                    result.set(sum); 
                    context.write(key, result);  
                }  
            }  
        }
  • 相关阅读:
    Lambda表达式详解
    MassTransit RabbitMQ 参考文档
    RabbitMQ
    LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
    LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
    LeetCode专题-Python实现之第20题:Valid Parentheses
    LeetCode专题-Python实现之第9题:Palindrome Number
    LeetCode专题-Python实现之第14题:Longest Common Prefix
    LeetCode专题-Python实现之第13题:Roman to Integer
    LeetCode专题-Python实现之第7题:Reverse Integer
  • 原文地址:https://www.cnblogs.com/xiaohaigege666/p/11455730.html
Copyright © 2011-2022 走看看