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 ...
随机推荐
- EMR-LDAP配置
usersync是负责在配置policy的时候可选用户有ldap里的用户,admin是负责登录webui的 https://cwiki.apache.org/confluence/display/RA ...
- 在linux下进行数据备份
一.完全备份 完全备份是指把所有需要备份的数据全部备份.当然,完全备份可以备份整块硬盘.整个分区或某个具体的目录.完全备份的好处是数据恢复方便,因为所有的数据都在同一个备份中,所以只要恢复完全备份,所 ...
- MySQL中的触发器应用
直接上代码: /*数据库 - udi_ems_test*********************************************************************内容:在 ...
- Android试题
1. Binder:例子: https://blog.csdn.net/qq_33208587/article/details/82767720
- C - Co-prime
Given a number N, you are asked to count the number of integers between A and B inclusive which are ...
- QT 打包exe
QT打包主要方法: 1.把无措的代码进行Release编译 2.在运行完后,找到运行后生成的目录,以下是我的文件,名为result,运行类型有两种,一种是Debug,另一种是Release,我们需要的 ...
- java包装类的缓存机制(转)
出处: java包装类的缓存机制 java 包装类的缓存机制,是在Java 5中引入的一个有助于节省内存.提高性能的功能,只有在自动装箱时有效 Integer包装类 举个栗子: Integer a = ...
- 基于bootstrap selectpicker ,实现select下拉框模糊查询功能
1.html代码块 需要引入bootstrap的css js jquery bootstrap.css bootstrap-select.min.css jquery-1.11.3.min.js bo ...
- C#向远程地址发送数据
static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...
- POJ 1789 Prim
给定N个字符串,某个字符串转为另一个字符串的花费为他们每一位不相同的字符数. 求最小花费Q. Input 多组输入,以0结束. 保证N不超过2000. Output 每组输出"The hig ...