2週目
2週目においては、セットアップの途中でインターネット接続する必要がある。2週目は、Hadoopにより円周率をモンテカルロ法で求める。
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 time=0.111 ms 通信できていない場合以下を確認してみる LANケーブルが接続されているか確認する。 仮想マシンの右上の電池のマークのところをクリックしてEthernet(enp0s3)が接続状態になっていることを確認する。 仮想マシンの右上の電池のマークのところをクリックして右下の電源ボタンから電源オフを選択し、もう一度起動する。 マスターPCの方でも上記3つを実行する。 これでできなかった場合はTAを呼ぶ。(ルーターの差し直し?)
80. ここから,マスターPCにて,確認作業を行う. マスターPCを操作する際は、sshの接続によって遠隔で操作する。 ○sshによる遠隔操作の仕方(スレイブ→マスターへ移動)
ssh master○ ○は自分の班の番号 ○sshによる遠隔操作からの抜け方(マスター→スレイブへ移動)
Exit
81. マスターPCで今回の課題を実行するときに使う台数を変えるためにslaves(slaveされるPCを管理するファイル)を編集する。○は自分の番号に変更、slave○は必要に応じて削除または追加していく。下の例は、slave○のPCを使う場合。他のslaveを使うときは下に追加していく。 $ sudo vim $HADOOP_HOME/etc/hadoop/slaves
slave○
マスターPCのある机に移動して、マスターPCで仮想マシンの左上のアプリケーションからお気に入りにあるファイルをクリックする。そして、その中の「hadoop-2.8.5」をフォルダの中のnameとdataのフォルダを右クリックで削除する
82. マスターPCで以下のコマンドにより$HADOOP_HOMEの中にnameとdataディレクトリを作成する
$ 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
数値積分やモンテカルロ法を用いて円周率を求めるサンプルプログラム(Hadoop)
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.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat; 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 qMC methods. * In this example, * we use a qMC method to approximate the integral $I = \int_S f(x) dx$, * 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 of the square $S$, * $f(x)=1$ if $(2x_1-1)^2+(2x_2-1)^2 <= 1$ and $f(x)=0$, otherwise. * It is easy to see that Pi is equal to $4I$. * So an approximation of Pi is obtained once $I$ is evaluated numerically. * * There are better methods for computing Pi. * We emphasize numerical approximation of arbitrary integrals in this example. * 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 inscribed circle of the square. * * Reducer: * Accumulate points inside/outside results from the mappers. * * Let numTotal = numInside + numOutside. * The fraction numInside/numTotal is a rational approximation of * the value (Area of the circle)/(Area of the square) = $I$, * 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/numTotal). */
public class QuasiMonteCarlo extends Configured implements Tool {
static final String DESCRIPTION
= "A map/reduce program that estimates Pi using a quasi-Monte Carlo method.";
/** tmp directory for input/output */
static private final String TMP_DIR_PREFIX = QuasiMonteCarlo.class.getSimpleName();
/** 2-dimensional Halton sequence {H(i)},
* where H(i) is a 2-dimensional point and i >= 1 is the index.
* Halton sequence is used to generate sample points for Pi estimation.
*/
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 [0,1)^2
*/
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 inscribed circle of the square.
*/
public static class QmcMapper extends
Mapper<LongWritable, LongWritable, BooleanWritable, LongWritable> {
/** Map method.
* @param offset samples starting from the (offset+1)th sample.
* @param size the number of samples for this map
* @param context output {ture->numInside, false->numOutside}
*/
public void map(LongWritable offset,
LongWritable size,
Context context)
throws IOException, InterruptedException {
final HaltonSequence haltonsequence = new HaltonSequence(offset.get());
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 circle of the square
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 LongWritable(numInside));
context.write(new BooleanWritable(false), new LongWritable(numOutside));
}
}
/**
* Reducer class for Pi estimation.
* Accumulate points inside/outside results from the mappers.
*/
public static class QmcReducer extends
Reducer<BooleanWritable, LongWritable, WritableComparable<?>, Writable> {
private long numInside = 0;
private long numOutside = 0;
/**
* Accumulate number of points inside/outside results from the mappers.
* @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 IOException {
//write output to a file
Configuration conf = context.getConfiguration();
Path outDir = new Path(conf.get(FileOutputFormat.OUTDIR));
Path outFile = new Path(outDir, "reduce-out");
FileSystem fileSys = FileSystem.get(conf);
SequenceFile.Writer writer = SequenceFile.createWriter(fileSys, conf,
outFile, LongWritable.class, LongWritable.class,
CompressionType.NONE);
writer.append(new LongWritable(numInside), new LongWritable(numOutside));
writer.close();
}
}
/**
* Run a map/reduce job for estimating Pi.
*
* @return the estimated value of Pi
*/
public static BigDecimal estimatePi(int numMaps, long numPoints,
Path tmpDir, Configuration conf
) throws IOException, ClassNotFoundException, InterruptedException {
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.class);
job.setMapperClass(QmcMapper.class);
job.setReducerClass(QmcReducer.class); job.setNumReduceTasks(1);
// turn off speculative execution, because DFS doesn't handle // 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.makeQualified(tmpDir)
+ " already exists. Please remove it first.");
}
if (!fs.mkdirs(inDir)) {
throw new IOException("Cannot create input directory " + inDir);
}
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 * numPoints);
final LongWritable size = new LongWritable(numPoints);
final SequenceFile.Writer writer = SequenceFile.createWriter(
fs, conf, file,
LongWritable.class, LongWritable.class, CompressionType.NONE);
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() + " failed!");
System.exit(1);
}
final double duration = (System.currentTimeMillis() - startTime)/1000.0;
System.out.println("Job Finished in " + duration + " seconds");
//read outputs
Path inFile = new Path(outDir, "reduce-out");
LongWritable numInside = new LongWritable();
LongWritable numOutside = new LongWritable();
SequenceFile.Reader reader = new SequenceFile.Reader(fs, inFile, conf);
try {
reader.next(numInside, numOutside);
} finally {
reader.close();
}
//compute estimated value
final BigDecimal numTotal
= BigDecimal.valueOf(numMaps).multiply(BigDecimal.valueOf(numPoints));
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, return 0.
*/
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: "+getClass().getName()+" <nMaps> <nSamples>");
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 + "_" + now + "_" + rand);
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(), argv));
}
}
$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.5.jar pi 10 10000
以下はレポート課題7をやるときに使用
学籍番号下2桁35の場合 hadoop-2.8.5のshareの中にnumber35というフォルダを作成する。 hadoop-2.8.5のshareの中にあるQuasiMonteCarlo.javaをコピーしてQuasiMonteCarlo.javaをhadoop-2.8.5/share/number35の中に貼り付ける。
以下のコマンドでコンパイルする。 $HADOOP_HOME/bin/hadoop com.sun.tools.javac.Main $HADOOP_HOME/share//number35/QuasiMonteCarlo.java
以下のコマンドでshare/number35に移動する。 cd $HADOOP_HOME/share/number35
以下のコマンドでjar実行ファイル作成する。 jar cf Pi.jar QuasiMonteCarlo*.class
以下のコマンドで実行する。 $HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/number35/Pi.jar QuasiMonteCarlo 10 10000
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版 オープンソース分散処理環境の構築」翔泳社