IE2_2
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
[[Rene_情報工学実験2]]
2週目
2週目においては、セットアップの途中でインターネット接続す...
75. まず1組が4つの班に分かれ、着席する。(グループ分けは...
76. 揃った班からテキストを進めてください
77. ハブにつながっているLANケーブルを自分のPCに接続する。
78. 1週目で作成した仮想マシンを起動する。
79. マスターPCに通信できるか確かめる。
$ ping master○
(○はホワイトボードに書かれている自分のmasterの番号)
以下のように表示されていれば通信できている。
64 bytes from master○ (192.168.2.○):icmp_seq=1 ttl=64 tim...
通信できていない場合以下を確認してみる
LANケーブルが接続されているか確認する。
仮想マシンの右上の電池のマークのところをクリックしてEth...
仮想マシンの右上の電池のマークのところをクリックして右...
マスターPCの方でも上記3つを実行する。
これでできなかった場合はTAを呼ぶ。(ルーターの差し直し...
80. ここから,マスターPCにて,確認作業を行う.
マスターPCを操作する際は、sshの接続によって遠隔で操作する。
○sshによる遠隔操作の仕方(スレイブ→マスターへ移動)
ssh master○ ○は自分の班の番号
○sshによる遠隔操作からの抜け方(マスター→スレイブへ移動)
Exit
81. マスターPCで今回の課題を実行するときに使う台数を変え...
$ sudo vim $HADOOP_HOME/etc/hadoop/slaves
slave○
マスターPCのある机に移動して、マスターPCで仮想マシンの左...
82. マスターPCで以下のコマンドにより$HADOOP_HOMEの中にnam...
$ mkdir $HADOOP_HOME/name
$ mkdir $HADOOP_HOME/data
83. スレイブPCでもdataフォルダを削除する
84. スレイブPCで以下のコマンドで$HADOOP_HOMEの中にdataデ...
$ mkdir $HADOOP_HOME/data
85. マスターPCでHDFS(HaDoop FileSystemの略)を初期化
$ $HADOOP_HOME/bin/hdfs namenode –format
86. マスターPCでHDFSの起動
$ $HADOOP_HOME/sbin/start-dfs.sh
87. マスターPCでYARN関連の起動(YARNとはリソースを管理する...
$ $HADOOP_HOME/sbin/start-yarn.sh
88. マスターPCとスレイブPCでHDFSとYARN関連のプロセス起動...
$ jps
※masterでは(順不同)
2625 NameNode
2946 Jps
2823 SecondaryNameNode
3005 ResourceManager
※slaveでは
4560 NodeManager
5090 Jps
4456 DataNode
数値積分やモンテカルロ法を用いて円周率を求めるサンプルプ...
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFor...
import org.apache.hadoop.mapreduce.lib.input.SequenceFile...
import org.apache.hadoop.mapreduce.lib.output.FileOutputF...
import org.apache.hadoop.mapreduce.lib.output.SequenceFil...
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* A map/reduce program that estimates the value of Pi
* using a quasi-Monte Carlo (qMC) method.
* Arbitrary integrals can be approximated numerically by...
* In this example,
* we use a qMC method to approximate the integral $I = \...
* where $S=[0,1)^2$ is a unit square,
* $x=(x_1,x_2)$ is a 2-dimensional point,
* and $f$ is a function describing the inscribed circle ...
* $f(x)=1$ if $(2x_1-1)^2+(2x_2-1)^2 <= 1$ and $f(x)=...
* It is easy to see that Pi is equal to $4I$.
* So an approximation of Pi is obtained once $I$ is eval...
*
* There are better methods for computing Pi.
* We emphasize numerical approximation of arbitrary inte...
* For computing many digits of Pi, consider using bbp.
*
* The implementation is discussed below.
*
* Mapper:
* Generate points in a unit square
* and then count points inside/outside of the inscribe...
*
* Reducer:
* Accumulate points inside/outside results from the ma...
*
* Let numTotal = numInside + numOutside.
* The fraction numInside/numTotal is a rational approxim...
* the value (Area of the circle)/(Area of the square) = ...
* where the area of the inscribed circle is Pi/4
* and the area of unit square is 1.
* Finally, the estimated value of Pi is 4(numInside/numT...
*/
public class QuasiMonteCarlo extends Configured implement...
static final String DESCRIPTION
= "A map/reduce program that estimates Pi using a q...
/** tmp directory for input/output */
static private final String TMP_DIR_PREFIX = QuasiMonte...
/** 2-dimensional Halton sequence {H(i)},
* where H(i) is a 2-dimensional point and i >= 1 is th...
* Halton sequence is used to generate sample points fo...
*/
private static class HaltonSequence {
/** Bases */
static final int[] P = {2, 3};
/** Maximum number of digits allowed */
static final int[] K = {63, 40};
private long index;
private double[] x;
private double[][] q;
private int[][] d;
/** Initialize to H(startindex),
* so the sequence begins with H(startindex+1).
*/
HaltonSequence(long startindex) {
index = startindex;
x = new double[K.length];
q = new double[K.length][];
d = new int[K.length][];
for(int i = 0; i < K.length; i++) {
q[i] = new double[K[i]];
d[i] = new int[K[i]];
}
for(int i = 0; i < K.length; i++) {
long k = index;
x[i] = 0;
for(int j = 0; j < K[i]; j++) {
q[i][j] = (j == 0? 1.0: q[i][j-1])/P[i];
d[i][j] = (int)(k % P[i]);
k = (k - d[i][j])/P[i];
x[i] += d[i][j] * q[i][j];
}
}
}
/** Compute next point.
* Assume the current point is H(index).
* Compute H(index+1).
*
* @return a 2-dimensional point with coordinates in ...
*/
double[] nextPoint() {
index++;
for(int i = 0; i < K.length; i++) {
for(int j = 0; j < K[i]; j++) {
d[i][j]++;
x[i] += q[i][j];
if (d[i][j] < P[i]) {
break;
}
d[i][j] = 0;
x[i] -= (j == 0? 1.0: q[i][j-1]);
}
}
return x;
}
}
/**
* Mapper class for Pi estimation.
* Generate points in a unit square
* and then count points inside/outside of the inscribe...
*/
public static class QmcMapper extends
Mapper<LongWritable, LongWritable, BooleanWritable,...
/** Map method.
* @param offset samples starting from the (offset+1)...
* @param size the number of samples for this map
* @param context output {ture->numInside, false-&...
*/
public void map(LongWritable offset,
LongWritable size,
Context context)
throws IOException, InterruptedException {
final HaltonSequence haltonsequence = new HaltonSeq...
long numInside = 0L;
long numOutside = 0L;
for(long i = 0; i < size.get(); ) {
//generate points in a unit square
final double[] point = haltonsequence.nextPoint();
//count points inside/outside of the inscribed ci...
final double x = point[0] - 0.5;
final double y = point[1] - 0.5;
if (x*x + y*y > 0.25) {
numOutside++;
} else {
numInside++;
}
//report status
i++;
if (i % 1000 == 0) {
context.setStatus("Generated " + i + " samples....
}
}
//output map results
context.write(new BooleanWritable(true), new LongWr...
context.write(new BooleanWritable(false), new LongW...
}
}
/**
* Reducer class for Pi estimation.
* Accumulate points inside/outside results from the ma...
*/
public static class QmcReducer extends
Reducer<BooleanWritable, LongWritable, WritableComp...
private long numInside = 0;
private long numOutside = 0;
/**
* Accumulate number of points inside/outside results...
* @param isInside Is the points inside?
* @param values An iterator to a list of point counts
* @param context dummy, not used here.
*/
public void reduce(BooleanWritable isInside,
Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {
if (isInside.get()) {
for (LongWritable val : values) {
numInside += val.get();
}
} else {
for (LongWritable val : values) {
numOutside += val.get();
}
}
}
/**
* Reduce task done, write output to a file.
*/
@Override
public void cleanup(Context context) throws IOExcepti...
//write output to a file
Configuration conf = context.getConfiguration();
Path outDir = new Path(conf.get(FileOutputFormat.OU...
Path outFile = new Path(outDir, "reduce-out");
FileSystem fileSys = FileSystem.get(conf);
SequenceFile.Writer writer = SequenceFile.createWri...
outFile, LongWritable.class, LongWritable.class,
CompressionType.NONE);
writer.append(new LongWritable(numInside), new Long...
writer.close();
}
}
/**
* Run a map/reduce job for estimating Pi.
*
* @return the estimated value of Pi
*/
public static BigDecimal estimatePi(int numMaps, long n...
Path tmpDir, Configuration conf
) throws IOException, ClassNotFoundException, Inter...
Job job = Job.getInstance(conf);
//setup job conf
job.setJobName(QuasiMonteCarlo.class.getSimpleName());
job.setJarByClass(QuasiMonteCarlo.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputKeyClass(BooleanWritable.class);
job.setOutputValueClass(LongWritable.class);
job.setOutputFormatClass(SequenceFileOutputFormat.cla...
job.setMapperClass(QmcMapper.class);
job.setReducerClass(QmcReducer.class);
job.setNumReduceTasks(1);
// turn off speculative execution, because DFS doesn'...
// multiple writers to the same file.
job.setSpeculativeExecution(false);
//setup input/output directories
final Path inDir = new Path(tmpDir, "in");
final Path outDir = new Path(tmpDir, "out");
FileInputFormat.setInputPaths(job, inDir);
FileOutputFormat.setOutputPath(job, outDir);
final FileSystem fs = FileSystem.get(conf);
if (fs.exists(tmpDir)) {
throw new IOException("Tmp directory " + fs.makeQua...
+ " already exists. Please remove it first.");
}
if (!fs.mkdirs(inDir)) {
throw new IOException("Cannot create input director...
}
try {
//generate an input file for each map task
for(int i=0; i < numMaps; ++i) {
final Path file = new Path(inDir, "part"+i);
final LongWritable offset = new LongWritable(i * ...
final LongWritable size = new LongWritable(numPoi...
final SequenceFile.Writer writer = SequenceFile.c...
fs, conf, file,
LongWritable.class, LongWritable.class, Compr...
try {
writer.append(offset, size);
} finally {
writer.close();
}
System.out.println("Wrote input for Map #"+i);
}
//start a map/reduce job
System.out.println("Starting Job");
final long startTime = System.currentTimeMillis();
job.waitForCompletion(true);
if (!job.isSuccessful()) {
System.out.println("Job " + job.getJobID() + " fa...
System.exit(1);
}
final double duration = (System.currentTimeMillis()...
System.out.println("Job Finished in " + duration + ...
//read outputs
Path inFile = new Path(outDir, "reduce-out");
LongWritable numInside = new LongWritable();
LongWritable numOutside = new LongWritable();
SequenceFile.Reader reader = new SequenceFile.Reade...
try {
reader.next(numInside, numOutside);
} finally {
reader.close();
}
//compute estimated value
final BigDecimal numTotal
= BigDecimal.valueOf(numMaps).multiply(BigDecim...
return BigDecimal.valueOf(4).setScale(20)
.multiply(BigDecimal.valueOf(numInside.get()))
.divide(numTotal, RoundingMode.HALF_UP);
} finally {
fs.delete(tmpDir, true);
}
}
/**
* Parse arguments and then runs a map/reduce job.
* Print output in standard out.
*
* @return a non-zero if there is an error. Otherwise,...
*/
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: "+getClass().getName()+"...
ToolRunner.printGenericCommandUsage(System.err);
return 2;
}
final int nMaps = Integer.parseInt(args[0]);
final long nSamples = Long.parseLong(args[1]);
long now = System.currentTimeMillis();
int rand = new Random().nextInt(Integer.MAX_VALUE);
final Path tmpDir = new Path(TMP_DIR_PREFIX + "_" + n...
System.out.println("Number of Maps = " + nMaps);
System.out.println("Samples per Map = " + nSamples);
System.out.println("Estimated value of Pi is "
+ estimatePi(nMaps, nSamples, tmpDir, getConf()));
return 0;
}
/**
* main method for running it as a stand alone command.
*/
public static void main(String[] argv) throws Exception {
System.exit(ToolRunner.run(null, new QuasiMonteCarlo(...
}
}
$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/hadoop/map...
以下はレポート課題7をやるときに使用
学籍番号下2桁35の場合
hadoop-2.8.5のshareの中にnumber35というフォルダを作成する。
hadoop-2.8.5のshareの中にあるQuasiMonteCarlo.javaをコピー...
以下のコマンドでコンパイルする。
$HADOOP_HOME/bin/hadoop com.sun.tools.javac.Main $HADOOP_...
以下のコマンドでshare/number35に移動する。
cd $HADOOP_HOME/share/number35
以下のコマンドでjar実行ファイル作成する。
jar cf Pi.jar QuasiMonteCarlo*.class
以下のコマンドで実行する。
$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/number35/P...
89. Hdfsとyarnをいったん終了させるコマンドを打つ
$ $HADOOP_HOME/sbin/stop-yarn.sh
$ $HADOOP_HOME/sbin/stop-dfs.sh
90. 81に戻り、スレイブを追加して行う
91. 以上で、2週目における作業は終了です。
92. 仮想マシンをシャットダウンします。以下のコマンドを実...
$ poweroff
93. VirtualBoxの仮想ウィンドウが閉じ、仮想マシンが停止状...
したら、VirtualBoxを終了し、Windowsを終了します。
2週目のレポート課題
5) 自分のPCで、円周率を計算し時間を計測せよ。
6) グループ全員のPCで、円周率を計算し時間を計測せよ。
7) Hadoopのプログラムを理解して、円ではなくて球による円...
プログラムが完成したらソースコードをTAに確認してもらう。
完成したプログラムのソースコードを自分のPCに移し、レポー...
参考文献
※以下に挙げているものは、主に1週目におけるHadoopの構築...
[1] 「Hadoop 第3版」オライリージャパン
[2] 「Hadoop徹底入門 第2版 オープンソース分散処理環境の構...
終了行:
[[Rene_情報工学実験2]]
2週目
2週目においては、セットアップの途中でインターネット接続す...
75. まず1組が4つの班に分かれ、着席する。(グループ分けは...
76. 揃った班からテキストを進めてください
77. ハブにつながっているLANケーブルを自分のPCに接続する。
78. 1週目で作成した仮想マシンを起動する。
79. マスターPCに通信できるか確かめる。
$ ping master○
(○はホワイトボードに書かれている自分のmasterの番号)
以下のように表示されていれば通信できている。
64 bytes from master○ (192.168.2.○):icmp_seq=1 ttl=64 tim...
通信できていない場合以下を確認してみる
LANケーブルが接続されているか確認する。
仮想マシンの右上の電池のマークのところをクリックしてEth...
仮想マシンの右上の電池のマークのところをクリックして右...
マスターPCの方でも上記3つを実行する。
これでできなかった場合はTAを呼ぶ。(ルーターの差し直し...
80. ここから,マスターPCにて,確認作業を行う.
マスターPCを操作する際は、sshの接続によって遠隔で操作する。
○sshによる遠隔操作の仕方(スレイブ→マスターへ移動)
ssh master○ ○は自分の班の番号
○sshによる遠隔操作からの抜け方(マスター→スレイブへ移動)
Exit
81. マスターPCで今回の課題を実行するときに使う台数を変え...
$ sudo vim $HADOOP_HOME/etc/hadoop/slaves
slave○
マスターPCのある机に移動して、マスターPCで仮想マシンの左...
82. マスターPCで以下のコマンドにより$HADOOP_HOMEの中にnam...
$ mkdir $HADOOP_HOME/name
$ mkdir $HADOOP_HOME/data
83. スレイブPCでもdataフォルダを削除する
84. スレイブPCで以下のコマンドで$HADOOP_HOMEの中にdataデ...
$ mkdir $HADOOP_HOME/data
85. マスターPCでHDFS(HaDoop FileSystemの略)を初期化
$ $HADOOP_HOME/bin/hdfs namenode –format
86. マスターPCでHDFSの起動
$ $HADOOP_HOME/sbin/start-dfs.sh
87. マスターPCでYARN関連の起動(YARNとはリソースを管理する...
$ $HADOOP_HOME/sbin/start-yarn.sh
88. マスターPCとスレイブPCでHDFSとYARN関連のプロセス起動...
$ jps
※masterでは(順不同)
2625 NameNode
2946 Jps
2823 SecondaryNameNode
3005 ResourceManager
※slaveでは
4560 NodeManager
5090 Jps
4456 DataNode
数値積分やモンテカルロ法を用いて円周率を求めるサンプルプ...
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFor...
import org.apache.hadoop.mapreduce.lib.input.SequenceFile...
import org.apache.hadoop.mapreduce.lib.output.FileOutputF...
import org.apache.hadoop.mapreduce.lib.output.SequenceFil...
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* A map/reduce program that estimates the value of Pi
* using a quasi-Monte Carlo (qMC) method.
* Arbitrary integrals can be approximated numerically by...
* In this example,
* we use a qMC method to approximate the integral $I = \...
* where $S=[0,1)^2$ is a unit square,
* $x=(x_1,x_2)$ is a 2-dimensional point,
* and $f$ is a function describing the inscribed circle ...
* $f(x)=1$ if $(2x_1-1)^2+(2x_2-1)^2 <= 1$ and $f(x)=...
* It is easy to see that Pi is equal to $4I$.
* So an approximation of Pi is obtained once $I$ is eval...
*
* There are better methods for computing Pi.
* We emphasize numerical approximation of arbitrary inte...
* For computing many digits of Pi, consider using bbp.
*
* The implementation is discussed below.
*
* Mapper:
* Generate points in a unit square
* and then count points inside/outside of the inscribe...
*
* Reducer:
* Accumulate points inside/outside results from the ma...
*
* Let numTotal = numInside + numOutside.
* The fraction numInside/numTotal is a rational approxim...
* the value (Area of the circle)/(Area of the square) = ...
* where the area of the inscribed circle is Pi/4
* and the area of unit square is 1.
* Finally, the estimated value of Pi is 4(numInside/numT...
*/
public class QuasiMonteCarlo extends Configured implement...
static final String DESCRIPTION
= "A map/reduce program that estimates Pi using a q...
/** tmp directory for input/output */
static private final String TMP_DIR_PREFIX = QuasiMonte...
/** 2-dimensional Halton sequence {H(i)},
* where H(i) is a 2-dimensional point and i >= 1 is th...
* Halton sequence is used to generate sample points fo...
*/
private static class HaltonSequence {
/** Bases */
static final int[] P = {2, 3};
/** Maximum number of digits allowed */
static final int[] K = {63, 40};
private long index;
private double[] x;
private double[][] q;
private int[][] d;
/** Initialize to H(startindex),
* so the sequence begins with H(startindex+1).
*/
HaltonSequence(long startindex) {
index = startindex;
x = new double[K.length];
q = new double[K.length][];
d = new int[K.length][];
for(int i = 0; i < K.length; i++) {
q[i] = new double[K[i]];
d[i] = new int[K[i]];
}
for(int i = 0; i < K.length; i++) {
long k = index;
x[i] = 0;
for(int j = 0; j < K[i]; j++) {
q[i][j] = (j == 0? 1.0: q[i][j-1])/P[i];
d[i][j] = (int)(k % P[i]);
k = (k - d[i][j])/P[i];
x[i] += d[i][j] * q[i][j];
}
}
}
/** Compute next point.
* Assume the current point is H(index).
* Compute H(index+1).
*
* @return a 2-dimensional point with coordinates in ...
*/
double[] nextPoint() {
index++;
for(int i = 0; i < K.length; i++) {
for(int j = 0; j < K[i]; j++) {
d[i][j]++;
x[i] += q[i][j];
if (d[i][j] < P[i]) {
break;
}
d[i][j] = 0;
x[i] -= (j == 0? 1.0: q[i][j-1]);
}
}
return x;
}
}
/**
* Mapper class for Pi estimation.
* Generate points in a unit square
* and then count points inside/outside of the inscribe...
*/
public static class QmcMapper extends
Mapper<LongWritable, LongWritable, BooleanWritable,...
/** Map method.
* @param offset samples starting from the (offset+1)...
* @param size the number of samples for this map
* @param context output {ture->numInside, false-&...
*/
public void map(LongWritable offset,
LongWritable size,
Context context)
throws IOException, InterruptedException {
final HaltonSequence haltonsequence = new HaltonSeq...
long numInside = 0L;
long numOutside = 0L;
for(long i = 0; i < size.get(); ) {
//generate points in a unit square
final double[] point = haltonsequence.nextPoint();
//count points inside/outside of the inscribed ci...
final double x = point[0] - 0.5;
final double y = point[1] - 0.5;
if (x*x + y*y > 0.25) {
numOutside++;
} else {
numInside++;
}
//report status
i++;
if (i % 1000 == 0) {
context.setStatus("Generated " + i + " samples....
}
}
//output map results
context.write(new BooleanWritable(true), new LongWr...
context.write(new BooleanWritable(false), new LongW...
}
}
/**
* Reducer class for Pi estimation.
* Accumulate points inside/outside results from the ma...
*/
public static class QmcReducer extends
Reducer<BooleanWritable, LongWritable, WritableComp...
private long numInside = 0;
private long numOutside = 0;
/**
* Accumulate number of points inside/outside results...
* @param isInside Is the points inside?
* @param values An iterator to a list of point counts
* @param context dummy, not used here.
*/
public void reduce(BooleanWritable isInside,
Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {
if (isInside.get()) {
for (LongWritable val : values) {
numInside += val.get();
}
} else {
for (LongWritable val : values) {
numOutside += val.get();
}
}
}
/**
* Reduce task done, write output to a file.
*/
@Override
public void cleanup(Context context) throws IOExcepti...
//write output to a file
Configuration conf = context.getConfiguration();
Path outDir = new Path(conf.get(FileOutputFormat.OU...
Path outFile = new Path(outDir, "reduce-out");
FileSystem fileSys = FileSystem.get(conf);
SequenceFile.Writer writer = SequenceFile.createWri...
outFile, LongWritable.class, LongWritable.class,
CompressionType.NONE);
writer.append(new LongWritable(numInside), new Long...
writer.close();
}
}
/**
* Run a map/reduce job for estimating Pi.
*
* @return the estimated value of Pi
*/
public static BigDecimal estimatePi(int numMaps, long n...
Path tmpDir, Configuration conf
) throws IOException, ClassNotFoundException, Inter...
Job job = Job.getInstance(conf);
//setup job conf
job.setJobName(QuasiMonteCarlo.class.getSimpleName());
job.setJarByClass(QuasiMonteCarlo.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputKeyClass(BooleanWritable.class);
job.setOutputValueClass(LongWritable.class);
job.setOutputFormatClass(SequenceFileOutputFormat.cla...
job.setMapperClass(QmcMapper.class);
job.setReducerClass(QmcReducer.class);
job.setNumReduceTasks(1);
// turn off speculative execution, because DFS doesn'...
// multiple writers to the same file.
job.setSpeculativeExecution(false);
//setup input/output directories
final Path inDir = new Path(tmpDir, "in");
final Path outDir = new Path(tmpDir, "out");
FileInputFormat.setInputPaths(job, inDir);
FileOutputFormat.setOutputPath(job, outDir);
final FileSystem fs = FileSystem.get(conf);
if (fs.exists(tmpDir)) {
throw new IOException("Tmp directory " + fs.makeQua...
+ " already exists. Please remove it first.");
}
if (!fs.mkdirs(inDir)) {
throw new IOException("Cannot create input director...
}
try {
//generate an input file for each map task
for(int i=0; i < numMaps; ++i) {
final Path file = new Path(inDir, "part"+i);
final LongWritable offset = new LongWritable(i * ...
final LongWritable size = new LongWritable(numPoi...
final SequenceFile.Writer writer = SequenceFile.c...
fs, conf, file,
LongWritable.class, LongWritable.class, Compr...
try {
writer.append(offset, size);
} finally {
writer.close();
}
System.out.println("Wrote input for Map #"+i);
}
//start a map/reduce job
System.out.println("Starting Job");
final long startTime = System.currentTimeMillis();
job.waitForCompletion(true);
if (!job.isSuccessful()) {
System.out.println("Job " + job.getJobID() + " fa...
System.exit(1);
}
final double duration = (System.currentTimeMillis()...
System.out.println("Job Finished in " + duration + ...
//read outputs
Path inFile = new Path(outDir, "reduce-out");
LongWritable numInside = new LongWritable();
LongWritable numOutside = new LongWritable();
SequenceFile.Reader reader = new SequenceFile.Reade...
try {
reader.next(numInside, numOutside);
} finally {
reader.close();
}
//compute estimated value
final BigDecimal numTotal
= BigDecimal.valueOf(numMaps).multiply(BigDecim...
return BigDecimal.valueOf(4).setScale(20)
.multiply(BigDecimal.valueOf(numInside.get()))
.divide(numTotal, RoundingMode.HALF_UP);
} finally {
fs.delete(tmpDir, true);
}
}
/**
* Parse arguments and then runs a map/reduce job.
* Print output in standard out.
*
* @return a non-zero if there is an error. Otherwise,...
*/
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: "+getClass().getName()+"...
ToolRunner.printGenericCommandUsage(System.err);
return 2;
}
final int nMaps = Integer.parseInt(args[0]);
final long nSamples = Long.parseLong(args[1]);
long now = System.currentTimeMillis();
int rand = new Random().nextInt(Integer.MAX_VALUE);
final Path tmpDir = new Path(TMP_DIR_PREFIX + "_" + n...
System.out.println("Number of Maps = " + nMaps);
System.out.println("Samples per Map = " + nSamples);
System.out.println("Estimated value of Pi is "
+ estimatePi(nMaps, nSamples, tmpDir, getConf()));
return 0;
}
/**
* main method for running it as a stand alone command.
*/
public static void main(String[] argv) throws Exception {
System.exit(ToolRunner.run(null, new QuasiMonteCarlo(...
}
}
$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/hadoop/map...
以下はレポート課題7をやるときに使用
学籍番号下2桁35の場合
hadoop-2.8.5のshareの中にnumber35というフォルダを作成する。
hadoop-2.8.5のshareの中にあるQuasiMonteCarlo.javaをコピー...
以下のコマンドでコンパイルする。
$HADOOP_HOME/bin/hadoop com.sun.tools.javac.Main $HADOOP_...
以下のコマンドでshare/number35に移動する。
cd $HADOOP_HOME/share/number35
以下のコマンドでjar実行ファイル作成する。
jar cf Pi.jar QuasiMonteCarlo*.class
以下のコマンドで実行する。
$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/number35/P...
89. Hdfsとyarnをいったん終了させるコマンドを打つ
$ $HADOOP_HOME/sbin/stop-yarn.sh
$ $HADOOP_HOME/sbin/stop-dfs.sh
90. 81に戻り、スレイブを追加して行う
91. 以上で、2週目における作業は終了です。
92. 仮想マシンをシャットダウンします。以下のコマンドを実...
$ poweroff
93. VirtualBoxの仮想ウィンドウが閉じ、仮想マシンが停止状...
したら、VirtualBoxを終了し、Windowsを終了します。
2週目のレポート課題
5) 自分のPCで、円周率を計算し時間を計測せよ。
6) グループ全員のPCで、円周率を計算し時間を計測せよ。
7) Hadoopのプログラムを理解して、円ではなくて球による円...
プログラムが完成したらソースコードをTAに確認してもらう。
完成したプログラムのソースコードを自分のPCに移し、レポー...
参考文献
※以下に挙げているものは、主に1週目におけるHadoopの構築...
[1] 「Hadoop 第3版」オライリージャパン
[2] 「Hadoop徹底入門 第2版 オープンソース分散処理環境の構...
ページ名: