Mapreduce案例之Pi值估算
题目:
这个程序的原理是这样的。假如有一个边长为1的正方形。以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形。在正方形里随机生成若干的点,则有些点是在扇形内,有些点是在扇形外。正方形的面积是1,扇形的面积是0.25*Pi。设点的数量一共是n,扇形内的点数量是nc,在点足够多足够密集的情况下,会近似有nc/n的比值约等于扇形面积与正方形面积的比值,也就是nc/n= 0.25*Pi/1,即Pi = 4*nc/n。
实现思路:
通过map读入文件,文件内容为投掷次数,暂时设定为100次,共10次。
然后map中,生成随机数,即x y点的坐标,计算点到(0,0)的距离,如果小于1加入到计数器in中,大于1则加入计数器out中,然后计算出pi值
reduce中,将求得的pi值再次进行求平均值。
代码如下
package Demo3; /**
* @author 星际毁灭
* 使用算法随机生成xy的坐标
* */
public class Pi {
static int digit = 40;
private int[] bases= new int[2];
private double[] baseDigit = new double[2];
private double[][] background = new double[2][digit];
private long index; Pi(int[] base) {
bases = base.clone();
index = 0; for(int i=0; i<bases.length; i++) {
double b = 1.0/bases[i];
baseDigit[i] = b;
for(int j=0; j<digit; j++) {
background[i][j] = j == 0 ? b : background[i][j-1]*b;
}
}
} double[] getNext() {
index++; double[] result = {0,0}; for(int i=0; i<bases.length; i++) {
long num = index;
int j = 0;
while(num != 0) {
result[i] += num % bases[i] * background[i][j++];
num /= bases[i];
}
} return result;
} public static void main(String[] args) {
int[] base = {2,5};
Pi test = new Pi(base);
for(int x = 0; x < 100; x++){
double[] t = test.getNext();
System.out.println(t[0] + "\t" + t[1]);
} } }
package Demo3; import java.io.IOException; 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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import Demo1.WordCountTest;
/**
* @author 星际毁灭
* 求pi的值
*
* */
public class GetPoint {
public static class Map extends Mapper<Object , Text , Text , Text>{
private static Text newKey=new Text();
private static final IntWritable one = new IntWritable(1);
public void map(Object key,Text value,Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException{
String line=value.toString();
int num=Integer.parseInt(line); //读取生成的点的数量
int[] base = {2,5}; //生成pi的xy坐标
Pi test = new Pi(base); //生成pi的xy坐标
int in=0; //在圆内
int out=0; //在圆外
newKey.set("pi");
System.out.println(num);
for(int x = 0; x < num; x++){
double[] t = test.getNext();//生成pi的xy坐标
//System.out.println(t[0] + "\t" + t[1]);
if(t[0]*t[0]+t[1]*t[1]<=1) { //该点到原点的距离小于等于1
in++;
}else {
out++;
}
}
double pi=4.0000000000*in/num; //求pi的值
context.write(newKey,new Text(pi+"")); //输出结果
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
double sum=0;
int num=0;
for(Text val:values){ //求均值
sum+=Double.parseDouble(val.toString());
num++;
//context.write(key,val);
}
double pi=sum/num; //求pi的值
String p=""+pi;
context.write(key,new Text(p)); //输出结果
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
Configuration conf=new Configuration();
Path in=new Path("hdfs://192.168.6.132:9000/wys/in/pi.txt");
Path out=new Path("hdfs://192.168.6.132:9000/wys/out/piout");
// FileInputFormat.setMaxInputSplitSize(job, size);
Job job =new Job(conf,"OneSort");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out); job.setJarByClass(GetPoint.class);
job.setMapperClass(GetPoint.Map.class);
job.setReducerClass(GetPoint.Reduce.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
Mapreduce案例之Pi值估算的更多相关文章
- mapreduce案例:获取PI的值
mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...
- Hadoop下MapReduce实现Pi值的计算
Hadoop自带的例子中,有一个计算Pi值的例子. 这个程序的原理是这样的.假如有一个边长为1的正方形.以正方形的一个端点为圆心,以1为半径,画一个圆弧,于是在正方形内就有了一个直角扇形.在正方形里随 ...
- 【Hadoop离线基础总结】MapReduce案例之自定义groupingComparator
MapReduce案例之自定义groupingComparator 求取Top 1的数据 需求 求出每一个订单中成交金额最大的一笔交易 订单id 商品id 成交金额 Order_0000005 Pdt ...
- 整理打印PI值
准备锻炼背诵PI的小数,找到PI值: PI=3. 141592653589793238462643383279502884197169399375105820974944592307816406286 ...
- c++中sin,cos,arcsin等和在C/C++中使用pi (π) 值
先 #include<math.h> 反3角函数有 acos(double),asin(double),atan(double),atan(double,double),返回值 doubl ...
- Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况
mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...
- 计算圆周率 Pi (π)值, 精确到小数点后 10000 位 只需要 30 多句代码
大家都知道π=3.1415926……无穷多位, 历史上很多人都在计算这个数, 一直认为是一个非常复杂的问题.现在有了电脑, 这个问题就简单了.电脑可以利用级数计算出很多高精度的值, 有关级数的问题请参 ...
- 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐
案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...
- 【尚学堂·Hadoop学习】MapReduce案例1--天气
案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...
随机推荐
- linux基础命令笔记
配置IP地址 vi /etc/sysconfig/network-scripts/ifcfg-eth0 忘记root密码grub e 选择kernel按e 输入single b 1:目录及文件的基本操 ...
- 在centos 7下升级内核
前言 今天读了一篇老外的文章,讲的是如何在linux环境下升级内核.比较暴力,比较简单,故做个记录. 文章中,作者先列出一个常识:linux是内核名,不是系统名.我们平时说的"lin ...
- poj3449(判断直线相交)
题目链接:https://vjudge.net/problem/POJ-3449 题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出. 思路: 首先要知道的是根据正方形对角线的两个 ...
- 1.3.4 Fork/Join框架
package com.study.forkjoin; import java.util.ArrayList; import java.util.List; import java.util.conc ...
- 1.3.3 并发容器类MAP/LIST/SET/QUEUE
HashMap 下标计算方法:hashCode & (length-1) ,&按位与操作,二进制相同位都是1,该位才为1 JDK1.7与JDK1.8中HashMap区别: JDK1.8 ...
- S02_CH01_Hello World实验
S02_CH01_Hello World实验 ZYNQ是一款SOC芯片,在前面第一季的学习当中,我们只是粗略的学习了ZYNQ的PL部分,对于ZYNQ最突出的功能,其内部的双核Cortex-A9内核并未 ...
- VMware与主机联网设置
1.编辑-虚拟机网络编辑器 2.虚拟机设置 3. 4.主机ping虚拟机
- Thread 和 Runnable
Thread 和 Runnable 1. 简介 Java 主要是通过 java.lang.Thread 类以及 java.lang.Runnable 接口实现线程机制的. Thread 类为底层操作系 ...
- 怎样获取所有style节点
通过 document.styleSheets 获取所有的样式表节点. document.styleSheets instanceof StyleSheetList; // true 注意: 1. 返 ...
- 电脑无法上网,DNS出现fec0:0:0:ffff::1%1问题
具体描述:qq,微信可用网,但其他不能用. 一.win+r 输入cmd 打开命令行:ipconfig /all 查看DNS 二.打开文本编辑器,输入如下文本: @Echo onpushd\window ...